Skip to content

Commit

Permalink
added title language change
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 29, 2024
1 parent 455003e commit 7bd9f2a
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 62 deletions.
7 changes: 4 additions & 3 deletions anilist/src/commonMain/graphql/ViewerMutation.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutation ViewerMutation($adult: Boolean, $color: String, $html: Boolean) {
UpdateUser(displayAdultContent: $adult, profileColor: $color) {
mutation ViewerMutation($adult: Boolean, $color: String, $html: Boolean, $title: UserTitleLanguage) {
UpdateUser(displayAdultContent: $adult, profileColor: $color, titleLanguage: $title) {
id,
name,
about(asHtml: $html),
Expand All @@ -10,7 +10,8 @@ mutation ViewerMutation($adult: Boolean, $color: String, $html: Boolean) {
bannerImage,
options {
displayAdultContent,
profileColor
profileColor,
titleLanguage
}
}
}
3 changes: 2 additions & 1 deletion anilist/src/commonMain/graphql/ViewerQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ query ViewerQuery($html: Boolean) {
bannerImage,
options {
displayAdultContent,
profileColor
profileColor,
titleLanguage
}
}
}
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.UserTitleLanguage
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -12,7 +13,8 @@ data class User(
val avatar: Avatar = Avatar(),
val banner: String? = null,
val displayAdultContent: Boolean = false,
val profileColor: String? = null
val profileColor: String? = null,
val titleLanguage: TitleLanguage? = null,
) {
constructor(query: ViewerQuery.Viewer) : this(
id = query.id,
Expand All @@ -21,7 +23,8 @@ data class User(
avatar = query.avatar.let(::Avatar),
banner = query.bannerImage?.ifBlank { null },
displayAdultContent = query.options?.displayAdultContent ?: false,
profileColor = query.options?.profileColor?.ifBlank { null }
profileColor = query.options?.profileColor?.ifBlank { null },
titleLanguage = TitleLanguage.fromUser(query.options?.titleLanguage)
)

constructor(mutation: ViewerMutation.UpdateUser) : this(
Expand All @@ -31,7 +34,8 @@ data class User(
avatar = mutation.avatar.let(::Avatar),
banner = mutation.bannerImage?.ifBlank { null },
displayAdultContent = mutation.options?.displayAdultContent ?: false,
profileColor = mutation.options?.profileColor?.ifBlank { null }
profileColor = mutation.options?.profileColor?.ifBlank { null },
titleLanguage = TitleLanguage.fromUser(mutation.options?.titleLanguage)
)

@Serializable
Expand All @@ -49,4 +53,25 @@ data class User(
large = mutation?.large?.ifBlank { null }
)
}

@Serializable
sealed interface TitleLanguage {
@Serializable
data object Romaji : TitleLanguage

@Serializable
data object English : TitleLanguage

@Serializable
data object Native : TitleLanguage

companion object {
fun fromUser(user: UserTitleLanguage?): TitleLanguage? = when (user) {
UserTitleLanguage.ROMAJI, UserTitleLanguage.ROMAJI_STYLISED -> Romaji
UserTitleLanguage.ENGLISH, UserTitleLanguage.ENGLISH_STYLISED -> English
UserTitleLanguage.NATIVE, UserTitleLanguage.NATIVE_STYLISED -> Native
else -> null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@ package dev.datlag.aniflow.common
import dev.datlag.aniflow.anilist.TrendingQuery
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

actual fun Medium.Title.preferred(): String {
return this.userPreferred?.ifBlank { null } ?: run {
actual fun Medium.Title.preferred(setting: AppSettings.TitleLanguage?): 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)

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) {
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 }
?: this.romaji?.ifBlank { null }
?: this.native?.ifBlank { null }
?: ""
is AppSettings.TitleLanguage.Native -> this.native?.ifBlank { null }
?: this.romaji?.ifBlank { null }
?: this.english?.ifBlank { null }
?: ""
}
}

return this.userPreferred?.ifBlank { null } ?: run {
if (isJapanese) {
this.native?.ifBlank { null }
?: this.romaji?.ifBlank { null }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package dev.datlag.aniflow.common

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import dev.datlag.aniflow.LocalDI
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
import dev.datlag.aniflow.anilist.type.MediaRankType
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
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

fun Medium.preferred(): String {
return this.title.preferred().ifBlank { this.id.toString() }
@Composable
fun Medium.preferred(setting: AppSettings.TitleLanguage? = null): String {
val appSettings by LocalDI.current.instance<Settings.PlatformAppSettings>()
val title by appSettings.titleLanguage.collectAsStateWithLifecycle(null)

return this.title.preferred(setting ?: title).ifBlank { this.id.toString() }
}

fun Medium.notPreferred(): String? {
return this.title.notPreferred()?.ifBlank { null }
@Composable
fun Medium.notPreferred(setting: AppSettings.TitleLanguage? = null): String? {
val appSettings by LocalDI.current.instance<Settings.PlatformAppSettings>()
val title by appSettings.titleLanguage.collectAsStateWithLifecycle(null)

return this.title.notPreferred(setting ?: title)?.ifBlank { null }
}

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

@Composable
fun Medium.Title.notPreferred(setting: AppSettings.TitleLanguage? = null): String? {
val appSettings by LocalDI.current.instance<Settings.PlatformAppSettings>()
val title by appSettings.titleLanguage.collectAsStateWithLifecycle(null)

fun Medium.Title.notPreferred(): String? {
val preferred = this.preferred().trim()
val preferred = this.preferred(setting ?: title).trim()
val notPreferred = when {
this.native?.trim().equals(preferred, ignoreCase = true) -> {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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.UserTitleLanguage
import dev.icerock.moko.resources.StringResource
import dev.datlag.aniflow.settings.model.AppSettings

Expand All @@ -19,4 +21,24 @@ fun AppSettings.Color.toComposeString(): StringResource = when (this) {
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 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
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
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import dev.datlag.aniflow.anilist.PopularNextSeasonStateMachine
import dev.datlag.aniflow.anilist.PopularSeasonStateMachine
import dev.datlag.aniflow.anilist.TrendingAnimeStateMachine
import dev.datlag.aniflow.anilist.state.SeasonState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.updateAndGet
import dev.datlag.tooling.compose.ioDispatcher
import kotlinx.coroutines.flow.*

data object StateSaver {
var sekretLibraryLoaded: Boolean = false
Expand Down Expand Up @@ -69,7 +67,9 @@ data object StateSaver {
_popularNextState
) { t1, t2, t3, t4 ->
t1.isLoadingOrWaiting && t2.isLoadingOrWaiting && t3.isLoadingOrWaiting && t4.isLoadingOrWaiting
}
}.flowOn(
context = ioDispatcher()
)

fun updateAiring(state: AiringTodayStateMachine.State) = _airingState.updateAndGet {
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.apollographql.apollo3.api.Optional
import dev.datlag.aniflow.anilist.ViewerMutation
import dev.datlag.aniflow.anilist.ViewerQuery
import dev.datlag.aniflow.anilist.model.User
import dev.datlag.aniflow.common.toMutation
import dev.datlag.aniflow.common.toSettings
import dev.datlag.aniflow.model.safeFirstOrNull
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
Expand Down Expand Up @@ -56,7 +58,8 @@ class UserHelper(
user?.also {
appSettings.setData(
adultContent = it.displayAdultContent,
color = AppSettings.Color.fromString(it.profileColor)
color = AppSettings.Color.fromString(it.profileColor),
titleLanguage = it.titleLanguage.toSettings()
)
}
)
Expand Down Expand Up @@ -109,6 +112,21 @@ class UserHelper(
}
}

suspend fun updateTitleLanguage(value: AppSettings.TitleLanguage?) {
appSettings.setTitleLanguage(value)

if (value != null) {
changedUser.emit(
client.mutation(
ViewerMutation(
title = Optional.presentIfNotNull(value.toMutation()),
html = Optional.present(true)
)
).execute().data?.UpdateUser?.let(::User)
)
}
}

private suspend fun updateStoredToken(tokenResponse: AccessTokenResponse) {
userSettings.setAniListTokens(
access = tokenResponse.access_token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -14,10 +15,16 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import coil3.compose.rememberAsyncImagePainter
import dev.datlag.aniflow.LocalDI
import dev.datlag.aniflow.anilist.AiringQuery
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.common.preferred
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.ui.theme.SchemeTheme
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import org.kodein.di.instance
import org.kodein.di.instanceOrNull

@Composable
fun AiringCard(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ interface SettingsComponent : Component {
val user: Flow<User?>
val adultContent: Flow<Boolean>
val selectedColor: Flow<AppSettings.Color?>
val selectedTitleLanguage: Flow<AppSettings.TitleLanguage?>

fun changeAdultContent(value: Boolean)
fun changeProfileColor(value: AppSettings.Color?)
fun changeTitleLanguage(value: AppSettings.TitleLanguage?)
}
Loading

0 comments on commit 7bd9f2a

Please sign in to comment.