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

Commit ff9ad4ed authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Revert "[flexiglass] Added isUserInputOngoing to transition model"

This reverts commit 3770e4e2.

Reason for revert: b/303321199#comment3

Change-Id: Ia9427c81a4e095fe462115d37371072b1b88b230
parent 67863693
Loading
Loading
Loading
Loading
+4 −19
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ private fun CoroutineScope.animate(
) {
    val fromScene = layoutImpl.state.transitionState.currentScene
    val isUserInput =
        (layoutImpl.state.transitionState as? TransitionState.Transition)?.isInitiatedByUserInput
        (layoutImpl.state.transitionState as? TransitionState.Transition)?.isUserInputDriven
            ?: false

    val animationSpec = layoutImpl.transitions.transitionSpec(fromScene, target).spec
@@ -119,23 +119,9 @@ private fun CoroutineScope.animate(
    val targetProgress = if (reversed) 0f else 1f
    val transition =
        if (reversed) {
            OneOffTransition(
                fromScene = target,
                toScene = fromScene,
                currentScene = target,
                isUserInput,
                isUserInputOngoing = false,
                animatable,
            )
            OneOffTransition(target, fromScene, currentScene = target, isUserInput, animatable)
        } else {
            OneOffTransition(
                fromScene = fromScene,
                toScene = target,
                currentScene = target,
                isUserInput,
                isUserInputOngoing = false,
                animatable,
            )
            OneOffTransition(fromScene, target, currentScene = target, isUserInput, animatable)
        }

    // Change the current layout state to use this new transition.
@@ -156,8 +142,7 @@ private class OneOffTransition(
    override val fromScene: SceneKey,
    override val toScene: SceneKey,
    override val currentScene: SceneKey,
    override val isInitiatedByUserInput: Boolean,
    override val isUserInputOngoing: Boolean,
    override val isUserInputDriven: Boolean,
    private val animatable: Animatable<Float, AnimationVector1D>,
) : TransitionState.Transition {
    override val progress: Float
+2 −10
Original line number Diff line number Diff line
@@ -52,14 +52,7 @@ sealed class ObservableTransitionState {
         * scene, this value will remain true after the pointer is no longer touching the screen and
         * will be true in any transition created to animate back to the original position.
         */
        val isInitiatedByUserInput: Boolean,

        /**
         * Whether user input is currently driving the transition. For example, if a user is
         * dragging a pointer, this emits true. Once they lift their finger, this emits false while
         * the transition completes/settles.
         */
        val isUserInputOngoing: Flow<Boolean>,
        val isUserInputDriven: Boolean,
    ) : ObservableTransitionState()
}

@@ -80,8 +73,7 @@ fun SceneTransitionLayoutState.observableTransitionState(): Flow<ObservableTrans
                            fromScene = state.fromScene,
                            toScene = state.toScene,
                            progress = snapshotFlow { state.progress },
                            isInitiatedByUserInput = state.isInitiatedByUserInput,
                            isUserInputOngoing = snapshotFlow { state.isUserInputOngoing },
                            isUserInputDriven = state.isUserInputDriven,
                        )
                    }
                }
+1 −4
Original line number Diff line number Diff line
@@ -70,9 +70,6 @@ sealed interface TransitionState {
        val progress: Float

        /** Whether the transition was triggered by user input rather than being programmatic. */
        val isInitiatedByUserInput: Boolean

        /** Whether user input is currently driving the transition. */
        val isUserInputOngoing: Boolean
        val isUserInputDriven: Boolean
    }
}
+31 −31
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ internal fun Modifier.swipeToScene(
    // swipe in the other direction.
    val startDragImmediately =
        state == transition &&
            !transition.isUserInputOngoing &&
            transition.isAnimatingOffset &&
            !currentScene.shouldEnableSwipes(orientation.opposite())

    // The velocity threshold at which the intent of the user is to swipe up or down. It is the same
@@ -126,7 +126,7 @@ private class SwipeTransition(initialScene: Scene) : TransitionState.Transition

    override val progress: Float
        get() {
            val offset = if (isUserInputOngoing) dragOffset else offsetAnimatable.value
            val offset = if (isAnimatingOffset) offsetAnimatable.value else dragOffset
            if (distance == 0f) {
                // This can happen only if fromScene == toScene.
                error(
@@ -137,15 +137,16 @@ private class SwipeTransition(initialScene: Scene) : TransitionState.Transition
            return offset / distance
        }

    override val isInitiatedByUserInput = true

    var _isUserInputOngoing by mutableStateOf(false)
    override val isUserInputOngoing: Boolean
        get() = _isUserInputOngoing
    override val isUserInputDriven = true

    /** The current offset caused by the drag gesture. */
    var dragOffset by mutableFloatStateOf(0f)

    /**
     * Whether the offset is animated (the user lifted their finger) or if it is driven by gesture.
     */
    var isAnimatingOffset by mutableStateOf(false)

    /** The animatable used to animate the offset once the user lifted its finger. */
    val offsetAnimatable = Animatable(0f, visibilityThreshold = OffsetVisibilityThreshold)

@@ -208,11 +209,9 @@ private fun onDragStarted(
    transition: SwipeTransition,
    orientation: Orientation,
) {
    transition._isUserInputOngoing = true

    if (layoutImpl.state.transitionState == transition) {
        // This [transition] was already driving the animation: simply take over it.
        if (!transition.isUserInputOngoing) {
        if (transition.isAnimatingOffset) {
            // Stop animating and start from where the current offset. Setting the animation job to
            // `null` will effectively cancel the animation.
            transition.stopOffsetAnimation()
@@ -457,10 +456,10 @@ private fun CoroutineScope.animateOffset(
) {
    transition.startOffsetAnimation {
        launch {
            if (transition.isUserInputOngoing) {
                if (!transition.isAnimatingOffset) {
                    transition.offsetAnimatable.snapTo(transition.dragOffset)
                }
            transition._isUserInputOngoing = false
                transition.isAnimatingOffset = true

                transition.offsetAnimatable.animateTo(
                    targetOffset,
@@ -480,6 +479,7 @@ private fun CoroutineScope.animateOffset(
                    layoutImpl.state.transitionState = TransitionState.Idle(targetScene)
                }
            }
            .also { it.invokeOnCompletion { transition.isAnimatingOffset = false } }
    }
}

+4 −8
Original line number Diff line number Diff line
@@ -122,8 +122,7 @@ class SwipeToSceneTest {
        assertThat(transition.toScene).isEqualTo(TestScenes.SceneB)
        assertThat(transition.currentScene).isEqualTo(TestScenes.SceneA)
        assertThat(transition.progress).isEqualTo(55.dp / LayoutWidth)
        assertThat(transition.isInitiatedByUserInput).isTrue()
        assertThat(transition.isUserInputOngoing).isTrue()
        assertThat(transition.isUserInputDriven).isTrue()

        // Release the finger. We should now be animating back to A (currentScene = SceneA) given
        // that 55dp < positional threshold.
@@ -135,8 +134,7 @@ class SwipeToSceneTest {
        assertThat(transition.toScene).isEqualTo(TestScenes.SceneB)
        assertThat(transition.currentScene).isEqualTo(TestScenes.SceneA)
        assertThat(transition.progress).isEqualTo(55.dp / LayoutWidth)
        assertThat(transition.isInitiatedByUserInput).isTrue()
        assertThat(transition.isUserInputOngoing).isFalse()
        assertThat(transition.isUserInputDriven).isTrue()

        // Wait for the animation to finish. We should now be in scene A.
        rule.waitForIdle()
@@ -158,8 +156,7 @@ class SwipeToSceneTest {
        assertThat(transition.toScene).isEqualTo(TestScenes.SceneC)
        assertThat(transition.currentScene).isEqualTo(TestScenes.SceneA)
        assertThat(transition.progress).isEqualTo(56.dp / LayoutHeight)
        assertThat(transition.isInitiatedByUserInput).isTrue()
        assertThat(transition.isUserInputOngoing).isTrue()
        assertThat(transition.isUserInputDriven).isTrue()

        // Release the finger. We should now be animating to C (currentScene = SceneC) given
        // that 56dp >= positional threshold.
@@ -171,8 +168,7 @@ class SwipeToSceneTest {
        assertThat(transition.toScene).isEqualTo(TestScenes.SceneC)
        assertThat(transition.currentScene).isEqualTo(TestScenes.SceneC)
        assertThat(transition.progress).isEqualTo(56.dp / LayoutHeight)
        assertThat(transition.isInitiatedByUserInput).isTrue()
        assertThat(transition.isUserInputOngoing).isFalse()
        assertThat(transition.isUserInputDriven).isTrue()

        // Wait for the animation to finish. We should now be in scene C.
        rule.waitForIdle()
Loading