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

Commit c90380ae authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Android (Google) Code Review
Browse files

Merge "Add Transition.progressTo(ContentKey)" into main

parents c360dda0 11beddbf
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) }
    }
}