AutomaticReaderLayout.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/reader/AutomaticReaderLayout.kt
1package eu.friedcutlet.yokan.ui.screens.reader23import eu.friedcutlet.yokan.database.DbReaderMode45enum class ReaderDirection { Ltr, Rtl }6data class ReaderPageShape(val width: Int, val height: Int) {7 val valid: Boolean get() = width > 0 && height > 08 val wide: Boolean get() = valid && width.toDouble() / height >= 1.359 val tall: Boolean get() = valid && height.toDouble() / width >= 2.510}11data class ReaderViewport(val widthDp: Int = 0, val heightDp: Int = 0)12data class ResolvedReaderLayout(val mode: DbReaderMode, val direction: ReaderDirection, val pairPages: Boolean)1314internal const val AUTOMATIC_LAYOUT_VERSION = 115internal const val AUTOMATIC_LAYOUT_SAMPLE_COUNT = 316internal const val AUTOMATIC_LAYOUT_SAMPLE_TIMEOUT = 1500L1718/** At least two valid pages for multi-page chapters; mixed samples keep the user's fallback. */19fun automaticWebtoonSample(sample: List<ReaderPageShape>, chapterPageCount: Int): Boolean {20 val valid = sample.filter { it.valid }.take(AUTOMATIC_LAYOUT_SAMPLE_COUNT)21 val minimum = if (chapterPageCount == 1) 1 else 222 return valid.size >= minimum && valid.count { it.tall }.toDouble() / valid.size >= 0.7523}2425/** Direction comes only from explicit metadata or the fallback, never image aspect or language. */26fun resolveAutomaticReaderLayout(27 fallback: DbReaderMode,28 webtoon: Boolean,29 viewport: ReaderViewport,30 explicitDirection: ReaderDirection? = null,31 pairingOverride: Boolean? = null,32 settled: Boolean = true,33): ResolvedReaderLayout {34 val direction = explicitDirection ?: if (fallback.isRightToLeft()) ReaderDirection.Rtl else ReaderDirection.Ltr35 val mode = if (webtoon) DbReaderMode.Webtoon else when {36 explicitDirection != null && fallback.isHorizontalContinuous() -> if (direction == ReaderDirection.Rtl) DbReaderMode.HorizontalContinuousRtl else DbReaderMode.HorizontalContinuous37 explicitDirection != null && (fallback == DbReaderMode.Rtl || fallback == DbReaderMode.Ltr) -> if (direction == ReaderDirection.Rtl) DbReaderMode.Rtl else DbReaderMode.Ltr38 else -> fallback39 }40 val pair = pairingOverride ?: (settled && mode.isPaged() && viewport.widthDp >= 840 && viewport.widthDp > viewport.heightDp)41 return ResolvedReaderLayout(mode, direction, pair)42}4344internal fun automaticLayoutHasWebtoonMetadata(genres: List<String>): Boolean = genres.any {45 it.trim().lowercase() in setOf("webtoon", "long strip", "long-strip")46}47