package eu.friedcutlet.yokan.ui.screens.reader import androidx.compose.animation.core.CubicBezierEasing import androidx.compose.animation.core.tween import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.pager.PagerState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.referentialEqualityPolicy import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshotFlow import eu.friedcutlet.yokan.database.DbReaderMode private val ReaderPageTransitionEasing = CubicBezierEasing(0.25f, 0.1f, 0.25f, 1f) /** * Keeps the active renderer (pager, vertical or horizontal list) and the ViewModel's stream * position in step. A ViewModel move scrolls the renderer, jumping directly on initial * content/mode replacement and animating page turns only when [animateTransitions] allows it. * A user scroll reports the new position through [onPageChanged], preserving the logical page * within a regrouped spread. */ @Composable internal fun ReaderRendererSync( displayUnits: List, currentStreamIndex: Int, animatePageChange: Boolean, mode: DbReaderMode, animateTransitions: Boolean, pagerState: PagerState, verticalState: LazyListState, horizontalState: LazyListState, onPageChanged: (Int) -> Unit, ) { var lastSynchronizedUnits by remember { mutableStateOf?>(null, referentialEqualityPolicy()) } var synchronizingRenderer by remember { mutableStateOf(false) } var synchronizedMode by remember { mutableStateOf(null) } val logicalStreamIndex by rememberUpdatedState(currentStreamIndex) val rendererIndex: () -> Int = when { mode.isPaged() -> pagerState::currentPage mode.isVerticalLike() -> verticalState::firstVisibleItemIndex else -> horizontalState::firstVisibleItemIndex } LaunchedEffect(displayUnits, currentStreamIndex, mode, animateTransitions) { if (displayUnits.isEmpty()) return@LaunchedEffect val target = displayUnitIndexForStreamIndex(displayUnits, currentStreamIndex) val animate = animateTransitions && animatePageChange && lastSynchronizedUnits === displayUnits if (rendererIndex() != target) { synchronizingRenderer = true try { when { mode.isPaged() -> if (animate) { pagerState.animateScrollToPage( page = target, animationSpec = tween( durationMillis = ReaderPageTransitionDurationMillis, easing = ReaderPageTransitionEasing, ), ) } else { pagerState.scrollToPage(target) } mode.isVerticalLike() -> verticalState.scrollTo(target, animate) else -> horizontalState.scrollTo(target, animate) } } finally { synchronizingRenderer = false } } lastSynchronizedUnits = displayUnits synchronizedMode = mode } LaunchedEffect(pagerState, verticalState, horizontalState, displayUnits, mode) { snapshotFlow(rendererIndex).collect { unitIndex -> if (!synchronizingRenderer && synchronizedMode == mode && lastSynchronizedUnits == displayUnits) { readerRendererPageChange(displayUnits, unitIndex, logicalStreamIndex)?.let(onPageChanged) } } } } private suspend fun LazyListState.scrollTo(index: Int, animate: Boolean) { if (animate) animateScrollToItem(index) else scrollToItem(index) }