Fix per-category sort/display affecting the wrong category

This commit is contained in:
arkon 2023-02-23 13:54:08 -05:00
parent 9432d2d06a
commit bd47eafeec
6 changed files with 37 additions and 42 deletions

View File

@ -31,9 +31,16 @@ import tachiyomi.presentation.core.theme.header
@Composable @Composable
fun HeadingItem( fun HeadingItem(
@StringRes labelRes: Int, @StringRes labelRes: Int,
) {
HeadingItem(stringResource(labelRes))
}
@Composable
fun HeadingItem(
text: String,
) { ) {
Text( Text(
text = stringResource(labelRes), text = text,
style = MaterialTheme.typography.header, style = MaterialTheme.typography.header,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -6,10 +6,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import eu.kanade.domain.library.service.LibraryPreferences import eu.kanade.domain.library.service.LibraryPreferences
@ -35,13 +32,8 @@ import tachiyomi.domain.manga.model.TriStateFilter
fun LibrarySettingsDialog( fun LibrarySettingsDialog(
onDismissRequest: () -> Unit, onDismissRequest: () -> Unit,
screenModel: LibrarySettingsScreenModel, screenModel: LibrarySettingsScreenModel,
activeCategoryIndex: Int, category: Category,
) { ) {
val state by screenModel.state.collectAsState()
val category by remember(activeCategoryIndex) {
derivedStateOf { state.categories[activeCategoryIndex] }
}
TabbedDialog( TabbedDialog(
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,
tabTitles = listOf( tabTitles = listOf(

View File

@ -1,7 +1,6 @@
package eu.kanade.tachiyomi.ui.library package eu.kanade.tachiyomi.ui.library
import androidx.compose.runtime.Immutable import cafe.adriel.voyager.core.model.ScreenModel
import cafe.adriel.voyager.core.model.StateScreenModel
import cafe.adriel.voyager.core.model.coroutineScope import cafe.adriel.voyager.core.model.coroutineScope
import eu.kanade.domain.base.BasePreferences import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.category.interactor.SetDisplayModeForCategory 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.data.track.TrackManager
import eu.kanade.tachiyomi.util.preference.toggle import eu.kanade.tachiyomi.util.preference.toggle
import eu.kanade.tachiyomi.widget.TriState 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.Preference
import tachiyomi.core.preference.getAndSet import tachiyomi.core.preference.getAndSet
import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchIO
import tachiyomi.domain.category.interactor.GetCategories
import tachiyomi.domain.category.model.Category import tachiyomi.domain.category.model.Category
import tachiyomi.domain.library.model.LibraryDisplayMode import tachiyomi.domain.library.model.LibraryDisplayMode
import tachiyomi.domain.library.model.LibrarySort import tachiyomi.domain.library.model.LibrarySort
@ -25,39 +21,20 @@ import uy.kohesive.injekt.api.get
class LibrarySettingsScreenModel( class LibrarySettingsScreenModel(
val preferences: BasePreferences = Injekt.get(), val preferences: BasePreferences = Injekt.get(),
val libraryPreferences: LibraryPreferences = Injekt.get(), val libraryPreferences: LibraryPreferences = Injekt.get(),
private val getCategories: GetCategories = Injekt.get(),
private val setDisplayModeForCategory: SetDisplayModeForCategory = Injekt.get(), private val setDisplayModeForCategory: SetDisplayModeForCategory = Injekt.get(),
private val setSortModeForCategory: SetSortModeForCategory = Injekt.get(), private val setSortModeForCategory: SetSortModeForCategory = Injekt.get(),
trackManager: TrackManager = Injekt.get(), trackManager: TrackManager = Injekt.get(),
) : StateScreenModel<LibrarySettingsScreenModel.State>(State()) { ) : ScreenModel {
val trackServices = trackManager.services.filter { service -> service.isLogged } 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<Boolean>) { fun togglePreference(preference: (LibraryPreferences) -> Preference<Boolean>) {
preference(libraryPreferences).toggle() preference(libraryPreferences).toggle()
} }
fun toggleFilter(preference: (LibraryPreferences) -> Preference<Int>) { fun toggleFilter(preference: (LibraryPreferences) -> Preference<Int>) {
preference(libraryPreferences).getAndSet { preference(libraryPreferences).getAndSet {
when (it) { TriState.valueOf(it).next().value
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")
}
} }
} }
@ -76,9 +53,4 @@ class LibrarySettingsScreenModel(
setSortModeForCategory.await(category, mode, direction) setSortModeForCategory.await(category, mode, direction)
} }
} }
@Immutable
data class State(
val categories: List<Category> = emptyList(),
)
} }

View File

@ -203,7 +203,7 @@ object LibraryTab : Tab {
is LibraryScreenModel.Dialog.SettingsSheet -> LibrarySettingsDialog( is LibraryScreenModel.Dialog.SettingsSheet -> LibrarySettingsDialog(
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,
screenModel = settingsScreenModel, screenModel = settingsScreenModel,
activeCategoryIndex = screenModel.activeCategoryIndex, category = state.categories[screenModel.activeCategoryIndex],
) )
is LibraryScreenModel.Dialog.ChangeCategory -> { is LibraryScreenModel.Dialog.ChangeCategory -> {
ChangeCategoryDialog( ChangeCategoryDialog(

View File

@ -7,6 +7,21 @@ enum class TriState(val value: Int) {
DISABLED(0), DISABLED(0),
ENABLED_IS(1), ENABLED_IS(1),
ENABLED_NOT(2), 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 { fun Int.toTriStateFilter(): TriStateFilter {

View File

@ -4,4 +4,13 @@ enum class TriStateFilter {
DISABLED, // Disable filter DISABLED, // Disable filter
ENABLED_IS, // Enabled with "is" filter ENABLED_IS, // Enabled with "is" filter
ENABLED_NOT, // Enabled with "not" filter ENABLED_NOT, // Enabled with "not" filter
;
fun next(): TriStateFilter {
return when (this) {
DISABLED -> ENABLED_IS
ENABLED_IS -> ENABLED_NOT
ENABLED_NOT -> DISABLED
}
}
} }