Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -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"
+15 −0
Original line number Diff line number Diff line
@@ -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)
    }
}