ソースを参照

Set preference visibility immediately (fixes #2965)

arkon 5 年 前
コミット
45b5c1c262

+ 12 - 0
app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt

@@ -8,19 +8,31 @@ import androidx.preference.PreferenceManager
 import com.f2prateek.rx.preferences.Preference as RxPreference
 import com.f2prateek.rx.preferences.RxSharedPreferences
 import com.tfcporciuncula.flow.FlowSharedPreferences
+import com.tfcporciuncula.flow.Preference
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
 import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
 import eu.kanade.tachiyomi.data.track.TrackService
 import eu.kanade.tachiyomi.data.track.anilist.Anilist
+import eu.kanade.tachiyomi.util.lang.startWithCurrentValue
 import java.io.File
 import java.text.DateFormat
 import java.text.SimpleDateFormat
 import java.util.Locale
 import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.onEach
 
 fun <T> RxPreference<T>.getOrDefault(): T = get() ?: defaultValue()!!
 
+@OptIn(ExperimentalCoroutinesApi::class)
+fun <T> Preference<T>.asImmediateFlow(block: (value: T) -> Unit): Flow<T> {
+    block(get())
+    return asFlow()
+        .startWithCurrentValue { get() }
+        .onEach { block(it) }
+}
+
 private class DateFormatConverter : RxPreference.Adapter<DateFormat> {
     override fun get(key: String, preferences: SharedPreferences): DateFormat {
         val dateFormat = preferences.getString(Keys.dateFormat, "")!!

+ 9 - 9
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt

@@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
 import eu.kanade.tachiyomi.data.backup.BackupRestoreService
 import eu.kanade.tachiyomi.data.backup.models.Backup
 import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
+import eu.kanade.tachiyomi.data.preference.asImmediateFlow
 import eu.kanade.tachiyomi.ui.base.controller.DialogController
 import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe
 import eu.kanade.tachiyomi.util.preference.defaultValue
@@ -101,7 +102,7 @@ class SettingsBackupController : SettingsController() {
                     true
                 }
             }
-            val backupDir = preference {
+            preference {
                 key = Keys.backupDirectory
                 titleRes = R.string.pref_backup_directory
 
@@ -116,6 +117,9 @@ class SettingsBackupController : SettingsController() {
                     }
                 }
 
+                preferences.backupInterval().asImmediateFlow { isVisible = it > 0 }
+                    .launchIn(scope)
+
                 preferences.backupsDirectory().asFlow()
                     .onEach { path ->
                         val dir = UniFile.fromUri(context, Uri.parse(path))
@@ -123,21 +127,17 @@ class SettingsBackupController : SettingsController() {
                     }
                     .launchIn(scope)
             }
-            val backupNumber = intListPreference {
+            intListPreference {
                 key = Keys.numberOfBackups
                 titleRes = R.string.pref_backup_slots
                 entries = arrayOf("1", "2", "3", "4", "5")
                 entryValues = entries
                 defaultValue = "1"
                 summary = "%s"
-            }
 
-            preferences.backupInterval().asFlow()
-                .onEach {
-                    backupDir.isVisible = it > 0
-                    backupNumber.isVisible = it > 0
-                }
-                .launchIn(scope)
+                preferences.backupInterval().asImmediateFlow { isVisible = it > 0 }
+                    .launchIn(scope)
+            }
         }
     }
 

+ 2 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt

@@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
 import eu.kanade.tachiyomi.data.database.models.Category
 import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper
+import eu.kanade.tachiyomi.data.preference.asImmediateFlow
 import eu.kanade.tachiyomi.ui.base.controller.DialogController
 import eu.kanade.tachiyomi.util.preference.defaultValue
 import eu.kanade.tachiyomi.util.preference.entriesRes
@@ -102,8 +103,7 @@ class SettingsDownloadController : SettingsController() {
                 entries = categories.map { it.name }.toTypedArray()
                 entryValues = categories.map { it.id.toString() }.toTypedArray()
 
-                preferences.downloadNew().asFlow()
-                    .onEach { isVisible = it }
+                preferences.downloadNew().asImmediateFlow { isVisible = it }
                     .launchIn(scope)
 
                 preferences.downloadNewCategories().asFlow()

+ 3 - 7
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt

@@ -7,6 +7,7 @@ import androidx.preference.PreferenceScreen
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
 import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
+import eu.kanade.tachiyomi.data.preference.asImmediateFlow
 import eu.kanade.tachiyomi.util.preference.defaultValue
 import eu.kanade.tachiyomi.util.preference.entriesRes
 import eu.kanade.tachiyomi.util.preference.intListPreference
@@ -19,7 +20,6 @@ import eu.kanade.tachiyomi.util.preference.switchPreference
 import eu.kanade.tachiyomi.util.preference.titleRes
 import eu.kanade.tachiyomi.util.system.LocaleHelper
 import kotlinx.coroutines.flow.launchIn
-import kotlinx.coroutines.flow.onEach
 
 class SettingsGeneralController : SettingsController() {
 
@@ -151,9 +151,7 @@ class SettingsGeneralController : SettingsController() {
                 defaultValue = Values.THEME_LIGHT_DEFAULT
                 summary = "%s"
 
-                isVisible = preferences.themeMode().get() != Values.THEME_MODE_DARK
-                preferences.themeMode().asFlow()
-                    .onEach { isVisible = it != Values.THEME_MODE_DARK }
+                preferences.themeMode().asImmediateFlow { isVisible = it != Values.THEME_MODE_DARK }
                     .launchIn(scope)
 
                 onChange {
@@ -179,9 +177,7 @@ class SettingsGeneralController : SettingsController() {
                 defaultValue = Values.THEME_DARK_DEFAULT
                 summary = "%s"
 
-                isVisible = preferences.themeMode().get() != Values.THEME_MODE_LIGHT
-                preferences.themeMode().asFlow()
-                    .onEach { isVisible = it != Values.THEME_MODE_LIGHT }
+                preferences.themeMode().asImmediateFlow { isVisible = it != Values.THEME_MODE_LIGHT }
                     .launchIn(scope)
 
                 onChange {

+ 2 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt

@@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.data.database.models.Category
 import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
 import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper
+import eu.kanade.tachiyomi.data.preference.asImmediateFlow
 import eu.kanade.tachiyomi.data.preference.getOrDefault
 import eu.kanade.tachiyomi.ui.base.controller.DialogController
 import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
@@ -104,8 +105,7 @@ class SettingsLibraryController : SettingsController() {
                 entryValues = arrayOf("wifi", "ac")
                 summaryRes = R.string.pref_library_update_restriction_summary
 
-                preferences.libraryUpdateInterval().asFlow()
-                    .onEach { isVisible = it > 0 }
+                preferences.libraryUpdateInterval().asImmediateFlow { isVisible = it > 0 }
                     .launchIn(scope)
 
                 onChange {

+ 2 - 4
app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt

@@ -4,13 +4,13 @@ import androidx.biometric.BiometricManager
 import androidx.preference.PreferenceScreen
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
+import eu.kanade.tachiyomi.data.preference.asImmediateFlow
 import eu.kanade.tachiyomi.util.preference.defaultValue
 import eu.kanade.tachiyomi.util.preference.intListPreference
 import eu.kanade.tachiyomi.util.preference.summaryRes
 import eu.kanade.tachiyomi.util.preference.switchPreference
 import eu.kanade.tachiyomi.util.preference.titleRes
 import kotlinx.coroutines.flow.launchIn
-import kotlinx.coroutines.flow.onEach
 
 class SettingsSecurityController : SettingsController() {
 
@@ -38,9 +38,7 @@ class SettingsSecurityController : SettingsController() {
                 defaultValue = "0"
                 summary = "%s"
 
-                isVisible = preferences.useBiometricLock().get()
-                preferences.useBiometricLock().asFlow()
-                    .onEach { isVisible = it }
+                preferences.useBiometricLock().asImmediateFlow { isVisible = it }
                     .launchIn(scope)
             }
         }

+ 9 - 0
app/src/main/java/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt

@@ -6,6 +6,8 @@ import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.Job
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.onStart
 import kotlinx.coroutines.launch
 
 fun launchUI(block: suspend CoroutineScope.() -> Unit): Job =
@@ -17,3 +19,10 @@ fun launchIO(block: suspend CoroutineScope.() -> Unit): Job =
 @OptIn(ExperimentalCoroutinesApi::class)
 fun launchNow(block: suspend CoroutineScope.() -> Unit): Job =
     GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED, block)
+
+@OptIn(ExperimentalCoroutinesApi::class)
+fun <T> Flow<T>.startWithCurrentValue(block: () -> T?): Flow<T> {
+    return onStart {
+        block()?.let { emit(it) }
+    }
+}