From dd1923fe88bbb1e7f838d560806598a4ba2174f8 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 30 Oct 2022 11:37:02 -0400 Subject: [PATCH] Remove redundant preference composables --- .../browse/ExtensionDetailsScreen.kt | 15 +- .../browse/ExtensionFilterScreen.kt | 47 ++--- .../browse/SourcesFilterScreen.kt | 11 +- .../kanade/presentation/components/Divider.kt | 17 ++ .../presentation/components/Preferences.kt | 167 ------------------ .../settings/widget/BasePreferenceWidget.kt | 67 ++++--- .../settings/widget/SwitchPreferenceWidget.kt | 2 + .../extension/ExtensionFilterPresenter.kt | 2 - .../browse/source/SourcesFilterPresenter.kt | 2 - 9 files changed, 80 insertions(+), 250 deletions(-) create mode 100644 app/src/main/java/eu/kanade/presentation/components/Divider.kt delete mode 100644 app/src/main/java/eu/kanade/presentation/components/Preferences.kt diff --git a/app/src/main/java/eu/kanade/presentation/browse/ExtensionDetailsScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/ExtensionDetailsScreen.kt index 7c528092a0..2c8224dea7 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/ExtensionDetailsScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/ExtensionDetailsScreen.kt @@ -54,9 +54,10 @@ import eu.kanade.presentation.components.DIVIDER_ALPHA import eu.kanade.presentation.components.Divider import eu.kanade.presentation.components.EmptyScreen import eu.kanade.presentation.components.LoadingScreen -import eu.kanade.presentation.components.PreferenceRow import eu.kanade.presentation.components.Scaffold import eu.kanade.presentation.components.ScrollbarLazyColumn +import eu.kanade.presentation.more.settings.widget.TextPreferenceWidget +import eu.kanade.presentation.more.settings.widget.TrailingWidgetBuffer import eu.kanade.presentation.util.horizontalPadding import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.extension.model.Extension @@ -376,15 +377,14 @@ private fun SourceSwitchPreference( ) { val context = LocalContext.current - PreferenceRow( + TextPreferenceWidget( modifier = modifier, title = if (source.labelAsName) { source.source.toString() } else { LocaleHelper.getSourceDisplayName(source.source.lang, context) }, - onClick = { onClickSource(source.source.id) }, - action = { + widget = { Row( verticalAlignment = Alignment.CenterVertically, ) { @@ -398,9 +398,14 @@ private fun SourceSwitchPreference( } } - Switch(checked = source.enabled, onCheckedChange = null) + Switch( + checked = source.enabled, + onCheckedChange = null, + modifier = Modifier.padding(start = TrailingWidgetBuffer), + ) } }, + onPreferenceClick = { onClickSource(source.source.id) }, ) } diff --git a/app/src/main/java/eu/kanade/presentation/browse/ExtensionFilterScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/ExtensionFilterScreen.kt index 400276a2bf..3893cfc674 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/ExtensionFilterScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/ExtensionFilterScreen.kt @@ -3,7 +3,6 @@ package eu.kanade.presentation.browse import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.items -import androidx.compose.material3.Switch import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Modifier @@ -13,8 +12,8 @@ import eu.kanade.presentation.components.AppBar import eu.kanade.presentation.components.EmptyScreen import eu.kanade.presentation.components.FastScrollLazyColumn import eu.kanade.presentation.components.LoadingScreen -import eu.kanade.presentation.components.PreferenceRow import eu.kanade.presentation.components.Scaffold +import eu.kanade.presentation.more.settings.widget.SwitchPreferenceWidget import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.browse.extension.ExtensionFilterPresenter import eu.kanade.tachiyomi.util.system.LocaleHelper @@ -42,15 +41,13 @@ fun ExtensionFilterScreen( textResource = R.string.empty_screen, modifier = Modifier.padding(contentPadding), ) - else -> { - SourceFilterContent( - contentPadding = contentPadding, - state = presenter, - onClickLang = { - presenter.toggleLanguage(it) - }, - ) - } + else -> ExtensionFilterContent( + contentPadding = contentPadding, + state = presenter, + onClickLang = { + presenter.toggleLanguage(it) + }, + ) } } LaunchedEffect(Unit) { @@ -65,7 +62,7 @@ fun ExtensionFilterScreen( } @Composable -private fun SourceFilterContent( +private fun ExtensionFilterContent( contentPadding: PaddingValues, state: ExtensionFilterState, onClickLang: (String) -> Unit, @@ -76,29 +73,13 @@ private fun SourceFilterContent( items( items = state.items, ) { model -> - ExtensionFilterItem( + val lang = model.lang + SwitchPreferenceWidget( modifier = Modifier.animateItemPlacement(), - lang = model.lang, - enabled = model.enabled, - onClickItem = onClickLang, + title = LocaleHelper.getSourceDisplayName(lang, LocalContext.current), + checked = model.enabled, + onCheckedChanged = { onClickLang(lang) }, ) } } } - -@Composable -private fun ExtensionFilterItem( - modifier: Modifier, - lang: String, - enabled: Boolean, - onClickItem: (String) -> Unit, -) { - PreferenceRow( - modifier = modifier, - title = LocaleHelper.getSourceDisplayName(lang, LocalContext.current), - action = { - Switch(checked = enabled, onCheckedChange = null) - }, - onClick = { onClickItem(lang) }, - ) -} diff --git a/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt b/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt index 9ae0c52a26..c9d7c4d8b1 100644 --- a/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/browse/SourcesFilterScreen.kt @@ -4,7 +4,6 @@ import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.items import androidx.compose.material3.Checkbox -import androidx.compose.material3.Switch import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Modifier @@ -16,8 +15,8 @@ import eu.kanade.presentation.components.AppBar import eu.kanade.presentation.components.EmptyScreen import eu.kanade.presentation.components.FastScrollLazyColumn import eu.kanade.presentation.components.LoadingScreen -import eu.kanade.presentation.components.PreferenceRow import eu.kanade.presentation.components.Scaffold +import eu.kanade.presentation.more.settings.widget.SwitchPreferenceWidget import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.browse.source.FilterUiModel import eu.kanade.tachiyomi.ui.browse.source.SourcesFilterPresenter @@ -119,13 +118,11 @@ private fun SourcesFilterHeader( enabled: Boolean, onClickItem: (String) -> Unit, ) { - PreferenceRow( + SwitchPreferenceWidget( modifier = modifier, title = LocaleHelper.getSourceDisplayName(language, LocalContext.current), - action = { - Switch(checked = enabled, onCheckedChange = null) - }, - onClick = { onClickItem(language) }, + checked = enabled, + onCheckedChanged = { onClickItem(language) }, ) } diff --git a/app/src/main/java/eu/kanade/presentation/components/Divider.kt b/app/src/main/java/eu/kanade/presentation/components/Divider.kt new file mode 100644 index 0000000000..d59f8e83f2 --- /dev/null +++ b/app/src/main/java/eu/kanade/presentation/components/Divider.kt @@ -0,0 +1,17 @@ +package eu.kanade.presentation.components + +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier + +const val DIVIDER_ALPHA = 0.2f + +@Composable +fun Divider( + modifier: Modifier = Modifier, +) { + androidx.compose.material3.Divider( + modifier = modifier, + color = MaterialTheme.colorScheme.onSurface.copy(alpha = DIVIDER_ALPHA), + ) +} diff --git a/app/src/main/java/eu/kanade/presentation/components/Preferences.kt b/app/src/main/java/eu/kanade/presentation/components/Preferences.kt deleted file mode 100644 index 6fa0bff6ad..0000000000 --- a/app/src/main/java/eu/kanade/presentation/components/Preferences.kt +++ /dev/null @@ -1,167 +0,0 @@ -package eu.kanade.presentation.components - -import androidx.compose.foundation.combinedClickable -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.heightIn -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.widthIn -import androidx.compose.material3.Icon -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Switch -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.painter.Painter -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import eu.kanade.core.prefs.PreferenceMutableState -import eu.kanade.presentation.theme.TachiyomiTheme -import eu.kanade.presentation.util.horizontalPadding - -const val DIVIDER_ALPHA = 0.2f - -@Composable -fun Divider( - modifier: Modifier = Modifier, -) { - androidx.compose.material3.Divider( - modifier = modifier, - color = MaterialTheme.colorScheme.onSurface.copy(alpha = DIVIDER_ALPHA), - ) -} - -@Composable -fun PreferenceRow( - modifier: Modifier = Modifier, - title: String, - painter: Painter? = null, - onClick: () -> Unit = {}, - onLongClick: () -> Unit = {}, - subtitle: String? = null, - action: @Composable (() -> Unit)? = null, -) { - val height = if (subtitle != null) 72.dp else 56.dp - - val titleTextStyle = MaterialTheme.typography.bodyLarge - val subtitleTextStyle = MaterialTheme.typography.bodyMedium.copy( - color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.75f), - ) - - Row( - modifier = modifier - .fillMaxWidth() - .heightIn(min = height) - .combinedClickable( - onLongClick = onLongClick, - onClick = onClick, - ), - verticalAlignment = Alignment.CenterVertically, - ) { - if (painter != null) { - Icon( - painter = painter, - modifier = Modifier - .padding(start = horizontalPadding, end = 16.dp) - .size(24.dp), - tint = MaterialTheme.colorScheme.primary, - contentDescription = null, - ) - } - Column( - Modifier - .padding(horizontal = 16.dp) - .weight(1f), - ) { - Text( - text = title, - style = titleTextStyle, - ) - if (subtitle != null) { - Text( - modifier = Modifier.padding(top = 4.dp), - text = subtitle, - style = subtitleTextStyle, - ) - } - } - if (action != null) { - Box( - Modifier - .widthIn(min = 56.dp) - .padding(end = horizontalPadding), - ) { - action() - } - } - } -} - -@Composable -fun SwitchPreference( - modifier: Modifier = Modifier, - checked: Boolean, - onClick: () -> Unit, - title: String, - subtitle: String? = null, - painter: Painter? = null, -) { - PreferenceRow( - modifier = modifier, - title = title, - subtitle = subtitle, - painter = painter, - action = { Switch(checked = checked, onCheckedChange = null) }, - onClick = onClick, - ) -} - -@Composable -fun SwitchPreference( - modifier: Modifier = Modifier, - preference: PreferenceMutableState, - title: String, - subtitle: String? = null, - painter: Painter? = null, -) { - SwitchPreference( - modifier = modifier, - title = title, - subtitle = subtitle, - painter = painter, - checked = preference.value, - onClick = { preference.value = !preference.value }, - ) -} - -@Preview -@Composable -private fun PreferencesPreview() { - TachiyomiTheme { - Column { - PreferenceRow( - title = "Plain", - subtitle = "Subtitle", - ) - - Divider() - - SwitchPreference( - title = "Switch (on)", - subtitle = "Subtitle", - checked = true, - onClick = {}, - ) - SwitchPreference( - title = "Switch (off)", - subtitle = "Subtitle", - checked = false, - onClick = {}, - ) - } - } -} diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/widget/BasePreferenceWidget.kt b/app/src/main/java/eu/kanade/presentation/more/settings/widget/BasePreferenceWidget.kt index b7ffe63c37..267c03e3e0 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/widget/BasePreferenceWidget.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/widget/BasePreferenceWidget.kt @@ -44,43 +44,42 @@ internal fun BasePreferenceWidget( widget: @Composable (() -> Unit)? = null, ) { val highlighted = LocalPreferenceHighlighted.current - Box(modifier = Modifier.highlightBackground(highlighted)) { - Row( - modifier = modifier - .sizeIn(minHeight = 56.dp) - .clickable(enabled = onClick != null, onClick = { onClick?.invoke() }) - .fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically, + Row( + modifier = modifier + .highlightBackground(highlighted) + .sizeIn(minHeight = 56.dp) + .clickable(enabled = onClick != null, onClick = { onClick?.invoke() }) + .fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + ) { + if (icon != null) { + Box( + modifier = Modifier.padding(start = PrefsHorizontalPadding, end = 8.dp), + content = { icon() }, + ) + } + Column( + modifier = Modifier + .weight(1f) + .padding(vertical = PrefsVerticalPadding), ) { - if (icon != null) { - Box( - modifier = Modifier.padding(start = PrefsHorizontalPadding, end = 8.dp), - content = { icon() }, - ) - } - Column( - modifier = Modifier - .weight(1f) - .padding(vertical = PrefsVerticalPadding), - ) { - if (!title.isNullOrBlank()) { - Text( - modifier = Modifier.padding(horizontal = PrefsHorizontalPadding), - text = title, - overflow = TextOverflow.Ellipsis, - maxLines = 2, - style = MaterialTheme.typography.titleLarge, - fontSize = TitleFontSize, - ) - } - subcomponent?.invoke(this) - } - if (widget != null) { - Box( - modifier = Modifier.padding(end = PrefsHorizontalPadding), - content = { widget() }, + if (!title.isNullOrBlank()) { + Text( + modifier = Modifier.padding(horizontal = PrefsHorizontalPadding), + text = title, + overflow = TextOverflow.Ellipsis, + maxLines = 2, + style = MaterialTheme.typography.titleLarge, + fontSize = TitleFontSize, ) } + subcomponent?.invoke(this) + } + if (widget != null) { + Box( + modifier = Modifier.padding(end = PrefsHorizontalPadding), + content = { widget() }, + ) } } } diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/widget/SwitchPreferenceWidget.kt b/app/src/main/java/eu/kanade/presentation/more/settings/widget/SwitchPreferenceWidget.kt index 83ed78d0da..afbe0f9ffd 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/widget/SwitchPreferenceWidget.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/widget/SwitchPreferenceWidget.kt @@ -14,6 +14,7 @@ import androidx.compose.ui.tooling.preview.Preview @Composable fun SwitchPreferenceWidget( + modifier: Modifier = Modifier, title: String, subtitle: String? = null, icon: ImageVector? = null, @@ -21,6 +22,7 @@ fun SwitchPreferenceWidget( onCheckedChanged: (Boolean) -> Unit, ) { TextPreferenceWidget( + modifier = modifier, title = title, subtitle = subtitle, icon = icon, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterPresenter.kt index 3328ec0625..41f132a29d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionFilterPresenter.kt @@ -13,7 +13,6 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.receiveAsFlow -import kotlinx.coroutines.flow.stateIn import logcat.LogPriority import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -36,7 +35,6 @@ class ExtensionFilterPresenter( logcat(LogPriority.ERROR, exception) _events.send(Event.FailedFetchingLanguages) } - .stateIn(presenterScope) .collectLatest(::collectLatestSourceLangMap) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterPresenter.kt index c80d331d91..5cf6e4a5a5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcesFilterPresenter.kt @@ -15,7 +15,6 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.receiveAsFlow -import kotlinx.coroutines.flow.stateIn import logcat.LogPriority import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -40,7 +39,6 @@ class SourcesFilterPresenter( logcat(LogPriority.ERROR, exception) _events.send(Event.FailedFetchingLanguages) } - .stateIn(presenterScope) .collectLatest(::collectLatestSourceLangMap) } }