diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e715cae02b..64787068f8 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -22,7 +22,7 @@ android { defaultConfig { applicationId = "eu.kanade.tachiyomi" - versionCode = 98 + versionCode = 99 versionName = "0.14.5" buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"") diff --git a/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt b/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt index 2394696012..ec6fbad0b6 100644 --- a/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt +++ b/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt @@ -4,11 +4,12 @@ import eu.kanade.tachiyomi.data.preference.DEVICE_ONLY_ON_WIFI import eu.kanade.tachiyomi.data.preference.MANGA_HAS_UNREAD import eu.kanade.tachiyomi.data.preference.MANGA_NON_COMPLETED import eu.kanade.tachiyomi.data.preference.MANGA_NON_READ -import eu.kanade.tachiyomi.widget.TriState import tachiyomi.core.preference.PreferenceStore +import tachiyomi.core.preference.getEnum import tachiyomi.domain.library.model.LibraryDisplayMode import tachiyomi.domain.library.model.LibrarySort import tachiyomi.domain.manga.model.Manga +import tachiyomi.domain.manga.model.TriStateFilter class LibraryPreferences( private val preferenceStore: PreferenceStore, @@ -36,17 +37,17 @@ class LibraryPreferences( // region Filter - fun filterDownloaded() = preferenceStore.getInt("pref_filter_library_downloaded", TriState.DISABLED.value) + fun filterDownloaded() = preferenceStore.getEnum("pref_filter_library_downloaded_v2", TriStateFilter.DISABLED) - fun filterUnread() = preferenceStore.getInt("pref_filter_library_unread", TriState.DISABLED.value) + fun filterUnread() = preferenceStore.getEnum("pref_filter_library_unread_v2", TriStateFilter.DISABLED) - fun filterStarted() = preferenceStore.getInt("pref_filter_library_started", TriState.DISABLED.value) + fun filterStarted() = preferenceStore.getEnum("pref_filter_library_started_v2", TriStateFilter.DISABLED) - fun filterBookmarked() = preferenceStore.getInt("pref_filter_library_bookmarked", TriState.DISABLED.value) + fun filterBookmarked() = preferenceStore.getEnum("pref_filter_library_bookmarked_v2", TriStateFilter.DISABLED) - fun filterCompleted() = preferenceStore.getInt("pref_filter_library_completed", TriState.DISABLED.value) + fun filterCompleted() = preferenceStore.getEnum("pref_filter_library_completed_v2", TriStateFilter.DISABLED) - fun filterTracking(name: Int) = preferenceStore.getInt("pref_filter_library_tracked_$name", TriState.DISABLED.value) + fun filterTracking(id: Int) = preferenceStore.getEnum("pref_filter_library_tracked_${id}_v2", TriStateFilter.DISABLED) // endregion 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 04913835e9..b1f0059421 100644 --- a/app/src/main/java/eu/kanade/presentation/library/LibrarySettingsDialog.kt +++ b/app/src/main/java/eu/kanade/presentation/library/LibrarySettingsDialog.kt @@ -16,7 +16,6 @@ import eu.kanade.presentation.components.TriStateItem import eu.kanade.presentation.util.collectAsState import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.library.LibrarySettingsScreenModel -import eu.kanade.tachiyomi.widget.toTriStateFilter import tachiyomi.domain.category.model.Category import tachiyomi.domain.library.model.LibraryDisplayMode import tachiyomi.domain.library.model.LibrarySort @@ -76,7 +75,7 @@ private fun ColumnScope.FilterPage( state = if (downloadedOnly) { TriStateFilter.ENABLED_IS } else { - filterDownloaded.toTriStateFilter() + filterDownloaded }, enabled = !downloadedOnly, onClick = { screenModel.toggleFilter(LibraryPreferences::filterDownloaded) }, @@ -84,25 +83,25 @@ private fun ColumnScope.FilterPage( val filterUnread by screenModel.libraryPreferences.filterUnread().collectAsState() TriStateItem( label = stringResource(R.string.action_filter_unread), - state = filterUnread.toTriStateFilter(), + state = filterUnread, onClick = { screenModel.toggleFilter(LibraryPreferences::filterUnread) }, ) val filterStarted by screenModel.libraryPreferences.filterStarted().collectAsState() TriStateItem( label = stringResource(R.string.label_started), - state = filterStarted.toTriStateFilter(), + state = filterStarted, onClick = { screenModel.toggleFilter(LibraryPreferences::filterStarted) }, ) val filterBookmarked by screenModel.libraryPreferences.filterBookmarked().collectAsState() TriStateItem( label = stringResource(R.string.action_filter_bookmarked), - state = filterBookmarked.toTriStateFilter(), + state = filterBookmarked, onClick = { screenModel.toggleFilter(LibraryPreferences::filterBookmarked) }, ) val filterCompleted by screenModel.libraryPreferences.filterCompleted().collectAsState() TriStateItem( label = stringResource(R.string.completed), - state = filterCompleted.toTriStateFilter(), + state = filterCompleted, onClick = { screenModel.toggleFilter(LibraryPreferences::filterCompleted) }, ) @@ -115,7 +114,7 @@ private fun ColumnScope.FilterPage( val filterTracker by screenModel.libraryPreferences.filterTracking(service.id.toInt()).collectAsState() TriStateItem( label = stringResource(R.string.action_filter_tracked), - state = filterTracker.toTriStateFilter(), + state = filterTracker, onClick = { screenModel.toggleTracker(service.id.toInt()) }, ) } @@ -125,7 +124,7 @@ private fun ColumnScope.FilterPage( val filterTracker by screenModel.libraryPreferences.filterTracking(service.id.toInt()).collectAsState() TriStateItem( label = stringResource(service.nameRes()), - state = filterTracker.toTriStateFilter(), + state = filterTracker, onClick = { screenModel.toggleTracker(service.id.toInt()) }, ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt index dd2e6ee5e2..28208340e8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt @@ -23,8 +23,9 @@ import eu.kanade.tachiyomi.util.preference.minusAssign import eu.kanade.tachiyomi.util.preference.plusAssign import eu.kanade.tachiyomi.util.system.DeviceUtil import eu.kanade.tachiyomi.util.system.toast -import eu.kanade.tachiyomi.widget.TriState import tachiyomi.core.preference.PreferenceStore +import tachiyomi.core.preference.getEnum +import tachiyomi.domain.manga.model.TriStateFilter import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.io.File @@ -47,6 +48,7 @@ object Migrations { libraryPreferences: LibraryPreferences, readerPreferences: ReaderPreferences, backupPreferences: BackupPreferences, + trackManager: TrackManager, ): Boolean { val lastVersionCode = preferenceStore.getInt("last_version_code", 0) val oldVersion = lastVersionCode.get() @@ -114,9 +116,9 @@ object Migrations { fun convertBooleanPrefToTriState(key: String): Int { val oldPrefValue = prefs.getBoolean(key, false) return if (oldPrefValue) { - TriState.ENABLED_IS.value + 1 } else { - TriState.DISABLED.value + 0 } } prefs.edit { @@ -335,6 +337,30 @@ object Migrations { remove("automatic_ext_updates") } } + if (oldVersion < 99) { + val prefKeys = listOf( + "pref_filter_library_downloaded", + "pref_filter_library_unread", + "pref_filter_library_started", + "pref_filter_library_bookmarked", + "pref_filter_library_completed", + ) + trackManager.services.map { "pref_filter_library_tracked_${it.id}" } + + prefKeys.forEach { key -> + val pref = preferenceStore.getInt(key, 0) + prefs.edit { + remove(key) + + val newValue = when (pref.get()) { + 1 -> TriStateFilter.ENABLED_IS + 2 -> TriStateFilter.ENABLED_NOT + else -> TriStateFilter.DISABLED + } + + preferenceStore.getEnum("${key}_v2", TriStateFilter.DISABLED).set(newValue) + } + } + } return true } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterDialog.kt index be5b058b15..fde13802f5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterDialog.kt @@ -20,8 +20,7 @@ import eu.kanade.presentation.components.TriStateItem import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList -import eu.kanade.tachiyomi.widget.TriState -import eu.kanade.tachiyomi.widget.toTriStateFilter +import tachiyomi.domain.manga.model.TriStateFilter import tachiyomi.presentation.core.components.CheckboxItem import tachiyomi.presentation.core.components.CollapsibleBox import tachiyomi.presentation.core.components.HeadingItem @@ -101,7 +100,7 @@ private fun FilterItem(filter: Filter<*>, onUpdate: () -> Unit) { label = filter.name, state = filter.state.toTriStateFilter(), ) { - filter.state = TriState.valueOf(filter.state).next().value + filter.state = filter.state.toTriStateFilter().next().toTriStateInt() onUpdate() } } @@ -163,3 +162,21 @@ private fun FilterItem(filter: Filter<*>, onUpdate: () -> Unit) { } } } + +private fun Int.toTriStateFilter(): TriStateFilter { + return when (this) { + Filter.TriState.STATE_IGNORE -> TriStateFilter.DISABLED + Filter.TriState.STATE_INCLUDE -> TriStateFilter.ENABLED_IS + Filter.TriState.STATE_EXCLUDE -> TriStateFilter.ENABLED_NOT + else -> throw IllegalStateException("Unknown TriState state: $this") + } +} + +private fun TriStateFilter.toTriStateInt(): Int { + return when (this) { + TriStateFilter.DISABLED -> Filter.TriState.STATE_IGNORE + TriStateFilter.ENABLED_IS -> Filter.TriState.STATE_INCLUDE + TriStateFilter.ENABLED_NOT -> Filter.TriState.STATE_EXCLUDE + else -> throw IllegalStateException("Unknown TriStateFilter state: $this") + } +} 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 f1436846a4..7035c449e6 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 @@ -32,7 +32,6 @@ import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.util.chapter.getNextUnread import eu.kanade.tachiyomi.util.removeCovers -import eu.kanade.tachiyomi.widget.TriState import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine @@ -59,6 +58,7 @@ import tachiyomi.domain.library.model.sort import tachiyomi.domain.manga.interactor.GetLibraryManga import tachiyomi.domain.manga.model.Manga import tachiyomi.domain.manga.model.MangaUpdate +import tachiyomi.domain.manga.model.TriStateFilter import tachiyomi.domain.track.interactor.GetTracksPerManga import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -143,15 +143,15 @@ class LibraryScreenModel( getLibraryItemPreferencesFlow(), getTrackingFilterFlow(), ) { prefs, trackFilter -> - val a = ( - prefs.filterDownloaded or - prefs.filterUnread or - prefs.filterStarted or - prefs.filterBookmarked or - prefs.filterCompleted - ) != TriState.DISABLED.value - val b = trackFilter.values.any { it != TriState.DISABLED.value } - a || b + ( + listOf( + prefs.filterDownloaded, + prefs.filterUnread, + prefs.filterStarted, + prefs.filterBookmarked, + prefs.filterCompleted, + ) + trackFilter.values + ).any { it != TriStateFilter.DISABLED } } .distinctUntilChanged() .onEach { @@ -167,7 +167,7 @@ class LibraryScreenModel( */ private suspend fun LibraryMap.applyFilters( trackMap: Map>, - loggedInTrackServices: Map, + loggedInTrackServices: Map, ): LibraryMap { val prefs = getLibraryItemPreferencesFlow().first() val downloadedOnly = prefs.globalFilterDownloaded @@ -179,17 +179,17 @@ class LibraryScreenModel( val isNotLoggedInAnyTrack = loggedInTrackServices.isEmpty() - val excludedTracks = loggedInTrackServices.mapNotNull { if (it.value == TriState.ENABLED_NOT.value) it.key else null } - val includedTracks = loggedInTrackServices.mapNotNull { if (it.value == TriState.ENABLED_IS.value) it.key else null } + val excludedTracks = loggedInTrackServices.mapNotNull { if (it.value == TriStateFilter.ENABLED_NOT) it.key else null } + val includedTracks = loggedInTrackServices.mapNotNull { if (it.value == TriStateFilter.ENABLED_IS) it.key else null } val trackFiltersIsIgnored = includedTracks.isEmpty() && excludedTracks.isEmpty() val filterFnDownloaded: (LibraryItem) -> Boolean = downloaded@{ - if (!downloadedOnly && filterDownloaded == TriState.DISABLED.value) return@downloaded true + if (!downloadedOnly && filterDownloaded == TriStateFilter.DISABLED) return@downloaded true val isDownloaded = it.libraryManga.manga.isLocal() || it.downloadCount > 0 || downloadManager.getDownloadCount(it.libraryManga.manga) > 0 - return@downloaded if (downloadedOnly || filterDownloaded == TriState.ENABLED_IS.value) { + return@downloaded if (downloadedOnly || filterDownloaded == TriStateFilter.ENABLED_IS) { isDownloaded } else { !isDownloaded @@ -197,10 +197,10 @@ class LibraryScreenModel( } val filterFnUnread: (LibraryItem) -> Boolean = unread@{ - if (filterUnread == TriState.DISABLED.value) return@unread true + if (filterUnread == TriStateFilter.DISABLED) return@unread true val isUnread = it.libraryManga.unreadCount > 0 - return@unread if (filterUnread == TriState.ENABLED_IS.value) { + return@unread if (filterUnread == TriStateFilter.ENABLED_IS) { isUnread } else { !isUnread @@ -208,10 +208,10 @@ class LibraryScreenModel( } val filterFnStarted: (LibraryItem) -> Boolean = started@{ - if (filterStarted == TriState.DISABLED.value) return@started true + if (filterStarted == TriStateFilter.DISABLED) return@started true val hasStarted = it.libraryManga.hasStarted - return@started if (filterStarted == TriState.ENABLED_IS.value) { + return@started if (filterStarted == TriStateFilter.ENABLED_IS) { hasStarted } else { !hasStarted @@ -219,10 +219,10 @@ class LibraryScreenModel( } val filterFnBookmarked: (LibraryItem) -> Boolean = bookmarked@{ - if (filterBookmarked == TriState.DISABLED.value) return@bookmarked true + if (filterBookmarked == TriStateFilter.DISABLED) return@bookmarked true val hasBookmarks = it.libraryManga.hasBookmarks - return@bookmarked if (filterBookmarked == TriState.ENABLED_IS.value) { + return@bookmarked if (filterBookmarked == TriStateFilter.ENABLED_IS) { hasBookmarks } else { !hasBookmarks @@ -230,10 +230,10 @@ class LibraryScreenModel( } val filterFnCompleted: (LibraryItem) -> Boolean = completed@{ - if (filterCompleted == TriState.DISABLED.value) return@completed true + if (filterCompleted == TriStateFilter.DISABLED) return@completed true val isCompleted = it.libraryManga.manga.status.toInt() == SManga.COMPLETED - return@completed if (filterCompleted == TriState.ENABLED_IS.value) { + return@completed if (filterCompleted == TriStateFilter.ENABLED_IS) { isCompleted } else { !isCompleted @@ -349,11 +349,11 @@ class LibraryScreenModel( localBadge = it[1] as Boolean, languageBadge = it[2] as Boolean, globalFilterDownloaded = it[3] as Boolean, - filterDownloaded = it[4] as Int, - filterUnread = it[5] as Int, - filterStarted = it[6] as Int, - filterBookmarked = it[7] as Int, - filterCompleted = it[8] as Int, + filterDownloaded = it[4] as TriStateFilter, + filterUnread = it[5] as TriStateFilter, + filterStarted = it[6] as TriStateFilter, + filterBookmarked = it[7] as TriStateFilter, + filterCompleted = it[8] as TriStateFilter, ) }, ) @@ -406,7 +406,7 @@ class LibraryScreenModel( * * @return map of track id with the filter value */ - private fun getTrackingFilterFlow(): Flow> { + private fun getTrackingFilterFlow(): Flow> { val loggedServices = trackManager.services.filter { it.isLogged } return if (loggedServices.isNotEmpty()) { val prefFlows = loggedServices @@ -706,11 +706,11 @@ class LibraryScreenModel( val languageBadge: Boolean, val globalFilterDownloaded: Boolean, - val filterDownloaded: Int, - val filterUnread: Int, - val filterStarted: Int, - val filterBookmarked: Int, - val filterCompleted: Int, + val filterDownloaded: TriStateFilter, + val filterUnread: TriStateFilter, + val filterStarted: TriStateFilter, + val filterBookmarked: TriStateFilter, + val filterCompleted: TriStateFilter, ) @Immutable 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 fae076b773..cfda4895f7 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 @@ -8,13 +8,13 @@ import eu.kanade.domain.category.interactor.SetSortModeForCategory 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 tachiyomi.core.preference.Preference import tachiyomi.core.preference.getAndSet import tachiyomi.core.util.lang.launchIO import tachiyomi.domain.category.model.Category import tachiyomi.domain.library.model.LibraryDisplayMode import tachiyomi.domain.library.model.LibrarySort +import tachiyomi.domain.manga.model.TriStateFilter import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -32,9 +32,9 @@ class LibrarySettingsScreenModel( preference(libraryPreferences).toggle() } - fun toggleFilter(preference: (LibraryPreferences) -> Preference) { + fun toggleFilter(preference: (LibraryPreferences) -> Preference) { preference(libraryPreferences).getAndSet { - TriState.valueOf(it).next().value + it.next() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index dc3e767208..ccb86bafa5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -137,6 +137,7 @@ class MainActivity : BaseActivity() { libraryPreferences = libraryPreferences, readerPreferences = Injekt.get(), backupPreferences = Injekt.get(), + trackManager = Injekt.get(), ) } else { false 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 106755d240..6e2cfa9086 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/TriState.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/TriState.kt @@ -1,34 +1,13 @@ package eu.kanade.tachiyomi.widget +import eu.kanade.tachiyomi.source.model.Filter import tachiyomi.domain.manga.model.TriStateFilter -// TODO: replace this with TriStateFilter entirely -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 { return when (this) { - TriState.DISABLED.value -> TriStateFilter.DISABLED - TriState.ENABLED_IS.value -> TriStateFilter.ENABLED_IS - TriState.ENABLED_NOT.value -> TriStateFilter.ENABLED_NOT - else -> throw IllegalStateException("Unknown TriStateGroup state: $this") + Filter.TriState.STATE_IGNORE -> TriStateFilter.DISABLED + Filter.TriState.STATE_INCLUDE -> TriStateFilter.ENABLED_IS + Filter.TriState.STATE_EXCLUDE -> TriStateFilter.ENABLED_NOT + else -> throw IllegalStateException("Unknown TriState state: $this") } }