From 945afc71ef7aa753ec09640cbf9859ff98770c5a Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 17 Dec 2021 10:11:07 -0500 Subject: [PATCH] Refactor dependant preference visibility flows --- .../setting/SettingsAppearanceController.kt | 5 +---- .../ui/setting/SettingsBackupController.kt | 7 ++----- .../ui/setting/SettingsController.kt | 8 ++++++++ .../ui/setting/SettingsDownloadController.kt | 4 +--- .../ui/setting/SettingsLibraryController.kt | 7 ++----- .../ui/setting/SettingsReaderController.kt | 20 +++++++++---------- .../ui/setting/SettingsSecurityController.kt | 5 +---- 7 files changed, 24 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAppearanceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAppearanceController.kt index 1c02ca141..8352e9890 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAppearanceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAppearanceController.kt @@ -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) } 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 e29a5a309..2eeb079ac 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 @@ -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 } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsController.kt index cd30fb776..c670404d9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsController.kt @@ -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 Preference.visibleIf(preference: com.tfcporciuncula.flow.Preference, crossinline block: (T) -> Boolean) { + preference.asImmediateFlow { isVisible = block(it) } + .launchIn(viewScope) + } } 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 8bb4c8031..21f17c0db 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 @@ -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() 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 ff5e76007..41288b2a8 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 @@ -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() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt index cadeb403d..8e43dbadd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsReaderController.kt @@ -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 } } } 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 e0e5e4c39..b71fbb327 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 @@ -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 } } }