ReaderRendererSync.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/reader/ReaderRendererSync.kt
1package eu.friedcutlet.yokan.ui.screens.reader23import androidx.compose.animation.core.CubicBezierEasing4import androidx.compose.animation.core.tween5import androidx.compose.foundation.lazy.LazyListState6import androidx.compose.foundation.pager.PagerState7import androidx.compose.runtime.Composable8import androidx.compose.runtime.LaunchedEffect9import androidx.compose.runtime.getValue10import androidx.compose.runtime.mutableStateOf11import androidx.compose.runtime.referentialEqualityPolicy12import androidx.compose.runtime.remember13import androidx.compose.runtime.rememberUpdatedState14import androidx.compose.runtime.setValue15import androidx.compose.runtime.snapshotFlow16import eu.friedcutlet.yokan.database.DbReaderMode1718private val ReaderPageTransitionEasing = CubicBezierEasing(0.25f, 0.1f, 0.25f, 1f)1920/**21 * Keeps the active renderer (pager, vertical or horizontal list) and the ViewModel's stream22 * position in step. A ViewModel move scrolls the renderer, jumping directly on initial23 * content/mode replacement and animating page turns only when [animateTransitions] allows it.24 * A user scroll reports the new position through [onPageChanged], preserving the logical page25 * within a regrouped spread.26 */27@Composable28internal fun ReaderRendererSync(29 displayUnits: List<ReaderDisplayUnit>,30 currentStreamIndex: Int,31 animatePageChange: Boolean,32 mode: DbReaderMode,33 animateTransitions: Boolean,34 pagerState: PagerState,35 verticalState: LazyListState,36 horizontalState: LazyListState,37 onPageChanged: (Int) -> Unit,38) {39 var lastSynchronizedUnits by remember {40 mutableStateOf<List<ReaderDisplayUnit>?>(null, referentialEqualityPolicy())41 }42 var synchronizingRenderer by remember { mutableStateOf(false) }43 var synchronizedMode by remember { mutableStateOf<DbReaderMode?>(null) }44 val logicalStreamIndex by rememberUpdatedState(currentStreamIndex)45 val rendererIndex: () -> Int = when {46 mode.isPaged() -> pagerState::currentPage47 mode.isVerticalLike() -> verticalState::firstVisibleItemIndex48 else -> horizontalState::firstVisibleItemIndex49 }5051 LaunchedEffect(displayUnits, currentStreamIndex, mode, animateTransitions) {52 if (displayUnits.isEmpty()) return@LaunchedEffect53 val target = displayUnitIndexForStreamIndex(displayUnits, currentStreamIndex)54 val animate = animateTransitions && animatePageChange && lastSynchronizedUnits === displayUnits55 if (rendererIndex() != target) {56 synchronizingRenderer = true57 try {58 when {59 mode.isPaged() -> if (animate) {60 pagerState.animateScrollToPage(61 page = target,62 animationSpec = tween(63 durationMillis = ReaderPageTransitionDurationMillis,64 easing = ReaderPageTransitionEasing,65 ),66 )67 } else {68 pagerState.scrollToPage(target)69 }70 mode.isVerticalLike() -> verticalState.scrollTo(target, animate)71 else -> horizontalState.scrollTo(target, animate)72 }73 } finally {74 synchronizingRenderer = false75 }76 }77 lastSynchronizedUnits = displayUnits78 synchronizedMode = mode79 }8081 LaunchedEffect(pagerState, verticalState, horizontalState, displayUnits, mode) {82 snapshotFlow(rendererIndex).collect { unitIndex ->83 if (!synchronizingRenderer && synchronizedMode == mode && lastSynchronizedUnits == displayUnits) {84 readerRendererPageChange(displayUnits, unitIndex, logicalStreamIndex)?.let(onPageChanged)85 }86 }87 }88}8990private suspend fun LazyListState.scrollTo(index: Int, animate: Boolean) {91 if (animate) animateScrollToItem(index) else scrollToItem(index)92}93