From 9e2768968bca6cdf3fa4c31bfa7f7cdcbb7fa34d Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Tue, 16 Aug 2022 12:23:44 +0200 Subject: [PATCH] Improve BottomSheet drag handling logic --- .../vimusic/ui/components/BottomSheet.kt | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/BottomSheet.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/BottomSheet.kt index f62668d..1277622 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/BottomSheet.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/BottomSheet.kt @@ -39,7 +39,6 @@ import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.Velocity -import androidx.compose.ui.unit.dp import kotlin.math.absoluteValue import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.coroutineScope @@ -62,35 +61,29 @@ fun BottomSheet( IntOffset(x = 0, y = y) } .pointerInput(state) { - var initialValue = 0.dp val velocityTracker = VelocityTracker() detectVerticalDragGestures( - onDragStart = { - initialValue = state.value - }, onVerticalDrag = { change, dragAmount -> velocityTracker.addPointerInputChange(change) state.dispatchRawDelta(dragAmount) }, onDragCancel = { velocityTracker.resetTracking() - state.snapTo(initialValue) + state.snapTo(state.collapsedBound) }, onDragEnd = { - val velocity = velocityTracker.calculateVelocity().y + val velocity = -velocityTracker.calculateVelocity().y velocityTracker.resetTracking() - if (velocity.absoluteValue > 250 && initialValue != state.value) { - when (initialValue) { - state.expandedBound -> state.collapse() - state.collapsedBound -> if (initialValue > state.value && onDismiss != null) { - state.dismiss() - onDismiss.invoke() - } else { - state.expand() - } - else -> state.expand() + if (velocity > 250) { + state.expand() + } else if (velocity < -250) { + if (state.value < state.collapsedBound && onDismiss != null) { + state.dismiss() + onDismiss.invoke() + } else { + state.collapse() } } else { val l0 = state.dismissedBound @@ -109,12 +102,9 @@ fun BottomSheet( } in l1..l2 -> state.collapse() in l2..l3 -> state.expand() - else -> { - - } + else -> Unit } } - } ) }