Split library preferences from PreferencesHelper (#8036)

This commit is contained in:
Andreas
2022-09-20 05:55:07 +02:00
committed by GitHub
parent 0e3176a77c
commit c740558327
31 changed files with 234 additions and 193 deletions

View File

@@ -3,7 +3,7 @@ package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.anyWithName
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.domain.library.service.LibraryPreferences
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
@@ -11,7 +11,7 @@ import logcat.LogPriority
class CreateCategoryWithName(
private val categoryRepository: CategoryRepository,
private val preferences: PreferencesHelper,
private val preferences: LibraryPreferences,
) {
private val initialFlags: Long

View File

@@ -1,11 +1,11 @@
package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.library.setting.plus
import eu.kanade.domain.library.model.plus
import eu.kanade.domain.library.service.LibraryPreferences
class ResetCategoryFlags(
private val preferences: PreferencesHelper,
private val preferences: LibraryPreferences,
private val categoryRepository: CategoryRepository,
) {

View File

@@ -3,12 +3,12 @@ package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.library.setting.LibraryDisplayMode
import eu.kanade.tachiyomi.ui.library.setting.plus
import eu.kanade.domain.library.model.LibraryDisplayMode
import eu.kanade.domain.library.model.plus
import eu.kanade.domain.library.service.LibraryPreferences
class SetDisplayModeForCategory(
private val preferences: PreferencesHelper,
private val preferences: LibraryPreferences,
private val categoryRepository: CategoryRepository,
) {

View File

@@ -3,12 +3,12 @@ package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.library.setting.LibrarySort
import eu.kanade.tachiyomi.ui.library.setting.plus
import eu.kanade.domain.library.model.LibrarySort
import eu.kanade.domain.library.model.plus
import eu.kanade.domain.library.service.LibraryPreferences
class SetSortModeForCategory(
private val preferences: PreferencesHelper,
private val preferences: LibraryPreferences,
private val categoryRepository: CategoryRepository,
) {

View File

@@ -0,0 +1,35 @@
package eu.kanade.domain.library.model
interface Flag {
val flag: Long
}
interface Mask {
val mask: Long
}
interface FlagWithMask : Flag, Mask
operator fun Long.contains(other: Flag): Boolean {
return if (other is Mask) {
other.flag == this and other.mask
} else {
other.flag == this
}
}
operator fun Long.plus(other: Flag): Long {
return if (other is Mask) {
this and other.mask.inv() or (other.flag and other.mask)
} else {
this or other.flag
}
}
operator fun Flag.plus(other: Flag): Long {
return if (other is Mask) {
this.flag and other.mask.inv() or (other.flag and other.mask)
} else {
this.flag or other.flag
}
}

View File

@@ -0,0 +1,59 @@
package eu.kanade.domain.library.model
import eu.kanade.domain.category.model.Category
sealed class LibraryDisplayMode(
override val flag: Long,
) : FlagWithMask {
override val mask: Long = 0b00000011L
object CompactGrid : LibraryDisplayMode(0b00000000)
object ComfortableGrid : LibraryDisplayMode(0b00000001)
object List : LibraryDisplayMode(0b00000010)
object CoverOnlyGrid : LibraryDisplayMode(0b00000011)
object Serializer {
fun deserialize(serialized: String): LibraryDisplayMode {
return LibraryDisplayMode.deserialize(serialized)
}
fun serialize(value: LibraryDisplayMode): String {
return value.serialize()
}
}
companion object {
val values = setOf(CompactGrid, ComfortableGrid, List, CoverOnlyGrid)
val default = CompactGrid
fun valueOf(flag: Long?): LibraryDisplayMode {
if (flag == null) return default
return values
.find { mode -> mode.flag == flag and mode.mask }
?: default
}
fun deserialize(serialized: String): LibraryDisplayMode {
return when (serialized) {
"COMFORTABLE_GRID" -> ComfortableGrid
"COMPACT_GRID" -> CompactGrid
"COVER_ONLY_GRID" -> CoverOnlyGrid
"LIST" -> List
else -> default
}
}
}
fun serialize(): String {
return when (this) {
ComfortableGrid -> "COMFORTABLE_GRID"
CompactGrid -> "COMPACT_GRID"
CoverOnlyGrid -> "COVER_ONLY_GRID"
List -> "LIST"
}
}
}
val Category.display: LibraryDisplayMode
get() = LibraryDisplayMode.valueOf(flags)

View File

@@ -0,0 +1,121 @@
package eu.kanade.domain.library.model
import eu.kanade.domain.category.model.Category
data class LibrarySort(
val type: Type,
val direction: Direction,
) : FlagWithMask {
override val flag: Long
get() = type + direction
override val mask: Long
get() = type.mask or direction.mask
val isAscending: Boolean
get() = direction == Direction.Ascending
sealed class Type(
override val flag: Long,
) : FlagWithMask {
override val mask: Long = 0b00111100L
object Alphabetical : Type(0b00000000)
object LastRead : Type(0b00000100)
object LastUpdate : Type(0b00001000)
object UnreadCount : Type(0b00001100)
object TotalChapters : Type(0b00010000)
object LatestChapter : Type(0b00010100)
object ChapterFetchDate : Type(0b00011000)
object DateAdded : Type(0b00011100)
companion object {
fun valueOf(flag: Long): Type {
return types.find { type -> type.flag == flag and type.mask } ?: default.type
}
}
}
sealed class Direction(
override val flag: Long,
) : FlagWithMask {
override val mask: Long = 0b01000000L
object Ascending : Direction(0b01000000)
object Descending : Direction(0b00000000)
companion object {
fun valueOf(flag: Long): Direction {
return directions.find { direction -> direction.flag == flag and direction.mask } ?: default.direction
}
}
}
object Serializer {
fun deserialize(serialized: String): LibrarySort {
return LibrarySort.deserialize(serialized)
}
fun serialize(value: LibrarySort): String {
return value.serialize()
}
}
companion object {
val types = setOf(Type.Alphabetical, Type.LastRead, Type.LastUpdate, Type.UnreadCount, Type.TotalChapters, Type.LatestChapter, Type.ChapterFetchDate, Type.DateAdded)
val directions = setOf(Direction.Ascending, Direction.Descending)
val default = LibrarySort(Type.Alphabetical, Direction.Ascending)
fun valueOf(flag: Long): LibrarySort {
return LibrarySort(
Type.valueOf(flag),
Direction.valueOf(flag),
)
}
fun deserialize(serialized: String): LibrarySort {
if (serialized.isEmpty()) return default
return try {
val values = serialized.split(",")
val type = when (values[0]) {
"ALPHABETICAL" -> Type.Alphabetical
"LAST_READ" -> Type.LastRead
"LAST_MANGA_UPDATE" -> Type.LastUpdate
"UNREAD_COUNT" -> Type.UnreadCount
"TOTAL_CHAPTERS" -> Type.TotalChapters
"LATEST_CHAPTER" -> Type.LatestChapter
"CHAPTER_FETCH_DATE" -> Type.ChapterFetchDate
"DATE_ADDED" -> Type.DateAdded
else -> Type.Alphabetical
}
val ascending = if (values[1] == "ASCENDING") Direction.Ascending else Direction.Descending
LibrarySort(type, ascending)
} catch (e: Exception) {
default
}
}
}
fun serialize(): String {
val type = when (type) {
Type.Alphabetical -> "ALPHABETICAL"
Type.LastRead -> "LAST_READ"
Type.LastUpdate -> "LAST_MANGA_UPDATE"
Type.UnreadCount -> "UNREAD_COUNT"
Type.TotalChapters -> "TOTAL_CHAPTERS"
Type.LatestChapter -> "LATEST_CHAPTER"
Type.ChapterFetchDate -> "CHAPTER_FETCH_DATE"
Type.DateAdded -> "DATE_ADDED"
}
val direction = if (direction == Direction.Ascending) "ASCENDING" else "DESCENDING"
return "$type,$direction"
}
}
val Category.sort: LibrarySort
get() = LibrarySort.valueOf(flags)

View File

@@ -0,0 +1,76 @@
package eu.kanade.domain.library.service
import eu.kanade.domain.library.model.LibraryDisplayMode
import eu.kanade.domain.library.model.LibrarySort
import eu.kanade.tachiyomi.core.preference.PreferenceStore
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.ExtendedNavigationView
class LibraryPreferences(
private val preferenceStore: PreferenceStore,
) {
fun libraryDisplayMode() = this.preferenceStore.getObject("pref_display_mode_library", LibraryDisplayMode.default, LibraryDisplayMode.Serializer::serialize, LibraryDisplayMode.Serializer::deserialize)
fun librarySortingMode() = this.preferenceStore.getObject("library_sorting_mode", LibrarySort.default, LibrarySort.Serializer::serialize, LibrarySort.Serializer::deserialize)
fun portraitColumns() = this.preferenceStore.getInt("pref_library_columns_portrait_key", 0)
fun landscapeColumns() = this.preferenceStore.getInt("pref_library_columns_landscape_key", 0)
fun libraryUpdateInterval() = this.preferenceStore.getInt("pref_library_update_interval_key", 24)
fun libraryUpdateLastTimestamp() = this.preferenceStore.getLong("library_update_last_timestamp", 0L)
fun libraryUpdateDeviceRestriction() = this.preferenceStore.getStringSet("library_update_restriction", setOf(DEVICE_ONLY_ON_WIFI))
fun libraryUpdateMangaRestriction() = this.preferenceStore.getStringSet("library_update_manga_restriction", setOf(MANGA_HAS_UNREAD, MANGA_NON_COMPLETED, MANGA_NON_READ))
// region Filter
fun filterDownloaded() = this.preferenceStore.getInt("pref_filter_library_downloaded", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun filterUnread() = this.preferenceStore.getInt("pref_filter_library_unread", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun filterStarted() = this.preferenceStore.getInt("pref_filter_library_started", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun filterCompleted() = this.preferenceStore.getInt("pref_filter_library_completed", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
fun filterTracking(name: Int) = this.preferenceStore.getInt("pref_filter_library_tracked_$name", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
// endregion
// region Badges
fun downloadBadge() = this.preferenceStore.getBoolean("display_download_badge", false)
fun localBadge() = this.preferenceStore.getBoolean("display_local_badge", true)
fun unreadBadge() = this.preferenceStore.getBoolean("display_unread_badge", true)
fun languageBadge() = this.preferenceStore.getBoolean("display_language_badge", false)
fun showUpdatesNavBadge() = this.preferenceStore.getBoolean("library_update_show_tab_badge", false)
fun unreadUpdatesCount() = this.preferenceStore.getInt("library_unread_updates_count", 0)
// endregion
// region Category
fun defaultCategory() = this.preferenceStore.getInt("default_category", -1)
fun lastUsedCategory() = this.preferenceStore.getInt("last_used_category", 0)
fun categoryTabs() = this.preferenceStore.getBoolean("display_category_tabs", true)
fun categoryNumberOfItems() = this.preferenceStore.getBoolean("display_number_of_items", false)
fun categorizedDisplaySettings() = this.preferenceStore.getBoolean("categorized_display", false)
fun libraryUpdateCategories() = this.preferenceStore.getStringSet("library_update_categories", emptySet())
fun libraryUpdateCategoriesExclude() = this.preferenceStore.getStringSet("library_update_categories_exclude", emptySet())
// endregion
}

View File

@@ -1,5 +1,6 @@
package eu.kanade.domain.source.service
import eu.kanade.domain.library.model.LibraryDisplayMode
import eu.kanade.domain.source.interactor.SetMigrateSorting
import eu.kanade.tachiyomi.core.preference.PreferenceStore
import eu.kanade.tachiyomi.core.preference.getEnum
@@ -9,6 +10,8 @@ class SourcePreferences(
private val preferenceStore: PreferenceStore,
) {
fun sourceDisplayMode() = this.preferenceStore.getObject("pref_display_mode_catalogue", LibraryDisplayMode.default, LibraryDisplayMode.Serializer::serialize, LibraryDisplayMode.Serializer::deserialize)
fun enabledLanguages() = this.preferenceStore.getStringSet("source_languages", LocaleHelper.getDefaultEnabledLanguages())
fun disabledSources() = this.preferenceStore.getStringSet("hidden_catalogues", emptySet())

View File

@@ -1,15 +1,15 @@
package eu.kanade.domain.updates.interactor
import eu.kanade.domain.library.service.LibraryPreferences
import eu.kanade.domain.updates.model.UpdatesWithRelations
import eu.kanade.domain.updates.repository.UpdatesRepository
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach
import java.util.Calendar
class GetUpdates(
private val repository: UpdatesRepository,
private val preferences: PreferencesHelper,
private val preferences: LibraryPreferences,
) {
fun subscribe(calendar: Calendar): Flow<List<UpdatesWithRelations>> = subscribe(calendar.time.time)