Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class BottomSheetState(
private val animatable: Animatable<Dp, AnimationVector1D>,
private val onAnchorChanged: (Int) -> Unit,
val collapsedBound: Dp,
private val launchAnimation: (suspend () -> Unit) -> Unit = { block ->
coroutineScope.launch { block() }
},
) : DraggableState by draggableState {
val dismissedBound: Dp
get() = animatable.lowerBound!!
Expand Down Expand Up @@ -182,14 +185,14 @@ class BottomSheetState(

fun collapse(animationSpec: AnimationSpec<Dp>) {
onAnchorChanged(collapsedAnchor)
coroutineScope.launch {
launchAnimation {
animatable.animateTo(collapsedBound, animationSpec)
}
}

fun expand(animationSpec: AnimationSpec<Dp>) {
onAnchorChanged(expandedAnchor)
coroutineScope.launch {
launchAnimation {
animatable.animateTo(animatable.upperBound!!, animationSpec)
}
}
Expand All @@ -212,7 +215,7 @@ class BottomSheetState(

fun dismiss() {
onAnchorChanged(dismissedAnchor)
coroutineScope.launch {
launchAnimation {
animatable.animateTo(animatable.lowerBound!!)
}
}
Expand Down Expand Up @@ -330,22 +333,45 @@ fun rememberBottomSheetState(
var previousAnchor by rememberSaveable {
mutableIntStateOf(initialAnchor)
}

val activeAnimations = remember {
mutableIntStateOf(0)
}

val animatable = remember {
Animatable(0.dp, Dp.VectorConverter)
}

return remember(dismissedBound, expandedBound, collapsedBound, coroutineScope) {
val launchAnimation: (suspend () -> Unit) -> Unit = { block ->
coroutineScope.launch {
activeAnimations.intValue++
try {
block()
} finally {
activeAnimations.intValue--
}
}
}

fun animateToAnchor() {
val initialValue = when (previousAnchor) {
expandedAnchor -> expandedBound
collapsedAnchor -> collapsedBound
dismissedAnchor -> dismissedBound
else -> error("Unknown BottomSheet anchor")
}

animatable.updateBounds(dismissedBound.coerceAtMost(expandedBound), expandedBound)
coroutineScope.launch {
launchAnimation {
animatable.animateTo(initialValue, NavigationBarAnimationSpec)
}
}

return remember(dismissedBound, expandedBound, collapsedBound, coroutineScope) {
animatable.updateBounds(dismissedBound.coerceAtMost(expandedBound), expandedBound)

if (activeAnimations.intValue == 0) {
animateToAnchor()
}

BottomSheetState(
draggableState = DraggableState { delta ->
Expand All @@ -356,7 +382,8 @@ fun rememberBottomSheetState(
onAnchorChanged = { previousAnchor = it },
coroutineScope = coroutineScope,
animatable = animatable,
collapsedBound = collapsedBound
collapsedBound = collapsedBound,
launchAnimation = launchAnimation,
)
}
}
Loading