From bd47eafeecc837f833a0c370372dd6d5edb62130 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 23 Feb 2023 13:54:08 -0500 Subject: [PATCH] Fix per-category sort/display affecting the wrong category --- .../presentation/components/SettingsItems.kt | 9 ++++- .../library/LibrarySettingsDialog.kt | 10 +----- .../ui/library/LibrarySettingsScreenModel.kt | 34 ++----------------- .../kanade/tachiyomi/ui/library/LibraryTab.kt | 2 +- .../eu/kanade/tachiyomi/widget/TriState.kt | 15 ++++++++ .../domain/manga/model/TriStateFilter.kt | 9 +++++ 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/components/SettingsItems.kt b/app/src/main/java/eu/kanade/presentation/components/SettingsItems.kt index f670acf54b..0db51ef4c5 100644 --- a/app/src/main/java/eu/kanade/presentation/components/SettingsItems.kt +++ b/app/src/main/java/eu/kanade/presentation/components/SettingsItems.kt @@ -31,9 +31,16 @@ import tachiyomi.presentation.core.theme.header @Composable fun HeadingItem( @StringRes labelRes: Int, +) { + HeadingItem(stringResource(labelRes)) +} + +@Composable +fun HeadingItem( + text: String, ) { Text( - text = stringResource(labelRes), + text = text, style = MaterialTheme.typography.header, modifier = Modifier .fillMaxWidth() diff --git a/app/src/main/java/eu/kanade/presentation/library/LibrarySettingsDialog.kt b/app/src/main/java/eu/kanade/presentation/library/LibrarySettingsDialog.kt index afa6ccad66..86acece98d 100644 --- a/app/src/main/java/eu/kanade/presentation/library/LibrarySettingsDialog.kt +++ b/app/src/main/java/eu/kanade/presentation/library/LibrarySettingsDialog.kt @@ -6,10 +6,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue -import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import eu.kanade.domain.library.service.LibraryPreferences @@ -35,13 +32,8 @@ import tachiyomi.domain.manga.model.TriStateFilter fun LibrarySettingsDialog( onDismissRequest: () -> Unit, screenModel: LibrarySettingsScreenModel, - activeCategoryIndex: Int, + category: Category, ) { - val state by screenModel.state.collectAsState() - val category by remember(activeCategoryIndex) { - derivedStateOf { state.categories[activeCategoryIndex] } - } - TabbedDialog( onDismissRequest = onDismissRequest, tabTitles = listOf( diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsScreenModel.kt index 6cb681f89c..fae076b773 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySettingsScreenModel.kt @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.ui.library -import androidx.compose.runtime.Immutable -import cafe.adriel.voyager.core.model.StateScreenModel +import cafe.adriel.voyager.core.model.ScreenModel import cafe.adriel.voyager.core.model.coroutineScope import eu.kanade.domain.base.BasePreferences import eu.kanade.domain.category.interactor.SetDisplayModeForCategory @@ -10,12 +9,9 @@ import eu.kanade.domain.library.service.LibraryPreferences import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.util.preference.toggle import eu.kanade.tachiyomi.widget.TriState -import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.update import tachiyomi.core.preference.Preference import tachiyomi.core.preference.getAndSet import tachiyomi.core.util.lang.launchIO -import tachiyomi.domain.category.interactor.GetCategories import tachiyomi.domain.category.model.Category import tachiyomi.domain.library.model.LibraryDisplayMode import tachiyomi.domain.library.model.LibrarySort @@ -25,39 +21,20 @@ import uy.kohesive.injekt.api.get class LibrarySettingsScreenModel( val preferences: BasePreferences = Injekt.get(), val libraryPreferences: LibraryPreferences = Injekt.get(), - private val getCategories: GetCategories = Injekt.get(), private val setDisplayModeForCategory: SetDisplayModeForCategory = Injekt.get(), private val setSortModeForCategory: SetSortModeForCategory = Injekt.get(), trackManager: TrackManager = Injekt.get(), -) : StateScreenModel(State()) { +) : ScreenModel { val trackServices = trackManager.services.filter { service -> service.isLogged } - init { - coroutineScope.launchIO { - getCategories.subscribe() - .collectLatest { - mutableState.update { state -> - state.copy( - categories = it, - ) - } - } - } - } - fun togglePreference(preference: (LibraryPreferences) -> Preference) { preference(libraryPreferences).toggle() } fun toggleFilter(preference: (LibraryPreferences) -> Preference) { preference(libraryPreferences).getAndSet { - when (it) { - TriState.DISABLED.value -> TriState.ENABLED_IS.value - TriState.ENABLED_IS.value -> TriState.ENABLED_NOT.value - TriState.ENABLED_NOT.value -> TriState.DISABLED.value - else -> throw IllegalStateException("Unknown TriStateGroup state: $this") - } + TriState.valueOf(it).next().value } } @@ -76,9 +53,4 @@ class LibrarySettingsScreenModel( setSortModeForCategory.await(category, mode, direction) } } - - @Immutable - data class State( - val categories: List = emptyList(), - ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt index d579de51b0..63a8bfee2f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt @@ -203,7 +203,7 @@ object LibraryTab : Tab { is LibraryScreenModel.Dialog.SettingsSheet -> LibrarySettingsDialog( onDismissRequest = onDismissRequest, screenModel = settingsScreenModel, - activeCategoryIndex = screenModel.activeCategoryIndex, + category = state.categories[screenModel.activeCategoryIndex], ) is LibraryScreenModel.Dialog.ChangeCategory -> { ChangeCategoryDialog( diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/TriState.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/TriState.kt index 0b25a1dbdf..106755d240 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/TriState.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/TriState.kt @@ -7,6 +7,21 @@ enum class TriState(val value: Int) { DISABLED(0), ENABLED_IS(1), ENABLED_NOT(2), + ; + + fun next(): TriState { + return when (this) { + DISABLED -> ENABLED_IS + ENABLED_IS -> ENABLED_NOT + ENABLED_NOT -> DISABLED + } + } + + companion object { + fun valueOf(value: Int): TriState { + return TriState.values().first { it.value == value } + } + } } fun Int.toTriStateFilter(): TriStateFilter { diff --git a/domain/src/main/java/tachiyomi/domain/manga/model/TriStateFilter.kt b/domain/src/main/java/tachiyomi/domain/manga/model/TriStateFilter.kt index b890cafad3..86ed7a5c00 100644 --- a/domain/src/main/java/tachiyomi/domain/manga/model/TriStateFilter.kt +++ b/domain/src/main/java/tachiyomi/domain/manga/model/TriStateFilter.kt @@ -4,4 +4,13 @@ enum class TriStateFilter { DISABLED, // Disable filter ENABLED_IS, // Enabled with "is" filter ENABLED_NOT, // Enabled with "not" filter + ; + + fun next(): TriStateFilter { + return when (this) { + DISABLED -> ENABLED_IS + ENABLED_IS -> ENABLED_NOT + ENABLED_NOT -> DISABLED + } + } }