package eu.friedcutlet.yokan.ui.screens.reader import androidx.compose.animation.core.LinearEasing import androidx.compose.animation.core.tween import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.scaleIn import androidx.compose.animation.scaleOut import androidx.compose.foundation.gestures.animateScrollBy import androidx.compose.foundation.gestures.scrollBy import androidx.compose.foundation.ScrollState import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyListState import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Pause import androidx.compose.material.icons.filled.PlayArrow import androidx.compose.material3.FloatingActionButton import androidx.compose.material3.Icon import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.MaterialTheme import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.snapshotFlow import androidx.compose.runtime.withFrameNanos import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import kotlinx.coroutines.delay import kotlinx.coroutines.CancellationException import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.isActive import kotlinx.coroutines.flow.first import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.reader_scroll_pause import yokan.composeapp.generated.resources.reader_scroll_play import yokan.composeapp.generated.resources.reader_hold_speed import yokan.composeapp.generated.resources.reader_hold_paused import kotlin.math.roundToInt @Composable internal fun BoxScope.ReaderHoldScrollIndicator(hold: ReaderHoldScroll) { if (hold.phase == ReaderHoldPhase.Idle) return Surface( modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 64.dp), shape = RoundedCornerShape(50), color = MaterialTheme.colorScheme.secondaryContainer, ) { val multiplier = (hold.speed / hold.baseSpeed.coerceAtLeast(2f) * 10f).roundToInt() / 10f Text( text = if (hold.phase == ReaderHoldPhase.Holding) stringResource(Res.string.reader_hold_paused) else stringResource(Res.string.reader_hold_speed, multiplier.toString()), modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp), style = MaterialTheme.typography.labelLarge, ) } } /** * Floating play/pause FAB shown in continuous modes (vertical, webtoon, * horizontal continuous). Mirrors the Flutter `auto_scroll_button.dart`. * * Auto-scroll itself is driven by [DriveAutoScroll] below — kept separate so * the FAB stays a pure render-only composable. */ @Composable internal fun BoxScope.ReaderAutoScrollFab( visible: Boolean, isPlaying: Boolean, onToggle: () -> Unit, bottomPadding: Dp = 108.dp, ) { AnimatedVisibility( visible = visible, enter = scaleIn(), exit = scaleOut(), modifier = Modifier.align(Alignment.BottomEnd).padding(end = 18.dp, bottom = bottomPadding), ) { FloatingActionButton(onClick = onToggle) { if (isPlaying) Icon(Icons.Filled.Pause, contentDescription = stringResource(Res.string.reader_scroll_pause)) else Icon(Icons.Filled.PlayArrow, contentDescription = stringResource(Res.string.reader_scroll_play)) } } } /** * One frame-driven controller for ordinary and temporary hold scrolling. Speed changes * do not restart it. At a loading boundary it suspends until the extent changes; at the * confirmed final transition it relinquishes ownership. The old loop advanced one offset * over a 100 ms animation after a 100 ms delay, hence five offsets per second. */ @Composable internal fun DriveAutoScroll( state: LazyListState, isPlaying: Boolean, pageOffset: Float, endOfContent: Boolean = false, onFinished: () -> Unit = {}, ) { val playing by rememberUpdatedState(isPlaying) val offset by rememberUpdatedState(pageOffset.coerceIn(0f, 60f)) val terminal by rememberUpdatedState(endOfContent) val finished by rememberUpdatedState(onFinished) LaunchedEffect(state, isPlaying) { if (!playing) return@LaunchedEffect try { var previous = withFrameNanos { it } while (playing) { if (!state.canScrollForward) { if (terminal && state.layoutInfo.totalItemsCount > 0) { finished() break } snapshotFlow { state.canScrollForward || (terminal && state.layoutInfo.totalItemsCount > 0) || !playing }.first { it } previous = withFrameNanos { it } continue } if (offset <= 0f) { snapshotFlow { offset > 0f || !playing }.first { it } previous = withFrameNanos { it } continue } val now = withFrameNanos { it } val seconds = ((now - previous) / 1_000_000_000f).coerceIn(0f, 0.05f) if (playing) state.scrollBy(offset * 5f * seconds) previous = now } } catch (cancelled: CancellationException) { // A user drag can interrupt the list's scroll mutation while this effect is // still active. Disposing/replacing the effect must not stop a newer controller. if (currentCoroutineContext().isActive) finished() throw cancelled } } } /** ScrollState counterpart used by the continuous novel reader. */ @Composable internal fun DriveAutoScroll( state: ScrollState, isPlaying: Boolean, pageOffset: Float, ) { val playing by rememberUpdatedState(isPlaying) val offset by rememberUpdatedState(pageOffset.coerceIn(2f, 30f)) LaunchedEffect(state, playing, offset) { if (!playing) return@LaunchedEffect while (playing) { delay(100L) if (!playing) break if (state.value < state.maxValue && state.maxValue != Int.MAX_VALUE) { state.animateScrollBy( value = offset, animationSpec = tween(durationMillis = 100, easing = LinearEasing), ) } } } }