package eu.friedcutlet.yokan.ui.navigation import androidx.compose.animation.core.Spring import androidx.compose.animation.core.animate import androidx.compose.animation.core.spring import androidx.compose.foundation.gestures.awaitEachGesture import androidx.compose.foundation.gestures.awaitFirstDown import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.input.pointer.PointerEventPass import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.util.VelocityTracker import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.launch import kotlin.math.abs /** Gesture state belongs to one entry. Its offset survives until that entry finishes leaving. */ internal class InteractiveBackGesture(private val scope: CoroutineScope) { var active by mutableStateOf(false) private set var progress by mutableFloatStateOf(0f) private set private var settling: Job? = null private var disposed = false fun begin(): Boolean { if (disposed || active || settling?.isActive == true) return false active = true return true } fun drag(pixels: Float, width: Float) { progress = (progress + pixels / width).coerceIn(0f, 1f) } fun finish(velocity: Float, cancelled: Boolean, onComplete: () -> Unit) { if (disposed || !active || settling?.isActive == true) return val complete = !cancelled && shouldCompleteBack(progress, velocity) settling = scope.launch { animate( initialValue = progress, targetValue = if (complete) 1f else 0f, initialVelocity = if (cancelled) 0f else velocity, animationSpec = spring(dampingRatio = Spring.DampingRatioNoBouncy, stiffness = 700f, visibilityThreshold = 0.001f), ) { value, _ -> progress = value.coerceIn(0f, 1f) } if (complete) { // Do not reset to zero here: the outgoing entry is still composed for this frame. onComplete() } else { active = false } } } fun dispose() { disposed = true; settling?.cancel() } } /** Velocity is in screen widths/second, so the same gesture works on phones and tablets. */ internal fun shouldCompleteBack(progress: Float, velocity: Float): Boolean = if (abs(velocity) >= 1f) velocity > 0f else progress >= 0.5f internal fun Modifier.interactiveBackGesture( gesture: InteractiveBackGesture, edgeWidth: Float, canStart: () -> Boolean, onComplete: () -> Unit, ): Modifier = pointerInput(gesture, edgeWidth) { awaitEachGesture { val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial) // Check the original DOWN, not the position after touch slop. Leave other swipes alone. if (down.position.x > edgeWidth || !canStart() || !gesture.begin()) return@awaitEachGesture val width = size.width.toFloat().coerceAtLeast(1f) val tracker = VelocityTracker().apply { addPosition(down.uptimeMillis, down.position) } var dragging = false var cancelled = true var totalX = 0f var totalY = 0f try { while (true) { val event = awaitPointerEvent(PointerEventPass.Initial) val change = event.changes.firstOrNull { it.id == down.id } ?: break tracker.addPosition(change.uptimeMillis, change.position) if (change.isConsumed || event.changes.count { it.pressed } > 1) break if (!change.pressed) { cancelled = !dragging break } val delta = change.position - change.previousPosition totalX += delta.x totalY += delta.y if (!dragging) { if (abs(totalY) > viewConfiguration.touchSlop || totalX < -viewConfiguration.touchSlop) break if (totalX <= viewConfiguration.touchSlop) continue dragging = true gesture.drag(totalX - viewConfiguration.touchSlop, width) } else { gesture.drag(delta.x, width) } change.consume() } } finally { gesture.finish(tracker.calculateVelocity().x / width, cancelled, onComplete) } } }