Skip to content

Commit

Permalink
fix: fling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4rl3x committed Apr 17, 2024
1 parent c0d9134 commit cdabf28
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bottom-drawer-scaffold/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apply from: '../buildCompose.gradle'

ext {
PUBLISH_GROUP_ID = 'de.charlex.compose'
PUBLISH_VERSION = '2.0.0-alpha01'
PUBLISH_VERSION = '2.0.0-alpha02'
PUBLISH_ARTIFACT_ID = 'bottom-drawer-scaffold'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.unit.Velocity

@OptIn(ExperimentalFoundationApi::class)
internal fun <T> AnchoredDraggableState<T>.createPreUpPostDownNestedScrollConnection(): NestedScrollConnection {
internal fun <T> AnchoredDraggableState<T>.createPreUpPostDownNestedScrollConnection(topOffset: Int): NestedScrollConnection {
return object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
val delta = available.toFloat()
Expand All @@ -33,8 +33,7 @@ internal fun <T> AnchoredDraggableState<T>.createPreUpPostDownNestedScrollConnec

override suspend fun onPreFling(available: Velocity): Velocity {
val toFling = Offset(available.x, available.y).toFloat()
// return if (toFling < 0 && offset.value > minBound) {
return if (toFling < 0) {
return if (toFling < 0 && offset > topOffset) {
settle(velocity = toFling)
// since we go to the anchor with tween settling, consume all for the best UX
available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.collapse
import androidx.compose.ui.semantics.expand
Expand All @@ -61,14 +62,15 @@ fun rememberBottomDrawerScaffoldState(
confirmValueChange: (newValue: BottomDrawerValue) -> Boolean = { true }
): AnchoredDraggableState<BottomDrawerValue> {
val density = LocalDensity.current
val maxHeight = with(density) { LocalConfiguration.current.screenHeightDp.dp.toPx() }
return remember {
AnchoredDraggableState(
initialValue = initialValue,
positionalThreshold = positionalThreshold,
velocityThreshold = velocityThreshold ?: { with(density) { 100.dp.toPx() } },
animationSpec = animationSpec,
anchors = DraggableAnchors {
BottomDrawerValue.Collapsed at 0f
BottomDrawerValue.Collapsed at maxHeight
BottomDrawerValue.Expanded at 0f
},
confirmValueChange = confirmValueChange
Expand All @@ -86,9 +88,7 @@ fun BottomDrawerScaffold(
bottomBar: @Composable (() -> Unit)? = null,
gesturesEnabled: Boolean = true,
drawerModifier: Modifier = Modifier,
// snackbarHostState: SnackbarHostState,
snackbarHost: @Composable () -> Unit = {},
// snackbarHost: @Composable (SnackbarHostState) -> Unit = { SnackbarHost(it) },
floatingActionButton: @Composable (() -> Unit)? = null,
floatingActionButtonPosition: FabPosition = FabPosition.End,
drawerGesturesEnabled: Boolean? = null,
Expand Down Expand Up @@ -143,7 +143,7 @@ fun BottomDrawerScaffold(

val anchoredDraggableModifier = Modifier
.nestedScroll(
bottomDrawerScaffoldState.createPreUpPostDownNestedScrollConnection()
bottomDrawerScaffoldState.createPreUpPostDownNestedScrollConnection(topPadding.toInt())
)
.anchoredDraggable(
state = bottomDrawerScaffoldState,
Expand Down

0 comments on commit cdabf28

Please sign in to comment.