package eu.friedcutlet.yokan.services.library import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.database.chapterReleasePreference import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.domain.ChapterReleasePreferences import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.domain.chapterReadingOrder import eu.friedcutlet.yokan.domain.chapterReleaseIdentity import eu.friedcutlet.yokan.domain.selectChapterReleases /** * A directly opened alternate remains readable; it never enables the whole excluded raw list. * Reader policies may keep title-filtered chapters and skip read ones; the current chapter always stays. */ fun readingChapterSequence( manga: Manga, currentChapterId: Long, all: List, db: DbSettings, downloadedIds: Set, ): List { val policy = db.features.reader return resolveReadingChapterSequence( manga.name, currentChapterId, if (policy.skipFilteredChapters) filterChapters(manga, all, db, downloadedIds) else all, all, db.chapterReleasePreference(manga.id).skipDuplicateReleases, policy.skipReadChapters, ) } internal fun resolveReadingChapterSequence( mangaTitle: String, currentChapterId: Long, filteredChapters: List, allChapters: List, skipDuplicates: Boolean = true, skipRead: Boolean = false, ): List { // Releases are chosen before dropping read ones, so a read chapter never falls back to an unread alternate. val selected = selectChapterReleases(mangaTitle, filteredChapters, ChapterReleasePreferences(skipDuplicates = skipDuplicates)) .let { releases -> if (skipRead) releases.filter { !it.isRead || it.id == currentChapterId } else releases } if (selected.any { it.id == currentChapterId }) return chapterReadingOrder(mangaTitle, selected) val current = allChapters.firstOrNull { it.id == currentChapterId } ?: return chapterReadingOrder(mangaTitle, selected) val identity = chapterReleaseIdentity(mangaTitle, current.name, current.number, current.season, current.numberingName) val others = if (skipDuplicates && identity != null) selected.filterNot { chapterReleaseIdentity(mangaTitle, it.name, it.number, it.season, it.numberingName) == identity } else selected return chapterReadingOrder(mangaTitle, others + current) }