Migrate TriState usages to TriStateFilter enum

This commit is contained in:
arkon 2023-02-24 16:09:47 -05:00
parent a0e76d2fd9
commit 7ec87e76db
9 changed files with 108 additions and 85 deletions

View File

@ -22,7 +22,7 @@ android {
defaultConfig {
applicationId = "eu.kanade.tachiyomi"
versionCode = 98
versionCode = 99
versionName = "0.14.5"
buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"")

View File

@ -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

View File

@ -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()) },
)
}

View File

@ -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
}

View File

@ -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")
}
}

View File

@ -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<Long, List<Long>>,
loggedInTrackServices: Map<Long, Int>,
loggedInTrackServices: Map<Long, TriStateFilter>,
): 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<Map<Long, Int>> {
private fun getTrackingFilterFlow(): Flow<Map<Long, TriStateFilter>> {
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

View File

@ -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<Int>) {
fun toggleFilter(preference: (LibraryPreferences) -> Preference<TriStateFilter>) {
preference(libraryPreferences).getAndSet {
TriState.valueOf(it).next().value
it.next()
}
}

View File

@ -137,6 +137,7 @@ class MainActivity : BaseActivity() {
libraryPreferences = libraryPreferences,
readerPreferences = Injekt.get(),
backupPreferences = Injekt.get(),
trackManager = Injekt.get(),
)
} else {
false

View File

@ -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")
}
}