From 6554735c6323be15143562ba6fbbded0774f5b80 Mon Sep 17 00:00:00 2001 From: tarsin Date: Sat, 8 Jul 2023 18:07:53 +0800 Subject: [PATCH] Make Favscene drawer aware of Settings fav changes --- .../main/java/com/hippo/ehviewer/Settings.kt | 21 ++++++++++++------- .../com/hippo/ehviewer/SettingsCollector.kt | 3 +++ .../hippo/ehviewer/ui/scene/FavoritesScene.kt | 8 +++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.kt b/app/src/main/java/com/hippo/ehviewer/Settings.kt index af600ab9a..4f9b18ba8 100755 --- a/app/src/main/java/com/hippo/ehviewer/Settings.kt +++ b/app/src/main/java/com/hippo/ehviewer/Settings.kt @@ -5,7 +5,11 @@ package com.hippo.ehviewer import com.hippo.ehviewer.client.EhUtils import com.hippo.ehviewer.client.data.FavListUrlBuilder import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asFlow +import kotlinx.coroutines.flow.conflate +import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.flow.flatMapMerge import splitties.preferences.DataStorePreferences import splitties.preferences.edit import java.util.Locale @@ -14,13 +18,18 @@ import kotlin.reflect.KProperty object Settings : DataStorePreferences(null) { private const val KEY_SHOW_TAG_TRANSLATIONS = "show_tag_translations" + private val _favFlow = MutableSharedFlow() + val favChangesFlow = _favFlow.debounce(1000) + var favCat by stringArrayPref("fav_cat", 10, "Favorites").emitTo(_favFlow) + var favCount by intArrayPref("fav_count", 10).emitTo(_favFlow) + var favLocalCount by intPref("fav_local", 0).emitTo(_favFlow) + var favCloudCount by intPref("fav_cloud", 0).emitTo(_favFlow) + var downloadScheme by stringOrNullPref("image_scheme", null) var downloadAuthority by stringOrNullPref("image_authority", null) var downloadPath by stringOrNullPref("image_path", null) var downloadQuery by stringOrNullPref("image_query", null) var downloadFragment by stringOrNullPref("image_fragment", null) - var favCat by stringArrayPref("fav_cat", 10, "Favorites") - var favCount by intArrayPref("fav_count", 10) var archivePasswds by stringSetOrNullPref("archive_passwds") var downloadDelay by intFromStrPref("download_delay", 0) var gallerySite by intFromStrPref("gallery_site", 0).observed { updateWhenGallerySiteChanges() } @@ -57,8 +66,6 @@ object Settings : DataStorePreferences(null) { var enableQuic by boolPref("enable_quic", true) var bypassCloudflare by boolPref("bypass_cloudflare", false) var thumbSizeDp by intPref("thumb_size_", 120) - var favLocalCount by intPref("fav_local", 0) - var favCloudCount by intPref("fav_cloud", 0) var recentFavCat by intPref("recent_fav_cat", FavListUrlBuilder.FAV_CAT_ALL) var securityDelay by intPref("require_unlock_delay", 0) var clipboardTextHashCode by intPref("clipboard_text_hash_code", 0) @@ -96,7 +103,7 @@ object Settings : DataStorePreferences(null) { } private fun intArrayPref(key: String, count: Int) = object : Delegate { - override val flowGetter: () -> Flow = { flow { } } + override val flowGetter: () -> Flow = { _value.asFlow().flatMapMerge { it.changesFlow() }.conflate() } private var _value = (0 until count).map { intPref("${key}_$it", 0) }.toTypedArray() override fun getValue(thisRef: Any?, prop: KProperty<*>?): IntArray = _value.map { it.value }.toIntArray() override fun setValue(thisRef: Any?, prop: KProperty<*>?, value: IntArray) { @@ -106,7 +113,7 @@ object Settings : DataStorePreferences(null) { } private fun stringArrayPref(key: String, count: Int, defMetaValue: String) = object : Delegate> { - override val flowGetter: () -> Flow = { flow { } } + override val flowGetter: () -> Flow = { _value.asFlow().flatMapMerge { it.changesFlow() }.conflate() } private var _value = (0 until count).map { stringPref("${key}_$it", "$defMetaValue $it") }.toTypedArray() override fun getValue(thisRef: Any?, prop: KProperty<*>?): Array = _value.map { it.value }.toTypedArray() override fun setValue(thisRef: Any?, prop: KProperty<*>?, value: Array) { diff --git a/app/src/main/java/com/hippo/ehviewer/SettingsCollector.kt b/app/src/main/java/com/hippo/ehviewer/SettingsCollector.kt index 1f9275575..a64bd9628 100644 --- a/app/src/main/java/com/hippo/ehviewer/SettingsCollector.kt +++ b/app/src/main/java/com/hippo/ehviewer/SettingsCollector.kt @@ -13,6 +13,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.launch import splitties.init.appCtx import splitties.preferences.PrefDelegate @@ -20,6 +21,8 @@ import splitties.preferences.PrefDelegate private val collectScope = CoroutineScope(Dispatchers.Main + SupervisorJob()) fun > R.observed(func: (Unit) -> Unit) = apply { collectScope.launch { changesFlow().collect(func) } } fun > R.observed(func: (Unit) -> Unit) = apply { collectScope.launch { flowGetter().collect(func) } } +fun > R.emitTo(flow: MutableSharedFlow) = apply { collectScope.launch { flowGetter().collect { flow.emit(Unit) } } } +fun > R.emitTo(flow: MutableSharedFlow) = apply { collectScope.launch { changesFlow().collect { flow.emit(Unit) } } } fun updateWhenLocaleChanges() { val newValue = Settings.language diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index 327abe92f..dc4485344 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -30,6 +30,8 @@ import androidx.compose.material3.ElevatedCard import androidx.compose.material3.ListItem import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.currentRecomposeScope import androidx.compose.ui.Modifier import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.res.stringResource @@ -315,6 +317,12 @@ class FavoritesScene : return ComposeView(inflater.context).apply { setMD3Content { ElevatedCard { + val scope = currentRecomposeScope + LaunchedEffect(Unit) { + Settings.favChangesFlow.collect { + scope.invalidate() + } + } val faves = arrayOf( stringResource(id = R.string.local_favorites) to Settings.favLocalCount, stringResource(id = R.string.cloud_favorites) to Settings.favCloudCount,