Skip to content

Commit

Permalink
added character language option
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 1, 2024
1 parent dd9debb commit 7f9d851
Show file tree
Hide file tree
Showing 37 changed files with 637 additions and 303 deletions.
13 changes: 10 additions & 3 deletions anilist/src/commonMain/graphql/ViewerMutation.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
mutation ViewerMutation($adult: Boolean, $color: String, $html: Boolean, $title: UserTitleLanguage) {
UpdateUser(displayAdultContent: $adult, profileColor: $color, titleLanguage: $title) {
mutation ViewerMutation(
$adult: Boolean,
$color: String,
$html: Boolean,
$title: UserTitleLanguage,
$char: UserStaffNameLanguage
) {
UpdateUser(displayAdultContent: $adult, profileColor: $color, titleLanguage: $title, staffNameLanguage: $char) {
id,
name,
about(asHtml: $html),
Expand All @@ -11,7 +17,8 @@ mutation ViewerMutation($adult: Boolean, $color: String, $html: Boolean, $title:
options {
displayAdultContent,
profileColor,
titleLanguage
titleLanguage,
staffNameLanguage
}
}
}
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/ViewerQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ query ViewerQuery($html: Boolean) {
options {
displayAdultContent,
profileColor,
titleLanguage
titleLanguage,
staffNameLanguage
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.datlag.aniflow.anilist.model

import dev.datlag.aniflow.anilist.ViewerMutation
import dev.datlag.aniflow.anilist.ViewerQuery
import dev.datlag.aniflow.anilist.type.UserStaffNameLanguage
import dev.datlag.aniflow.anilist.type.UserTitleLanguage
import kotlinx.serialization.Serializable

Expand All @@ -15,6 +16,7 @@ data class User(
val displayAdultContent: Boolean = false,
val profileColor: String? = null,
val titleLanguage: TitleLanguage? = null,
val charLanguage: CharLanguage? = null
) {
constructor(query: ViewerQuery.Viewer) : this(
id = query.id,
Expand All @@ -24,7 +26,8 @@ data class User(
banner = query.bannerImage?.ifBlank { null },
displayAdultContent = query.options?.displayAdultContent ?: false,
profileColor = query.options?.profileColor?.ifBlank { null },
titleLanguage = TitleLanguage.fromUser(query.options?.titleLanguage)
titleLanguage = TitleLanguage.fromUser(query.options?.titleLanguage),
charLanguage = CharLanguage.fromUser(query.options?.staffNameLanguage)
)

constructor(mutation: ViewerMutation.UpdateUser) : this(
Expand All @@ -35,7 +38,8 @@ data class User(
banner = mutation.bannerImage?.ifBlank { null },
displayAdultContent = mutation.options?.displayAdultContent ?: false,
profileColor = mutation.options?.profileColor?.ifBlank { null },
titleLanguage = TitleLanguage.fromUser(mutation.options?.titleLanguage)
titleLanguage = TitleLanguage.fromUser(mutation.options?.titleLanguage),
charLanguage = CharLanguage.fromUser(mutation.options?.staffNameLanguage)
)

@Serializable
Expand Down Expand Up @@ -74,4 +78,25 @@ data class User(
}
}
}

@Serializable
sealed interface CharLanguage {
@Serializable
data object RomajiWestern : CharLanguage

@Serializable
data object Romaji : CharLanguage

@Serializable
data object Native : CharLanguage

companion object {
fun fromUser(user: UserStaffNameLanguage?): CharLanguage? = when (user) {
UserStaffNameLanguage.ROMAJI_WESTERN-> RomajiWestern
UserStaffNameLanguage.ROMAJI -> Romaji
UserStaffNameLanguage.NATIVE -> Native
else -> null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@ import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.settings.model.AppSettings
import java.util.Locale
import dev.datlag.aniflow.settings.model.TitleLanguage as SettingsTitle
import dev.datlag.aniflow.settings.model.CharLanguage as SettingsChar
import dev.datlag.aniflow.model.appendWithSpace

actual fun Medium.Title.preferred(setting: AppSettings.TitleLanguage?): String {
actual fun Medium.Title.preferred(setting: SettingsTitle?): String {
val locale = Locale.getDefault()
val isJapanese = locale.language.equals("jp", ignoreCase = true)
|| locale.language.equals("ja", ignoreCase = true)
|| locale.isO3Language.equals("jpn", ignoreCase = true)

if (setting != null) {
return when (setting) {
is AppSettings.TitleLanguage.Romaji -> this.romaji?.ifBlank { null } ?: if (isJapanese) {
is SettingsTitle.Romaji -> this.romaji?.ifBlank { null } ?: if (isJapanese) {
this.native?.ifBlank { null } ?: this.english?.ifBlank { null }
} else {
this.english?.ifBlank { null } ?: this.native?.ifBlank { null }
} ?: ""
is AppSettings.TitleLanguage.English -> this.english?.ifBlank { null }
is SettingsTitle.English -> this.english?.ifBlank { null }
?: this.romaji?.ifBlank { null }
?: this.native?.ifBlank { null }
?: ""
is AppSettings.TitleLanguage.Native -> this.native?.ifBlank { null }
is SettingsTitle.Native -> this.native?.ifBlank { null }
?: this.romaji?.ifBlank { null }
?: this.english?.ifBlank { null }
?: ""
Expand All @@ -43,7 +46,41 @@ actual fun Medium.Title.preferred(setting: AppSettings.TitleLanguage?): String {
} ?: ""
}

actual fun Character.Name.preferred(): String {
actual fun Character.Name.preferred(setting: SettingsChar?): String {
if (setting != null) {
return when (setting) {
is SettingsChar.RomajiWestern -> buildString {
appendWithSpace(this@preferred.first)
appendWithSpace(this@preferred.middle)
appendWithSpace(this@preferred.last)
}.trim().ifBlank { null }
?: this.userPreferred?.ifBlank { null }
?: this.full?.ifBlank { null }
?: this.native?.ifBlank { null }
?: ""

is SettingsChar.Romaji -> buildString {
appendWithSpace(this@preferred.last)
appendWithSpace(this@preferred.middle)
appendWithSpace(this@preferred.first)
}.trim().ifBlank { null }
?: this.userPreferred?.ifBlank { null }
?: this.full?.ifBlank { null }
?: this.native?.ifBlank { null }
?: ""

is SettingsChar.Native -> this.native?.ifBlank { null }
?: this.userPreferred?.ifBlank { null }
?: buildString {
appendWithSpace(this@preferred.last)
appendWithSpace(this@preferred.middle)
appendWithSpace(this@preferred.first)
}.trim().ifBlank { null }
?: this.full?.ifBlank { null }
?: ""
}
}

return this.userPreferred?.ifBlank { null } ?: run {

val locale = Locale.getDefault()
Expand All @@ -55,20 +92,16 @@ actual fun Character.Name.preferred(): String {
this.native?.ifBlank { null }
?: this.full?.ifBlank { null }
?: buildString {
append(this@preferred.first)
append(" ")
append(this@preferred.middle)
append(" ")
append(this@preferred.last)
appendWithSpace(this@preferred.first)
appendWithSpace(this@preferred.middle)
appendWithSpace(this@preferred.last)
}.ifBlank { null }
} else {
this.full?.ifBlank { null }
?: buildString {
append(this@preferred.first)
append(" ")
append(this@preferred.middle)
append(" ")
append(this@preferred.last)
appendWithSpace(this@preferred.first)
appendWithSpace(this@preferred.middle)
appendWithSpace(this@preferred.last)
}.ifBlank { null }
?: this.native
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import dev.datlag.aniflow.trace.model.SearchResponse
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import dev.icerock.moko.resources.StringResource
import org.kodein.di.instance
import dev.datlag.aniflow.settings.model.TitleLanguage as SettingsTitle
import dev.datlag.aniflow.settings.model.CharLanguage as SettingsChar

fun Medium.preferred(setting: AppSettings.TitleLanguage? = null): String {
fun Medium.preferred(setting: SettingsTitle?): String {
return this.title.preferred(setting).ifBlank { this.id.toString() }
}

fun Medium.notPreferred(setting: AppSettings.TitleLanguage? = null): String? {
fun Medium.notPreferred(setting: SettingsTitle?): String? {
return this.title.notPreferred(setting)?.ifBlank { null }
}

expect fun Medium.Title.preferred(setting: AppSettings.TitleLanguage? = null): String
expect fun Medium.Title.preferred(setting: SettingsTitle?): String

fun Medium.Title.notPreferred(setting: AppSettings.TitleLanguage? = null): String? {
fun Medium.Title.notPreferred(setting: SettingsTitle?): String? {
val preferred = this.preferred(setting).trim()
val notPreferred = when {
this.native?.trim().equals(preferred, ignoreCase = true) -> {
Expand Down Expand Up @@ -127,9 +129,9 @@ fun Collection<Medium.Ranking>.popular(): Medium.Ranking? {

fun Medium.popular(): Medium.Ranking? = this.ranking.popular()

expect fun Character.Name.preferred(): String
expect fun Character.Name.preferred(setting: SettingsChar?): String

fun Character.preferredName(): String = this.name.preferred()
fun Character.preferredName(settings: SettingsChar?): String = this.name.preferred(settings)

private fun SearchResponse.Result.AniList.Title?.asMediumTitle(): Medium.Title {
return Medium.Title(
Expand All @@ -147,6 +149,4 @@ fun SearchResponse.Result.AniList.asMedium(): Medium {
_isAdult = this.isAdult,
title = this.title.asMediumTitle()
)
}

fun SearchResponse.Result.AniList.Title.preferred(): String = this.asMediumTitle().preferred()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,65 @@ package dev.datlag.aniflow.common
import androidx.compose.ui.graphics.Color
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.anilist.model.User
import dev.datlag.aniflow.anilist.type.UserStaffNameLanguage
import dev.datlag.aniflow.anilist.type.UserTitleLanguage
import dev.icerock.moko.resources.StringResource
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.settings.model.Color as SettingsColor
import dev.datlag.aniflow.settings.model.TitleLanguage as SettingsTitle
import dev.datlag.aniflow.settings.model.CharLanguage as SettingsChar

@OptIn(ExperimentalStdlibApi::class)
fun AppSettings.Color.toComposeColor() = Color(
fun SettingsColor.toComposeColor() = Color(
this.hex.substringAfter('#').hexToLong() or 0x00000000FF000000
)

fun AppSettings.Color.toComposeString(): StringResource = when (this) {
is AppSettings.Color.Blue -> SharedRes.strings.color_blue
is AppSettings.Color.Purple -> SharedRes.strings.color_purple
is AppSettings.Color.Pink -> SharedRes.strings.color_pink
is AppSettings.Color.Orange -> SharedRes.strings.color_orange
is AppSettings.Color.Red -> SharedRes.strings.color_red
is AppSettings.Color.Green -> SharedRes.strings.color_green
is AppSettings.Color.Gray -> SharedRes.strings.color_gray
is AppSettings.Color.Custom -> SharedRes.strings.color_custom
fun SettingsColor.toComposeString(): StringResource = when (this) {
is SettingsColor.Blue -> SharedRes.strings.color_blue
is SettingsColor.Purple -> SharedRes.strings.color_purple
is SettingsColor.Pink -> SharedRes.strings.color_pink
is SettingsColor.Orange -> SharedRes.strings.color_orange
is SettingsColor.Red -> SharedRes.strings.color_red
is SettingsColor.Green -> SharedRes.strings.color_green
is SettingsColor.Gray -> SharedRes.strings.color_gray
is SettingsColor.Custom -> SharedRes.strings.color_custom
}

fun User.TitleLanguage?.toSettings(): AppSettings.TitleLanguage? = when (this) {
is User.TitleLanguage.Romaji -> AppSettings.TitleLanguage.Romaji
is User.TitleLanguage.English -> AppSettings.TitleLanguage.English
is User.TitleLanguage.Native -> AppSettings.TitleLanguage.Native
fun User.TitleLanguage?.toSettings(): SettingsTitle? = when (this) {
is User.TitleLanguage.Romaji -> SettingsTitle.Romaji
is User.TitleLanguage.English -> SettingsTitle.English
is User.TitleLanguage.Native -> SettingsTitle.Native
else -> null
}

fun AppSettings.TitleLanguage?.toMutation(): UserTitleLanguage? = when (this) {
is AppSettings.TitleLanguage.Romaji -> UserTitleLanguage.ROMAJI
is AppSettings.TitleLanguage.English -> UserTitleLanguage.ENGLISH
is AppSettings.TitleLanguage.Native -> UserTitleLanguage.NATIVE
fun SettingsTitle?.toMutation(): UserTitleLanguage? = when (this) {
is SettingsTitle.Romaji -> UserTitleLanguage.ROMAJI
is SettingsTitle.English -> UserTitleLanguage.ENGLISH
is SettingsTitle.Native -> UserTitleLanguage.NATIVE
else -> null
}

fun AppSettings.TitleLanguage.toComposeString(): StringResource = when (this) {
is AppSettings.TitleLanguage.Romaji -> SharedRes.strings.title_romaji
is AppSettings.TitleLanguage.English -> SharedRes.strings.title_english
is AppSettings.TitleLanguage.Native -> SharedRes.strings.title_native
fun SettingsTitle.toComposeString(): StringResource = when (this) {
is SettingsTitle.Romaji -> SharedRes.strings.title_romaji
is SettingsTitle.English -> SharedRes.strings.title_english
is SettingsTitle.Native -> SharedRes.strings.title_native
}

fun User.CharLanguage?.toSettings(): SettingsChar? = when (this) {
is User.CharLanguage.RomajiWestern -> SettingsChar.RomajiWestern
is User.CharLanguage.Romaji -> SettingsChar.Romaji
is User.CharLanguage.Native -> SettingsChar.Native
else -> null
}

fun SettingsChar?.toMutation(): UserStaffNameLanguage? = when (this) {
is SettingsChar.RomajiWestern -> UserStaffNameLanguage.ROMAJI_WESTERN
is SettingsChar.Romaji -> UserStaffNameLanguage.ROMAJI
is SettingsChar.Native -> UserStaffNameLanguage.NATIVE
else -> null
}

fun SettingsChar.toComposeString(): StringResource = when (this) {
is SettingsChar.RomajiWestern -> SharedRes.strings.char_romaji_western
is SettingsChar.Romaji -> SharedRes.strings.char_romaji
is SettingsChar.Native -> SharedRes.strings.char_native
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import dev.datlag.tooling.compose.ioDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlin.time.Duration.Companion.milliseconds
import dev.datlag.aniflow.settings.model.Color as SettingsColor

data object StateSaver {
var sekretLibraryLoaded: Boolean = false
val temporaryColor = MutableStateFlow<AppSettings.Color?>(null)
val temporaryColor = MutableStateFlow<SettingsColor?>(null)

fun updateTemporaryColor(value: AppSettings.Color?) = temporaryColor.update { value }
fun updateTemporaryColor(value: SettingsColor?) = temporaryColor.update { value }

data object List {
var homeOverview: Int = 0
Expand Down
Loading

0 comments on commit 7f9d851

Please sign in to comment.