ReaderChapterSequence.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/library/ReaderChapterSequence.kt
1package eu.friedcutlet.yokan.services.library23import eu.friedcutlet.yokan.database.DbSettings4import eu.friedcutlet.yokan.database.chapterReleasePreference5import eu.friedcutlet.yokan.domain.Chapter6import eu.friedcutlet.yokan.domain.ChapterReleasePreferences7import eu.friedcutlet.yokan.domain.Manga8import eu.friedcutlet.yokan.domain.chapterReadingOrder9import eu.friedcutlet.yokan.domain.chapterReleaseIdentity10import eu.friedcutlet.yokan.domain.selectChapterReleases1112/**13 * A directly opened alternate remains readable; it never enables the whole excluded raw list.14 * Reader policies may keep title-filtered chapters and skip read ones; the current chapter always stays.15 */16fun readingChapterSequence(17 manga: Manga,18 currentChapterId: Long,19 all: List<Chapter>,20 db: DbSettings,21 downloadedIds: Set<Long>,22): List<Chapter> {23 val policy = db.features.reader24 return resolveReadingChapterSequence(25 manga.name, currentChapterId,26 if (policy.skipFilteredChapters) filterChapters(manga, all, db, downloadedIds) else all,27 all,28 db.chapterReleasePreference(manga.id).skipDuplicateReleases,29 policy.skipReadChapters,30 )31}3233internal fun resolveReadingChapterSequence(34 mangaTitle: String,35 currentChapterId: Long,36 filteredChapters: List<Chapter>,37 allChapters: List<Chapter>,38 skipDuplicates: Boolean = true,39 skipRead: Boolean = false,40): List<Chapter> {41 // Releases are chosen before dropping read ones, so a read chapter never falls back to an unread alternate.42 val selected = selectChapterReleases(mangaTitle, filteredChapters, ChapterReleasePreferences(skipDuplicates = skipDuplicates))43 .let { releases -> if (skipRead) releases.filter { !it.isRead || it.id == currentChapterId } else releases }44 if (selected.any { it.id == currentChapterId }) return chapterReadingOrder(mangaTitle, selected)45 val current = allChapters.firstOrNull { it.id == currentChapterId } ?: return chapterReadingOrder(mangaTitle, selected)46 val identity = chapterReleaseIdentity(mangaTitle, current.name, current.number, current.season, current.numberingName)47 val others = if (skipDuplicates && identity != null) selected.filterNot { chapterReleaseIdentity(mangaTitle, it.name, it.number, it.season, it.numberingName) == identity }48 else selected49 return chapterReadingOrder(mangaTitle, others + current)50}51