Skip to content
Snippets Groups Projects
Commit a2e775d3 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Android (Google) Code Review
Browse files

Merge "Add MutableSTLState.snapToScene()" into main

parents 7ede1a66 e4a75f90
No related branches found
No related tags found
No related merge requests found
......@@ -117,6 +117,9 @@ sealed interface MutableSceneTransitionLayoutState : SceneTransitionLayoutState
coroutineScope: CoroutineScope,
transitionKey: TransitionKey? = null,
): TransitionState.Transition?
/** Immediately snap to the given [scene]. */
fun snapToScene(scene: SceneKey)
}
/**
......@@ -745,6 +748,17 @@ internal class MutableSceneTransitionLayoutStateImpl(
override fun CoroutineScope.onChangeScene(scene: SceneKey) {
setTargetScene(scene, coroutineScope = this)
}
override fun snapToScene(scene: SceneKey) {
// Force finish all transitions.
while (currentTransitions.isNotEmpty()) {
val transition = transitionStates[0] as TransitionState.Transition
finishTransition(transition, transition.currentScene)
}
check(transitionStates.size == 1)
transitionStates[0] = TransitionState.Idle(scene)
}
}
private const val TAG = "SceneTransitionLayoutState"
......
......@@ -635,4 +635,19 @@ class SceneTransitionLayoutStateTest {
Log.setWtfHandler(originalHandler)
}
}
@Test
fun snapToScene() = runMonotonicClockTest {
val state = MutableSceneTransitionLayoutState(SceneA)
// Transition to B.
state.setTargetScene(SceneB, coroutineScope = this)
val transition = assertThat(state.transitionState).isTransition()
assertThat(transition).hasCurrentScene(SceneB)
// Snap to C.
state.snapToScene(SceneC)
assertThat(state.transitionState).isIdle()
assertThat(state.transitionState).hasCurrentScene(SceneC)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment