From 8ff0c9d61a1d5b1cc08a3615ef4bfa3d1f04a5d2 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 26 Jul 2023 22:57:15 -0400 Subject: [PATCH] Allow more flexible custom preference composables --- .../more/settings/PreferenceItem.kt | 15 +++--------- .../more/settings/PreferenceModel.kt | 24 ++++++++----------- .../screen/SettingsAppearanceScreen.kt | 17 ++++++++++--- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceItem.kt b/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceItem.kt index b7190a4a7c..33a277c94b 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceItem.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceItem.kt @@ -13,8 +13,6 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.structuralEqualityPolicy import androidx.compose.ui.unit.dp import eu.kanade.domain.track.service.TrackPreferences -import eu.kanade.domain.ui.UiPreferences -import eu.kanade.presentation.more.settings.widget.AppThemePreferenceWidget import eu.kanade.presentation.more.settings.widget.EditTextPreferenceWidget import eu.kanade.presentation.more.settings.widget.InfoWidget import eu.kanade.presentation.more.settings.widget.ListPreferenceWidget @@ -158,16 +156,6 @@ internal fun PreferenceItem( }, ) } - is Preference.PreferenceItem.AppThemePreference -> { - val value by item.pref.collectAsState() - val amoled by Injekt.get().themeDarkAmoled().collectAsState() - AppThemePreferenceWidget( - title = item.title, - value = value, - amoled = amoled, - onItemClick = { scope.launch { item.pref.set(it) } }, - ) - } is Preference.PreferenceItem.TrackingPreference -> { val uName by Injekt.get() .getString(TrackPreferences.trackUsername(item.service.id)) @@ -183,6 +171,9 @@ internal fun PreferenceItem( is Preference.PreferenceItem.InfoPreference -> { InfoWidget(text = item.title) } + is Preference.PreferenceItem.CustomPreference -> { + item.content(item) + } } } } diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceModel.kt b/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceModel.kt index 2ba3eb9a01..5e8973626b 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceModel.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/PreferenceModel.kt @@ -4,7 +4,6 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.res.stringResource -import eu.kanade.domain.ui.model.AppTheme import eu.kanade.presentation.more.settings.Preference.PreferenceItem import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.track.TrackService @@ -133,19 +132,6 @@ sealed class Preference { override val onValueChanged: suspend (newValue: String) -> Boolean = { true }, ) : PreferenceItem() - /** - * A [PreferenceItem] that shows previews of [AppTheme] selection. - */ - data class AppThemePreference( - val pref: PreferenceData, - override val title: String, - ) : PreferenceItem() { - override val enabled: Boolean = true - override val subtitle: String? = null - override val icon: ImageVector? = null - override val onValueChanged: suspend (newValue: AppTheme) -> Boolean = { true } - } - /** * A [PreferenceItem] for individual tracking service. */ @@ -169,6 +155,16 @@ sealed class Preference { override val icon: ImageVector? = null override val onValueChanged: suspend (newValue: String) -> Boolean = { true } } + + data class CustomPreference( + override val title: String, + val content: @Composable (PreferenceItem) -> Unit, + ) : PreferenceItem() { + override val enabled: Boolean = true + override val subtitle: String? = null + override val icon: ImageVector? = null + override val onValueChanged: suspend (newValue: String) -> Boolean = { true } + } } data class PreferenceGroup( diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt index 9ae42cd5fb..14c54a2b49 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt @@ -21,6 +21,7 @@ import eu.kanade.domain.ui.model.TabletUiMode import eu.kanade.domain.ui.model.ThemeMode import eu.kanade.domain.ui.model.setAppCompatDelegateThemeMode import eu.kanade.presentation.more.settings.Preference +import eu.kanade.presentation.more.settings.widget.AppThemePreferenceWidget import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.toast @@ -59,8 +60,11 @@ object SettingsAppearanceScreen : SearchableSettings { ): Preference.PreferenceGroup { val themeModePref = uiPreferences.themeMode() val themeMode by themeModePref.collectAsState() + val appThemePref = uiPreferences.appTheme() + val amoledPref = uiPreferences.themeDarkAmoled() + val amoled by amoledPref.collectAsState() LaunchedEffect(themeMode) { setAppCompatDelegateThemeMode(themeMode) @@ -91,10 +95,17 @@ object SettingsAppearanceScreen : SearchableSettings { ) }, ), - Preference.PreferenceItem.AppThemePreference( + Preference.PreferenceItem.CustomPreference( title = stringResource(R.string.pref_app_theme), - pref = appThemePref, - ), + ) { item -> + val value by appThemePref.collectAsState() + AppThemePreferenceWidget( + title = item.title, + value = value, + amoled = amoled, + onItemClick = { appThemePref.set(it) }, + ) + }, Preference.PreferenceItem.SwitchPreference( pref = amoledPref, title = stringResource(R.string.pref_dark_theme_pure_black),