From fd7c993b0bfb897ea0770c91e4594367ae9c2316 Mon Sep 17 00:00:00 2001 From: arkon Date: Tue, 21 Feb 2023 16:50:21 -0500 Subject: [PATCH] Move CheckboxState to core module --- .../eu/kanade/core/prefs/CheckboxState.kt | 55 ++----------------- .../category/ChangeCategoryDialog.kt | 3 +- .../library/DeleteLibraryMangaDialog.kt | 2 +- .../more/stats/StatsScreenContent.kt | 2 +- .../util/DurationUtils.kt | 2 +- .../source/browse/BrowseSourceScreenModel.kt | 4 +- .../ui/library/LibraryScreenModel.kt | 2 +- .../tachiyomi/ui/manga/MangaScreenModel.kt | 4 +- .../core/preference/CheckboxState.kt | 47 ++++++++++++++++ 9 files changed, 63 insertions(+), 58 deletions(-) rename app/src/main/java/eu/kanade/{core => presentation}/util/DurationUtils.kt (95%) create mode 100644 core/src/main/java/tachiyomi/core/preference/CheckboxState.kt diff --git a/app/src/main/java/eu/kanade/core/prefs/CheckboxState.kt b/app/src/main/java/eu/kanade/core/prefs/CheckboxState.kt index 64e1a5606..c3b54ecab 100644 --- a/app/src/main/java/eu/kanade/core/prefs/CheckboxState.kt +++ b/app/src/main/java/eu/kanade/core/prefs/CheckboxState.kt @@ -1,55 +1,12 @@ package eu.kanade.core.prefs import androidx.compose.ui.state.ToggleableState +import tachiyomi.core.preference.CheckboxState -sealed class CheckboxState(open val value: T) { - abstract fun next(): CheckboxState - - sealed class State(override val value: T) : CheckboxState(value) { - data class Checked(override val value: T) : State(value) - data class None(override val value: T) : State(value) - - val isChecked: Boolean - get() = this is Checked - - override fun next(): CheckboxState { - return when (this) { - is Checked -> None(value) - is None -> Checked(value) - } - } - } - sealed class TriState(override val value: T) : CheckboxState(value) { - data class Include(override val value: T) : TriState(value) - data class Exclude(override val value: T) : TriState(value) - data class None(override val value: T) : TriState(value) - - override fun next(): CheckboxState { - return when (this) { - is Exclude -> None(value) - is Include -> Exclude(value) - is None -> Include(value) - } - } - - fun asState(): ToggleableState { - return when (this) { - is Exclude -> ToggleableState.Indeterminate - is Include -> ToggleableState.On - is None -> ToggleableState.Off - } - } +fun CheckboxState.TriState.asState(): ToggleableState { + return when (this) { + is CheckboxState.TriState.Exclude -> ToggleableState.Indeterminate + is CheckboxState.TriState.Include -> ToggleableState.On + is CheckboxState.TriState.None -> ToggleableState.Off } } - -inline fun T.asCheckboxState(condition: (T) -> Boolean): CheckboxState.State { - return if (condition(this)) { - CheckboxState.State.Checked(this) - } else { - CheckboxState.State.None(this) - } -} - -inline fun List.mapAsCheckboxState(condition: (T) -> Boolean): List> { - return this.map { it.asCheckboxState(condition) } -} diff --git a/app/src/main/java/eu/kanade/presentation/category/ChangeCategoryDialog.kt b/app/src/main/java/eu/kanade/presentation/category/ChangeCategoryDialog.kt index ba59308a7..91b75cf4b 100644 --- a/app/src/main/java/eu/kanade/presentation/category/ChangeCategoryDialog.kt +++ b/app/src/main/java/eu/kanade/presentation/category/ChangeCategoryDialog.kt @@ -21,8 +21,9 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource -import eu.kanade.core.prefs.CheckboxState +import eu.kanade.core.prefs.asState import eu.kanade.tachiyomi.R +import tachiyomi.core.preference.CheckboxState import tachiyomi.domain.category.model.Category import tachiyomi.presentation.core.components.material.TextButton import tachiyomi.presentation.core.components.material.padding diff --git a/app/src/main/java/eu/kanade/presentation/library/DeleteLibraryMangaDialog.kt b/app/src/main/java/eu/kanade/presentation/library/DeleteLibraryMangaDialog.kt index 2cde52c0c..b20c266ce 100644 --- a/app/src/main/java/eu/kanade/presentation/library/DeleteLibraryMangaDialog.kt +++ b/app/src/main/java/eu/kanade/presentation/library/DeleteLibraryMangaDialog.kt @@ -16,8 +16,8 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource -import eu.kanade.core.prefs.CheckboxState import eu.kanade.tachiyomi.R +import tachiyomi.core.preference.CheckboxState @Composable fun DeleteLibraryMangaDialog( diff --git a/app/src/main/java/eu/kanade/presentation/more/stats/StatsScreenContent.kt b/app/src/main/java/eu/kanade/presentation/more/stats/StatsScreenContent.kt index c9c5f497b..27a855d34 100644 --- a/app/src/main/java/eu/kanade/presentation/more/stats/StatsScreenContent.kt +++ b/app/src/main/java/eu/kanade/presentation/more/stats/StatsScreenContent.kt @@ -13,11 +13,11 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource -import eu.kanade.core.util.toDurationString import eu.kanade.presentation.more.stats.components.StatsItem import eu.kanade.presentation.more.stats.components.StatsOverviewItem import eu.kanade.presentation.more.stats.components.StatsSection import eu.kanade.presentation.more.stats.data.StatsData +import eu.kanade.presentation.util.toDurationString import eu.kanade.tachiyomi.R import tachiyomi.presentation.core.components.LazyColumn import tachiyomi.presentation.core.components.material.padding diff --git a/app/src/main/java/eu/kanade/core/util/DurationUtils.kt b/app/src/main/java/eu/kanade/presentation/util/DurationUtils.kt similarity index 95% rename from app/src/main/java/eu/kanade/core/util/DurationUtils.kt rename to app/src/main/java/eu/kanade/presentation/util/DurationUtils.kt index 3eec1a0dc..644f5ca13 100644 --- a/app/src/main/java/eu/kanade/core/util/DurationUtils.kt +++ b/app/src/main/java/eu/kanade/presentation/util/DurationUtils.kt @@ -1,4 +1,4 @@ -package eu.kanade.core.util +package eu.kanade.presentation.util import android.content.Context import eu.kanade.tachiyomi.R diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt index 1e9a3a8da..e9e6e6f51 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt @@ -15,9 +15,7 @@ import androidx.paging.map import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.coroutineScope import eu.davidea.flexibleadapter.items.IFlexible -import eu.kanade.core.prefs.CheckboxState import eu.kanade.core.prefs.asState -import eu.kanade.core.prefs.mapAsCheckboxState import eu.kanade.domain.chapter.interactor.SetMangaDefaultChapterFlags import eu.kanade.domain.chapter.interactor.SyncChaptersWithTrackServiceTwoWay import eu.kanade.domain.library.service.LibraryPreferences @@ -60,6 +58,8 @@ import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import logcat.LogPriority +import tachiyomi.core.preference.CheckboxState +import tachiyomi.core.preference.mapAsCheckboxState import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.withIOContext import tachiyomi.core.util.lang.withNonCancellableContext diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt index e6724463b..f1436846a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryScreenModel.kt @@ -7,7 +7,6 @@ import androidx.compose.ui.util.fastAny import androidx.compose.ui.util.fastMap import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.coroutineScope -import eu.kanade.core.prefs.CheckboxState import eu.kanade.core.prefs.PreferenceMutableState import eu.kanade.core.prefs.asState import eu.kanade.core.util.fastDistinctBy @@ -45,6 +44,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.update +import tachiyomi.core.preference.CheckboxState import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchNonCancellable import tachiyomi.core.util.lang.withIOContext diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt index bdecb22bb..8eb44ef34 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaScreenModel.kt @@ -8,9 +8,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import cafe.adriel.voyager.core.model.StateScreenModel import cafe.adriel.voyager.core.model.coroutineScope -import eu.kanade.core.prefs.CheckboxState import eu.kanade.core.prefs.asState -import eu.kanade.core.prefs.mapAsCheckboxState import eu.kanade.core.util.addOrRemove import eu.kanade.domain.chapter.interactor.SetMangaDefaultChapterFlags import eu.kanade.domain.chapter.interactor.SetReadStatus @@ -53,6 +51,8 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import logcat.LogPriority +import tachiyomi.core.preference.CheckboxState +import tachiyomi.core.preference.mapAsCheckboxState import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchNonCancellable import tachiyomi.core.util.lang.withIOContext diff --git a/core/src/main/java/tachiyomi/core/preference/CheckboxState.kt b/core/src/main/java/tachiyomi/core/preference/CheckboxState.kt new file mode 100644 index 000000000..da681e16d --- /dev/null +++ b/core/src/main/java/tachiyomi/core/preference/CheckboxState.kt @@ -0,0 +1,47 @@ +package tachiyomi.core.preference + +sealed class CheckboxState(open val value: T) { + + abstract fun next(): CheckboxState + + sealed class State(override val value: T) : CheckboxState(value) { + data class Checked(override val value: T) : State(value) + data class None(override val value: T) : State(value) + + val isChecked: Boolean + get() = this is Checked + + override fun next(): CheckboxState { + return when (this) { + is Checked -> None(value) + is None -> Checked(value) + } + } + } + + sealed class TriState(override val value: T) : CheckboxState(value) { + data class Include(override val value: T) : TriState(value) + data class Exclude(override val value: T) : TriState(value) + data class None(override val value: T) : TriState(value) + + override fun next(): CheckboxState { + return when (this) { + is Exclude -> None(value) + is Include -> Exclude(value) + is None -> Include(value) + } + } + } +} + +inline fun T.asCheckboxState(condition: (T) -> Boolean): CheckboxState.State { + return if (condition(this)) { + CheckboxState.State.Checked(this) + } else { + CheckboxState.State.None(this) + } +} + +inline fun List.mapAsCheckboxState(condition: (T) -> Boolean): List> { + return this.map { it.asCheckboxState(condition) } +}