From 45b5c1c26249616d0d757265accadce9c72d4b09 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 26 Apr 2020 16:26:42 -0400 Subject: [PATCH] Set preference visibility immediately (fixes #2965) --- .../data/preference/PreferencesHelper.kt | 12 ++++++++++++ .../ui/setting/SettingsBackupController.kt | 18 +++++++++--------- .../ui/setting/SettingsDownloadController.kt | 4 ++-- .../ui/setting/SettingsGeneralController.kt | 10 +++------- .../ui/setting/SettingsLibraryController.kt | 4 ++-- .../ui/setting/SettingsSecurityController.kt | 6 ++---- .../util/lang/CoroutinesExtensions.kt | 9 +++++++++ 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 2fca7fde6b..80893023bf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/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 RxPreference.getOrDefault(): T = get() ?: defaultValue()!! +@OptIn(ExperimentalCoroutinesApi::class) +fun Preference.asImmediateFlow(block: (value: T) -> Unit): Flow { + block(get()) + return asFlow() + .startWithCurrentValue { get() } + .onEach { block(it) } +} + private class DateFormatConverter : RxPreference.Adapter { override fun get(key: String, preferences: SharedPreferences): DateFormat { val dateFormat = preferences.getString(Keys.dateFormat, "")!! diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 2ae61edcae..87c5f9d663 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/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) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index 4e6cc5e39a..bb9b9ed06b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/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() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index 971535c9a6..acdc9e0859 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/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 { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index 0dfa2f688a..fa13afcf1c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/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 { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt index c9207f5023..6fb3a1e663 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSecurityController.kt +++ b/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) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt index c126c028a0..576493d7cf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/lang/CoroutinesExtensions.kt +++ b/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 Flow.startWithCurrentValue(block: () -> T?): Flow { + return onStart { + block()?.let { emit(it) } + } +}