1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.compose.animation.scene
18
19 import androidx.compose.runtime.snapshotFlow
20 import kotlinx.coroutines.flow.Flow
21 import kotlinx.coroutines.flow.distinctUntilChanged
22 import kotlinx.coroutines.flow.flowOf
23
24 /**
25 * A scene transition state.
26 *
27 * This models the same thing as [TransitionState], with the following distinctions:
28 * 1. [TransitionState] values are backed by the Snapshot system (Compose State objects) and can be
29 * used by callers tracking State reads, for instance in Compose code during the composition,
30 * layout or Compose drawing phases.
31 * 2. [ObservableTransitionState] values are backed by Kotlin [Flow]s and can be collected by
32 * non-Compose code to observe value changes.
33 * 3. [ObservableTransitionState.Transition.fromScene] and
34 * [ObservableTransitionState.Transition.toScene] will never be equal, while
35 * [TransitionState.Transition.fromScene] and [TransitionState.Transition.toScene] can be equal.
36 */
37 sealed interface ObservableTransitionState {
38 /**
39 * The current effective scene. If a new transition was triggered, it would start from this
40 * scene.
41 */
currentScenenull42 fun currentScene(): Flow<SceneKey> {
43 return when (this) {
44 is Idle -> flowOf(currentScene)
45 is Transition -> currentScene
46 }
47 }
48
49 /** No transition/animation is currently running. */
50 data class Idle(val currentScene: SceneKey) : ObservableTransitionState
51
52 /** There is a transition animating between two scenes. */
53 class Transition(
54 val fromScene: SceneKey,
55 val toScene: SceneKey,
56 val currentScene: Flow<SceneKey>,
57 val progress: Flow<Float>,
58
59 /**
60 * Whether the transition was originally triggered by user input rather than being
61 * programmatic. If this value is initially true, it will remain true until the transition
62 * fully completes, even if the user input that triggered the transition has ended. Any
63 * sub-transitions launched by this one will inherit this value. For example, if the user
64 * drags a pointer but does not exceed the threshold required to transition to another
65 * scene, this value will remain true after the pointer is no longer touching the screen and
66 * will be true in any transition created to animate back to the original position.
67 */
68 val isInitiatedByUserInput: Boolean,
69
70 /**
71 * Whether user input is currently driving the transition. For example, if a user is
72 * dragging a pointer, this emits true. Once they lift their finger, this emits false while
73 * the transition completes/settles.
74 */
75 val isUserInputOngoing: Flow<Boolean>,
76 ) : ObservableTransitionState {
toStringnull77 override fun toString(): String =
78 """Transition
79 |(from=$fromScene,
80 | to=$toScene,
81 | isInitiatedByUserInput=$isInitiatedByUserInput,
82 | isUserInputOngoing=$isUserInputOngoing
83 |)"""
84 .trimMargin()
85 }
86
87 fun isIdle(scene: SceneKey?): Boolean {
88 return this is Idle && (scene == null || this.currentScene == scene)
89 }
90
isTransitioningnull91 fun isTransitioning(from: SceneKey? = null, to: SceneKey? = null): Boolean {
92 return this is Transition &&
93 (from == null || this.fromScene == from) &&
94 (to == null || this.toScene == to)
95 }
96 }
97
98 /**
99 * The current [ObservableTransitionState]. This models the same thing as
100 * [SceneTransitionLayoutState.transitionState], except that it is backed by Flows and can be used
101 * by non-Compose code to observe state changes.
102 */
SceneTransitionLayoutStatenull103 fun SceneTransitionLayoutState.observableTransitionState(): Flow<ObservableTransitionState> {
104 return snapshotFlow {
105 when (val state = transitionState) {
106 is TransitionState.Idle -> ObservableTransitionState.Idle(state.currentScene)
107 is TransitionState.Transition -> {
108 ObservableTransitionState.Transition(
109 fromScene = state.fromScene,
110 toScene = state.toScene,
111 currentScene = snapshotFlow { state.currentScene },
112 progress = snapshotFlow { state.progress },
113 isInitiatedByUserInput = state.isInitiatedByUserInput,
114 isUserInputOngoing = snapshotFlow { state.isUserInputOngoing },
115 )
116 }
117 }
118 }
119 .distinctUntilChanged()
120 }
121