Set preference visibility immediately (fixes #2965)

This commit is contained in:
arkon 2020-04-26 16:26:42 -04:00
parent 7fd547a2d6
commit 45b5c1c262
7 changed files with 39 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
fun launchUI(block: suspend CoroutineScope.() -> Unit): Job = fun launchUI(block: suspend CoroutineScope.() -> Unit): Job =
@ -17,3 +19,10 @@ fun launchIO(block: suspend CoroutineScope.() -> Unit): Job =
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
fun launchNow(block: suspend CoroutineScope.() -> Unit): Job = fun launchNow(block: suspend CoroutineScope.() -> Unit): Job =
GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED, block) 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) }
}
}