From 9f9f2e69d873efc3015cc441be1bef279d2d89bb Mon Sep 17 00:00:00 2001 From: DatLag Date: Fri, 10 May 2024 13:23:27 +0200 Subject: [PATCH] fix user settings not changing --- .../datlag/aniflow/module/NetworkModule.kt | 4 +++ .../dev/datlag/aniflow/other/UserHelper.kt | 33 ++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt index 7f05638..666abd2 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt @@ -10,8 +10,10 @@ import coil3.svg.SvgDecoder import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.api.http.HttpRequest import com.apollographql.apollo3.api.http.HttpResponse +import com.apollographql.apollo3.cache.normalized.FetchPolicy import com.apollographql.apollo3.cache.normalized.api.MemoryCacheFactory import com.apollographql.apollo3.cache.normalized.api.NormalizedCacheFactory +import com.apollographql.apollo3.cache.normalized.fetchPolicy import com.apollographql.apollo3.cache.normalized.normalizedCache import com.apollographql.apollo3.network.http.HttpInterceptor import com.apollographql.apollo3.network.http.HttpInterceptorChain @@ -87,6 +89,7 @@ data object NetworkModule { } }) .normalizedCache(instance(Constants.AniList.CACHE_FACTORY)) + .fetchPolicy(FetchPolicy.CacheAndNetwork) .build() } bindSingleton(Constants.AniList.FALLBACK_APOLLO_CLIENT) { @@ -94,6 +97,7 @@ data object NetworkModule { .dispatcher(ioDispatcher()) .serverUrl(Constants.AniList.SERVER_URL) .normalizedCache(instance(Constants.AniList.CACHE_FACTORY)) + .fetchPolicy(FetchPolicy.CacheAndNetwork) .build() } bindSingleton { diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/other/UserHelper.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/other/UserHelper.kt index 87561b0..3bd28fc 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/other/UserHelper.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/other/UserHelper.kt @@ -6,6 +6,8 @@ import androidx.compose.ui.platform.LocalUriHandler import androidx.compose.ui.platform.UriHandler import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.api.Optional +import com.apollographql.apollo3.cache.normalized.FetchPolicy +import com.apollographql.apollo3.cache.normalized.fetchPolicy import dev.datlag.aniflow.anilist.ViewerMutation import dev.datlag.aniflow.anilist.ViewerQuery import dev.datlag.aniflow.anilist.model.User @@ -19,6 +21,7 @@ import dev.datlag.tooling.async.suspendCatching import dev.datlag.tooling.compose.ioDispatcher import dev.datlag.tooling.compose.withIOContext import dev.datlag.tooling.compose.withMainContext +import io.github.aakira.napier.Napier import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.* @@ -44,7 +47,7 @@ class UserHelper( private val updatableUser = isLoggedIn.transform { loggedIn -> if (loggedIn) { emitAll( - client.query(ViewerQuery()).toFlow().map { + client.query(ViewerQuery()).fetchPolicy(FetchPolicy.NetworkFirst).toFlow().map { it.data?.Viewer?.let(::User) } ) @@ -56,17 +59,15 @@ class UserHelper( initialValue = null ) - val user = updatableUser.transform { user -> - emit( - user?.also { - appSettings.setData( - adultContent = it.displayAdultContent, - color = SettingsColor.fromString(it.profileColor), - titleLanguage = it.titleLanguage.toSettings(), - charLanguage = it.charLanguage.toSettings() - ) - } - ) + val user = updatableUser.map { user -> + user?.also { + appSettings.setData( + adultContent = it.displayAdultContent, + color = SettingsColor.fromString(it.profileColor), + titleLanguage = it.titleLanguage.toSettings(), + charLanguage = it.charLanguage.toSettings() + ) + } }.flowOn(ioDispatcher()) suspend fun updateAdultSetting(value: Boolean) { @@ -76,7 +77,7 @@ class UserHelper( ViewerMutation( adult = Optional.present(value) ) - ).execute().data?.UpdateUser?.let(::User) + ).fetchPolicy(FetchPolicy.NetworkFirst).execute().data?.UpdateUser?.let(::User) ) } @@ -89,7 +90,7 @@ class UserHelper( ViewerMutation( color = Optional.present(value.label) ) - ).execute().data?.UpdateUser?.let(::User) + ).fetchPolicy(FetchPolicy.NetworkFirst).execute().data?.UpdateUser?.let(::User) ) } } @@ -103,7 +104,7 @@ class UserHelper( ViewerMutation( title = Optional.presentIfNotNull(value.toMutation()) ) - ).execute().data?.UpdateUser?.let(::User) + ).fetchPolicy(FetchPolicy.NetworkFirst).execute().data?.UpdateUser?.let(::User) ) } } @@ -117,7 +118,7 @@ class UserHelper( ViewerMutation( char = Optional.presentIfNotNull(value.toMutation()) ) - ).execute().data?.UpdateUser?.let(::User) + ).fetchPolicy(FetchPolicy.NetworkFirst).execute().data?.UpdateUser?.let(::User) ) } }