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

Commit 11beddbf authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Add Transition.progressTo(ContentKey)

This CL adds convenience method Transition.progressTo(), which I found
can be useful when used together with isTransitioningFromOrTo() or
isTransitioningBetween().

Bug: 373799480
Test: atest SceneTransitionLayoutStateTest
Flag: com.android.systemui.scene_container
Change-Id: I797fb1c69808d03ffb4b97fbfe2687819ede68bb
parent 7f60633a
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -311,6 +311,22 @@ sealed interface TransitionState {
            return fromContent == content || toContent == content
        }

        /**
         * Return [progress] if [content] is equal to [toContent], `1f - progress` if [content] is
         * equal to [fromContent], and throw otherwise.
         */
        fun progressTo(content: ContentKey): Float {
            return when (content) {
                toContent -> progress
                fromContent -> 1f - progress
                else ->
                    throw IllegalArgumentException(
                        "content ($content) should be either toContent ($toContent) or " +
                            "fromContent ($fromContent)"
                    )
            }
        }

        /** Run this transition and return once it is finished. */
        abstract suspend fun run()

+9 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertThrows
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@@ -768,4 +769,12 @@ class SceneTransitionLayoutStateTest {
        assertThat(state.transitionState).isIdle()
        assertThat(state.transitionState).hasCurrentScene(SceneC)
    }

    @Test
    fun transition_progressTo() {
        val transition = transition(from = SceneA, to = SceneB, progress = { 0.2f })
        assertThat(transition.progressTo(SceneB)).isEqualTo(0.2f)
        assertThat(transition.progressTo(SceneA)).isEqualTo(1f - 0.2f)
        assertThrows(IllegalArgumentException::class.java) { transition.progressTo(SceneC) }
    }
}