From ddb6c40c768b653f5de2a67d8f091c2c04274a4c Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Tue, 30 Aug 2022 11:34:11 +0200 Subject: [PATCH] Fix BottomSheet nestedScrollConnection buggy fling logic --- .../vimusic/ui/components/BottomSheet.kt | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 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 5e136b6..f30c980 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 kotlin.math.absoluteValue import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch @@ -240,14 +239,23 @@ class BottomSheetState( override suspend fun onPreFling(available: Velocity): Velocity { if (isTopReached) { + val velocity = -available.y coroutineScope { - if (available.y.absoluteValue > 1000) { + if (velocity > 250) { + expand() + } else if (velocity < -250) { collapse() } else { - if (animatable.upperBound!! - value > value - collapsedBound) { - collapse() - } else { - expand() + val l0 = dismissedBound + val l1 = (collapsedBound - dismissedBound) / 2 + val l2 = (expandedBound - collapsedBound) / 2 + val l3 = expandedBound + + when (value) { + in l0..l1 -> collapse() + in l1..l2 -> collapse() + in l2..l3 -> expand() + else -> Unit } } }