Skip to content

Commit

Permalink
Merge pull request #806 from arkivanov/cancel-predictive-back-animati…
Browse files Browse the repository at this point in the history
…on-when-stack-changed

Cancel predictive back gesture when stack changed (experimental animation API)
  • Loading branch information
arkivanov authored Nov 7, 2024
2 parents 00bf7ee + e4c0d00 commit b843d5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal class DefaultStackAnimation<C : Any, T : Any>(

if (updateItems) {
val newItems = getAnimationItems(newStack = currentStack, oldStack = oldStack)
if (items.size == 1) {
if ((items.size == 1) || items.values.last().transitionState.isSeekable()) {
items = newItems
} else {
nextItems = newItems
Expand Down Expand Up @@ -401,3 +401,6 @@ private fun TransitionState<*>.isIdle(): Boolean =
is SeekableTransitionState -> false
else -> false
}

private fun TransitionState<*>.isSeekable(): Boolean =
this is SeekableTransitionState
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,29 @@ class PredictiveBackGestureTest {
composeRule.onNodeWithText("3").assertDoesNotExist()
}

@Test
fun GIVEN_gesture_started_WHEN_stack_popped_THEN_gesture_cancelled() {
var stack by mutableStateOf(stack("1", "2"))
val animation = DefaultStackAnimation(animator = fade(), onBack = { stack = stack.dropLast() },)

composeRule.setContent {
animation(stack, Modifier) {
Text(text = it.configuration)
}
}

backDispatcher.startPredictiveBack(BackEvent(progress = 0F))
composeRule.waitForIdle()
backDispatcher.progressPredictiveBack(BackEvent(progress = 0.5F))
composeRule.waitForIdle()

stack = stack.dropLast()
composeRule.mainClock.autoAdvance = false
composeRule.mainClock.advanceTimeByFrame()

composeRule.onNodeWithText("2").assertTestTagToRootDoesNotExist { it.startsWith(TEST_TAG_PREFIX) }
}

@Test
fun GIVEN_gesture_started_WHEN_stack_pushed_and_back_THEN_new_child_popped_and_gesture_finished() {
var stack by mutableStateOf(stack("1", "2"))
Expand Down Expand Up @@ -471,6 +494,7 @@ class PredictiveBackGestureTest {

private fun DefaultStackAnimation(
predictiveBackAnimatable: (initialBackEvent: BackEvent) -> PredictiveBackAnimatable? = ::TestAnimatable,
animator: StackAnimator? = null,
onBack: () -> Unit,
): DefaultStackAnimation<String, String> =
DefaultStackAnimation(
Expand All @@ -482,7 +506,7 @@ class PredictiveBackGestureTest {
animatable = predictiveBackAnimatable,
)
},
selector = { _, _, _ -> null },
selector = { _, _, _ -> animator },
)

private fun stack(configs: List<String>): ChildStack<String, String> =
Expand Down

0 comments on commit b843d5a

Please sign in to comment.