Refactor dependant preference visibility flows

This commit is contained in:
arkon 2021-12-17 10:11:07 -05:00
parent 818fe50f77
commit 945afc71ef
7 changed files with 24 additions and 32 deletions

View File

@ -7,7 +7,6 @@ import androidx.core.app.ActivityCompat
import androidx.preference.PreferenceScreen
import com.google.android.material.color.DynamicColors
import eu.kanade.tachiyomi.R
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.initThenAdd
@ -19,7 +18,6 @@ import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.util.system.isTablet
import eu.kanade.tachiyomi.widget.preference.ThemesPreference
import kotlinx.coroutines.flow.launchIn
import java.util.Date
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
@ -89,8 +87,7 @@ class SettingsAppearanceController : SettingsController() {
titleRes = R.string.pref_dark_theme_pure_black
defaultValue = false
preferences.themeMode().asImmediateFlow { isVisible = it != Values.ThemeMode.light }
.launchIn(viewScope)
visibleIf(preferences.themeMode()) { it != Values.ThemeMode.light }
onChange {
activity?.let { ActivityCompat.recreate(it) }

View File

@ -23,7 +23,6 @@ import eu.kanade.tachiyomi.data.backup.BackupRestoreService
import eu.kanade.tachiyomi.data.backup.full.FullBackupRestoreValidator
import eu.kanade.tachiyomi.data.backup.full.models.BackupFull
import eu.kanade.tachiyomi.data.backup.legacy.LegacyBackupRestoreValidator
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
@ -137,8 +136,7 @@ class SettingsBackupController : SettingsController() {
}
}
preferences.backupInterval().asImmediateFlow { isVisible = it > 0 }
.launchIn(viewScope)
visibleIf(preferences.backupInterval()) { it > 0 }
preferences.backupsDirectory().asFlow()
.onEach { path ->
@ -155,8 +153,7 @@ class SettingsBackupController : SettingsController() {
defaultValue = "1"
summary = "%s"
preferences.backupInterval().asImmediateFlow { isVisible = it > 0 }
.launchIn(viewScope)
visibleIf(preferences.backupInterval()) { it > 0 }
}
}

View File

@ -13,6 +13,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.animation.doOnEnd
import androidx.core.view.updatePadding
import androidx.preference.Preference
import androidx.preference.PreferenceController
import androidx.preference.PreferenceGroup
import androidx.preference.PreferenceScreen
@ -21,12 +22,14 @@ import com.bluelinelabs.conductor.ControllerChangeType
import dev.chrisbanes.insetter.applyInsetter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.ui.base.controller.BaseController
import eu.kanade.tachiyomi.ui.base.controller.RootController
import eu.kanade.tachiyomi.util.system.getResourceColor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@ -133,4 +136,9 @@ abstract class SettingsController : PreferenceController() {
(activity as? AppCompatActivity)?.supportActionBar?.title = getTitle()
}
inline fun <T> Preference.visibleIf(preference: com.tfcporciuncula.flow.Preference<T>, crossinline block: (T) -> Boolean) {
preference.asImmediateFlow { isVisible = block(it) }
.launchIn(viewScope)
}
}

View File

@ -16,7 +16,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
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
@ -133,8 +132,7 @@ class SettingsDownloadController : SettingsController() {
DownloadCategoriesDialog().showDialog(router)
}
preferences.downloadNew().asImmediateFlow { isVisible = it }
.launchIn(viewScope)
visibleIf(preferences.downloadNew()) { it }
fun updateSummary() {
val selectedCategories = preferences.downloadNewCategories().get()

View File

@ -16,7 +16,6 @@ import eu.kanade.tachiyomi.data.preference.DEVICE_ONLY_ON_WIFI
import eu.kanade.tachiyomi.data.preference.MANGA_FULLY_READ
import eu.kanade.tachiyomi.data.preference.MANGA_ONGOING
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.databinding.PrefLibraryColumnsBinding
import eu.kanade.tachiyomi.ui.base.controller.DialogController
@ -164,8 +163,7 @@ class SettingsLibraryController : SettingsController() {
entryValues = arrayOf(DEVICE_ONLY_ON_WIFI, DEVICE_CHARGING)
defaultValue = preferences.libraryUpdateDeviceRestriction().defaultValue
preferences.libraryUpdateInterval().asImmediateFlow { isVisible = it > 0 }
.launchIn(viewScope)
visibleIf(preferences.libraryUpdateInterval()) { it > 0 }
onChange {
// Post to event looper to allow the preference to be updated.
@ -203,8 +201,7 @@ class SettingsLibraryController : SettingsController() {
entryValues = arrayOf(MANGA_FULLY_READ, MANGA_ONGOING)
defaultValue = preferences.libraryUpdateMangaRestriction().defaultValue
preferences.libraryUpdateInterval().asImmediateFlow { isVisible = it > 0 }
.launchIn(viewScope)
visibleIf(preferences.libraryUpdateInterval()) { it > 0 }
fun updateSummary() {
val restrictions = preferences.libraryUpdateMangaRestriction().get()

View File

@ -5,7 +5,6 @@ import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferenceValues
import eu.kanade.tachiyomi.data.preference.PreferenceValues.TappingInvertMode
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType
import eu.kanade.tachiyomi.util.preference.defaultValue
@ -17,7 +16,6 @@ import eu.kanade.tachiyomi.util.preference.summaryRes
import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.util.system.hasDisplayCutout
import kotlinx.coroutines.flow.launchIn
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsReaderController : SettingsController() {
@ -111,7 +109,8 @@ class SettingsReaderController : SettingsController() {
key = Keys.cutoutShort
titleRes = R.string.pref_cutout_short
defaultValue = true
preferences.fullscreen().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.fullscreen()) { it }
}
}
@ -159,7 +158,7 @@ class SettingsReaderController : SettingsController() {
defaultValue = "0"
summary = "%s"
preferences.readWithTapping().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.readWithTapping()) { it }
}
listPreference {
key = Keys.pagerNavInverted
@ -179,7 +178,7 @@ class SettingsReaderController : SettingsController() {
defaultValue = TappingInvertMode.NONE.name
summary = "%s"
preferences.readWithTapping().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.readWithTapping()) { it }
}
intListPreference {
key = Keys.imageScaleType
@ -224,7 +223,7 @@ class SettingsReaderController : SettingsController() {
titleRes = R.string.pref_dual_page_invert
summaryRes = R.string.pref_dual_page_invert_summary
defaultValue = false
preferences.dualPageSplitPaged().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.dualPageSplitPaged()) { it }
}
}
@ -240,7 +239,7 @@ class SettingsReaderController : SettingsController() {
defaultValue = "0"
summary = "%s"
preferences.readWithTapping().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.readWithTapping()) { it }
}
listPreference {
key = Keys.webtoonNavInverted
@ -260,7 +259,7 @@ class SettingsReaderController : SettingsController() {
defaultValue = TappingInvertMode.NONE.name
summary = "%s"
preferences.readWithTapping().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.readWithTapping()) { it }
}
intListPreference {
key = Keys.webtoonSidePadding
@ -306,7 +305,7 @@ class SettingsReaderController : SettingsController() {
titleRes = R.string.pref_dual_page_invert
summaryRes = R.string.pref_dual_page_invert_summary
defaultValue = false
preferences.dualPageSplitWebtoon().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.dualPageSplitWebtoon()) { it }
}
}
@ -327,8 +326,7 @@ class SettingsReaderController : SettingsController() {
key = Keys.readWithVolumeKeysInverted
titleRes = R.string.pref_read_with_volume_keys_inverted
defaultValue = false
preferences.readWithVolumeKeys().asImmediateFlow { isVisible = it }.launchIn(viewScope)
visibleIf(preferences.readWithVolumeKeys()) { it }
}
}

View File

@ -5,7 +5,6 @@ import androidx.fragment.app.FragmentActivity
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R
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.requireAuthentication
@ -16,7 +15,6 @@ import eu.kanade.tachiyomi.util.system.AuthenticatorUtil
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.isAuthenticationSupported
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil.startAuthentication
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.launchIn
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsSecurityController : SettingsController() {
@ -79,8 +77,7 @@ class SettingsSecurityController : SettingsController() {
false
}
preferences.useAuthenticator().asImmediateFlow { isVisible = it }
.launchIn(viewScope)
visibleIf(preferences.useAuthenticator()) { it }
}
}