LibraryPreferences.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/library/LibraryPreferences.kt
1package eu.friedcutlet.yokan.ui.screens.library23import eu.friedcutlet.yokan.data.toDb4import eu.friedcutlet.yokan.data.toDomain5import eu.friedcutlet.yokan.database.DbSettings6import eu.friedcutlet.yokan.database.DbSortLibraryManga7import eu.friedcutlet.yokan.database.DbCategoryLibraryDisplay8import eu.friedcutlet.yokan.database.DbCategoryLibraryPreferences9import eu.friedcutlet.yokan.database.DbLibraryGrouping10import eu.friedcutlet.yokan.database.DbLibraryExtraFilters11import kotlin.random.Random12import eu.friedcutlet.yokan.domain.DisplayType13import eu.friedcutlet.yokan.domain.ItemType1415/** How one item type's library is laid out and which badges its entries show. */16data class LibraryDisplay(17 val displayType: DisplayType,18 /** Columns per row; 0 derives them from the width. */19 val gridSize: Int,20 val downloadBadges: Boolean,21 val language: Boolean,22 val source: Boolean,23 val localBadge: Boolean,24 val continueButton: Boolean,25 val categoryTabs: Boolean,26 val itemCounts: Boolean,27)2829internal fun LibraryDisplay.forCategory(preferences: DbCategoryLibraryPreferences?): LibraryDisplay =30 preferences?.display?.let { stored -> copy(31 displayType = stored.displayType.toDomain(), gridSize = stored.gridSize.coerceIn(0, 7),32 downloadBadges = stored.downloadBadges, language = stored.language, source = stored.source,33 localBadge = stored.localBadge, continueButton = stored.continueButton,34 ) } ?: this3536internal fun LibraryDisplay.categoryPreferences() = DbCategoryLibraryDisplay(37 displayType.toDb(), gridSize.coerceIn(0, 7), downloadBadges, language, source, localBadge, continueButton,38)3940internal fun DbSettings.withLibraryGrouping(type: ItemType, grouping: DbLibraryGrouping): DbSettings =41 copy(features = features.copy(library = features.library.withGrouping(type.toDb(), grouping)))4243internal fun DbSettings.withCategoryLibraryPreferences(type: ItemType, id: Long, preferences: DbCategoryLibraryPreferences?): DbSettings =44 copy(features = features.copy(library = features.library.withCategory(type.toDb(), id, preferences)))4546internal fun DbSettings.withCategoryLibraryDisplay(type: ItemType, id: Long, transform: (DbSettings) -> DbSettings): DbSettings {47 val current = features.library.categories(type.toDb())[id] ?: DbCategoryLibraryPreferences()48 // Apply existing display controls to the effective category display, then store only card options.49 val effective = libraryDisplay(type).forCategory(current)50 val seed = withLibraryDisplayType(type, effective.displayType).withLibraryGridSize(type, effective.gridSize)51 .withLibraryDisplay(type, effective.downloadBadges, effective.language, effective.source,52 effective.localBadge, effective.continueButton)53 return withCategoryLibraryPreferences(type, id, current.copy(display = transform(seed).libraryDisplay(type).categoryPreferences()))54}5556internal fun DbSettings.withCategoryLibrarySortSelected(type: ItemType, id: Long, index: Int): DbSettings {57 val current = features.library.categories(type.toDb())[id] ?: DbCategoryLibraryPreferences()58 val before = current.sort ?: librarySort(type)59 return withCategoryLibraryPreferences(type, id, current.copy(sort = before.copy(60 index = index, reverse = if (before.index == index) !before.reverse else before.reverse,61 )))62}6364fun DbSettings.libraryDisplay(type: ItemType): LibraryDisplay = when (type) {65 ItemType.Manga -> LibraryDisplay(66 displayType = displayType.toDomain(),67 gridSize = (mangaGridSize ?: 0).coerceIn(0, 7),68 downloadBadges = libraryDownloadedChapters,69 language = libraryShowLanguage,70 source = libraryShowSource,71 localBadge = libraryLocalSource == true,72 continueButton = libraryShowContinueReadingButton,73 categoryTabs = libraryShowCategoryTabs,74 itemCounts = libraryShowNumbersOfItems,75 )76 ItemType.Anime -> LibraryDisplay(77 displayType = animeDisplayType.toDomain(),78 gridSize = (animeGridSize ?: 0).coerceIn(0, 7),79 downloadBadges = animeLibraryDownloadedChapters,80 language = animeLibraryShowLanguage,81 source = animeLibraryShowSource,82 localBadge = animeLibraryLocalSource == true,83 continueButton = animeLibraryShowContinueReadingButton,84 categoryTabs = animeLibraryShowCategoryTabs,85 itemCounts = animeLibraryShowNumbersOfItems,86 )87 ItemType.Novel -> LibraryDisplay(88 displayType = novelDisplayType.toDomain(),89 gridSize = (novelGridSize ?: 0).coerceIn(0, 7),90 downloadBadges = novelLibraryDownloadedChapters,91 language = novelLibraryShowLanguage,92 source = novelLibraryShowSource,93 localBadge = novelLibraryLocalSource == true,94 continueButton = novelLibraryShowContinueReadingButton,95 categoryTabs = novelLibraryShowCategoryTabs,96 itemCounts = novelLibraryShowNumbersOfItems,97 )98}99100/** Sets the given display flags for [type]; `null` keeps the stored value. */101fun DbSettings.withLibraryDisplay(102 type: ItemType,103 downloadBadges: Boolean? = null,104 language: Boolean? = null,105 source: Boolean? = null,106 localBadge: Boolean? = null,107 continueButton: Boolean? = null,108 categoryTabs: Boolean? = null,109 itemCounts: Boolean? = null,110): DbSettings = when (type) {111 ItemType.Manga -> copy(112 libraryDownloadedChapters = downloadBadges ?: libraryDownloadedChapters,113 libraryShowLanguage = language ?: libraryShowLanguage,114 libraryShowSource = source ?: libraryShowSource,115 libraryLocalSource = localBadge ?: libraryLocalSource,116 libraryShowContinueReadingButton = continueButton ?: libraryShowContinueReadingButton,117 libraryShowCategoryTabs = categoryTabs ?: libraryShowCategoryTabs,118 libraryShowNumbersOfItems = itemCounts ?: libraryShowNumbersOfItems,119 )120 ItemType.Anime -> copy(121 animeLibraryDownloadedChapters = downloadBadges ?: animeLibraryDownloadedChapters,122 animeLibraryShowLanguage = language ?: animeLibraryShowLanguage,123 animeLibraryShowSource = source ?: animeLibraryShowSource,124 animeLibraryLocalSource = localBadge ?: animeLibraryLocalSource,125 animeLibraryShowContinueReadingButton = continueButton ?: animeLibraryShowContinueReadingButton,126 animeLibraryShowCategoryTabs = categoryTabs ?: animeLibraryShowCategoryTabs,127 animeLibraryShowNumbersOfItems = itemCounts ?: animeLibraryShowNumbersOfItems,128 )129 ItemType.Novel -> copy(130 novelLibraryDownloadedChapters = downloadBadges ?: novelLibraryDownloadedChapters,131 novelLibraryShowLanguage = language ?: novelLibraryShowLanguage,132 novelLibraryShowSource = source ?: novelLibraryShowSource,133 novelLibraryLocalSource = localBadge ?: novelLibraryLocalSource,134 novelLibraryShowContinueReadingButton = continueButton ?: novelLibraryShowContinueReadingButton,135 novelLibraryShowCategoryTabs = categoryTabs ?: novelLibraryShowCategoryTabs,136 novelLibraryShowNumbersOfItems = itemCounts ?: novelLibraryShowNumbersOfItems,137 )138}139140fun DbSettings.withLibraryDisplayType(type: ItemType, displayType: DisplayType): DbSettings {141 val value = displayType.toDb()142 return when (type) {143 ItemType.Manga -> copy(displayType = value)144 ItemType.Anime -> copy(animeDisplayType = value)145 ItemType.Novel -> copy(novelDisplayType = value)146 }147}148149fun DbSettings.withLibraryGridSize(type: ItemType, size: Int): DbSettings {150 val normalized = size.coerceIn(0, 7)151 return when (type) {152 ItemType.Manga -> copy(mangaGridSize = normalized)153 ItemType.Anime -> copy(animeGridSize = normalized)154 ItemType.Novel -> copy(novelGridSize = normalized)155 }156}157158fun DbSettings.librarySearch(type: ItemType): String = when (type) {159 ItemType.Manga -> librarySearchQuery160 ItemType.Anime -> animeLibrarySearchQuery161 ItemType.Novel -> novelLibrarySearchQuery162}163164fun DbSettings.withLibrarySearch(type: ItemType, query: String): DbSettings = when (type) {165 ItemType.Manga -> copy(librarySearchQuery = query)166 ItemType.Anime -> copy(animeLibrarySearchQuery = query)167 ItemType.Novel -> copy(novelLibrarySearchQuery = query)168}169170fun DbSettings.libraryFilters(type: ItemType): LibraryFilterTypes = when (type) {171 ItemType.Manga -> LibraryFilterTypes(172 downloaded = libraryFilterMangasDownloadType,173 unread = libraryFilterMangasUnreadType,174 started = libraryFilterMangasStartedType,175 bookmarked = libraryFilterMangasBookMarkedType,176 completed = libraryFilterMangasCompletedType,177 tracking = libraryFilterMangasTrackingType,178 )179 ItemType.Anime -> LibraryFilterTypes(180 downloaded = libraryFilterAnimeDownloadType,181 unread = libraryFilterAnimeUnreadType,182 started = libraryFilterAnimeStartedType,183 bookmarked = libraryFilterAnimeBookMarkedType,184 completed = libraryFilterAnimeCompletedType,185 tracking = libraryFilterAnimeTrackingType,186 )187 ItemType.Novel -> LibraryFilterTypes(188 downloaded = libraryFilterNovelDownloadType,189 unread = libraryFilterNovelUnreadType,190 started = libraryFilterNovelStartedType,191 bookmarked = libraryFilterNovelBookMarkedType,192 completed = libraryFilterNovelCompletedType,193 tracking = libraryFilterNovelTrackingType,194 )195}.let { base ->196 val extra = features.library.filters(type.toDb())197 base.copy(customInterval = extra.customInterval.takeIf { it in 0..2 } ?: 0,198 providerTracking = extra.providerTracking.filter { (id, value) -> id > 0 && value in 1..2 })199}200201private fun DbSettings.withLibraryCustomIntervalFilter(type: ItemType, value: Int): DbSettings = copy(features = features.copy(202 library = features.library.withFilters(type.toDb(), features.library.filters(type.toDb()).copy(customInterval = value)),203))204205internal fun DbSettings.withLibraryProviderTrackingCycled(type: ItemType, providerId: Int): DbSettings {206 if (providerId <= 0) return this207 val before = features.library.filters(type.toDb())208 val next = ((before.providerTracking[providerId] ?: 0).coerceIn(0, 2) + 1) % 3209 return copy(features = features.copy(library = features.library.withFilters(type.toDb(), before.copy(210 providerTracking = if (next == 0) before.providerTracking - providerId else before.providerTracking + (providerId to next),211 ))))212}213214internal fun DbSettings.withLibraryRandomSeed(type: ItemType, seed: Long): DbSettings = copy(features = features.copy(215 library = features.library.copy(randomSeeds = features.library.randomSeeds + (type.toDb() to seed)),216))217218/** Advances [kind] for [type] through off → include → exclude. */219fun DbSettings.withLibraryFilterCycled(type: ItemType, kind: LibraryFilterKind): DbSettings {220 val next = (libraryFilters(type).value(kind) + 1) % 3221 return when (type) {222 ItemType.Manga -> when (kind) {223 LibraryFilterKind.Downloaded -> copy(libraryFilterMangasDownloadType = next)224 LibraryFilterKind.Unread -> copy(libraryFilterMangasUnreadType = next)225 LibraryFilterKind.Started -> copy(libraryFilterMangasStartedType = next)226 LibraryFilterKind.Bookmarked -> copy(libraryFilterMangasBookMarkedType = next)227 LibraryFilterKind.Completed -> copy(libraryFilterMangasCompletedType = next)228 LibraryFilterKind.Tracking -> copy(libraryFilterMangasTrackingType = next)229 LibraryFilterKind.CustomInterval -> withLibraryCustomIntervalFilter(type, next)230 }231 ItemType.Anime -> when (kind) {232 LibraryFilterKind.Downloaded -> copy(libraryFilterAnimeDownloadType = next)233 LibraryFilterKind.Unread -> copy(libraryFilterAnimeUnreadType = next)234 LibraryFilterKind.Started -> copy(libraryFilterAnimeStartedType = next)235 LibraryFilterKind.Bookmarked -> copy(libraryFilterAnimeBookMarkedType = next)236 LibraryFilterKind.Completed -> copy(libraryFilterAnimeCompletedType = next)237 LibraryFilterKind.Tracking -> copy(libraryFilterAnimeTrackingType = next)238 LibraryFilterKind.CustomInterval -> withLibraryCustomIntervalFilter(type, next)239 }240 ItemType.Novel -> when (kind) {241 LibraryFilterKind.Downloaded -> copy(libraryFilterNovelDownloadType = next)242 LibraryFilterKind.Unread -> copy(libraryFilterNovelUnreadType = next)243 LibraryFilterKind.Started -> copy(libraryFilterNovelStartedType = next)244 LibraryFilterKind.Bookmarked -> copy(libraryFilterNovelBookMarkedType = next)245 LibraryFilterKind.Completed -> copy(libraryFilterNovelCompletedType = next)246 LibraryFilterKind.Tracking -> copy(libraryFilterNovelTrackingType = next)247 LibraryFilterKind.CustomInterval -> withLibraryCustomIntervalFilter(type, next)248 }249 }250}251252fun DbSettings.librarySort(type: ItemType): DbSortLibraryManga = when (type) {253 ItemType.Manga -> sortLibraryManga254 ItemType.Anime -> sortLibraryAnime255 ItemType.Novel -> sortLibraryNovel256} ?: DbSortLibraryManga()257258/** Selects sort [index] for [type]; selecting the current one flips its direction. */259fun DbSettings.withLibrarySortSelected(type: ItemType, index: Int): DbSettings {260 val current = librarySort(type)261 val next = current.copy(262 index = index,263 reverse = if (current.index == index) !current.reverse else current.reverse,264 )265 val updated = when (type) {266 ItemType.Manga -> copy(sortLibraryManga = next)267 ItemType.Anime -> copy(sortLibraryAnime = next)268 ItemType.Novel -> copy(sortLibraryNovel = next)269 }270 return if (index == 7 && current.index != 7) updated.withLibraryRandomSeed(type, Random.nextLong()) else updated271}272