Yokan / rebuild
Course 1 8624561

ChapterSelection.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/detail/ChapterSelection.kt

implementation55 lines
1package eu.friedcutlet.yokan.ui.screens.detail23import eu.friedcutlet.yokan.domain.Chapter45internal enum class ChapterSelectionOption {6    All, Invert, Unread, Read, Downloaded, NotDownloaded, Bookmarked, Above, Below,7}89/** Selected chapter ids; [anchorId] is where range selections start. Chapter lists are in display order. */10internal data class ChapterSelectionState(11    val ids: Set<Long> = emptySet(),12    val anchorId: Long? = null,13) {14    fun toggle(id: Long) = copy(ids = if (id in ids) ids - id else ids + id, anchorId = id)1516    fun retainVisible(chapters: List<Chapter>): ChapterSelectionState {17        val visible = chapters.map { it.id }.toSet()18        return copy(ids = ids intersect visible, anchorId = anchorId?.takeIf(visible::contains))19    }2021    fun rangeTo(chapters: List<Chapter>, id: Long): ChapterSelectionState {22        val from = chapters.indexOfFirst { it.id == anchorId }23        val to = chapters.indexOfFirst { it.id == id }24        return if (to < 0) this25        else if (from < 0) copy(ids = ids + id, anchorId = id)26        else copy(ids = ids + chapters.subList(minOf(from, to), maxOf(from, to) + 1).map { it.id })27    }2829    /** Adds display rows [first]..[last] (1-based); invalid bounds change nothing. */30    fun selectRange(chapters: List<Chapter>, first: Int, last: Int): ChapterSelectionState {31        if (first !in 1..chapters.size || last !in first..chapters.size) return this32        val range = chapters.subList(first - 1, last)33        return copy(ids = ids + range.map(Chapter::id), anchorId = range.first().id)34    }3536    fun select(chapters: List<Chapter>, option: ChapterSelectionOption): ChapterSelectionState {37        val anchor = chapters.indexOfFirst { it.id == anchorId }38        val selected = when (option) {39            ChapterSelectionOption.All -> chapters.map(Chapter::id).toSet()40            ChapterSelectionOption.Invert -> chapters.map(Chapter::id).toSet() - ids41            ChapterSelectionOption.Above -> if (anchor < 0) ids else ids + chapters.take(anchor + 1).map(Chapter::id)42            ChapterSelectionOption.Below -> if (anchor < 0) ids else ids + chapters.drop(anchor).map(Chapter::id)43            else -> chapters.filter {44                when (option) {45                    ChapterSelectionOption.Unread -> !it.isRead46                    ChapterSelectionOption.Read -> it.isRead47                    ChapterSelectionOption.Downloaded -> it.downloadSize != null || it.archivePath.isNotBlank()48                    ChapterSelectionOption.NotDownloaded -> it.downloadSize == null && it.archivePath.isBlank()49                    ChapterSelectionOption.Bookmarked -> it.isBookmarked50                }51            }.map(Chapter::id).toSet()52        }53        return copy(ids = selected, anchorId = anchorId?.takeIf(selected::contains) ?: chapters.firstOrNull { it.id in selected }?.id)54    }55}56

Search chapters, reference and source filenames.