1 /*
2  * Copyright (C) 2024 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.transition.link
18 
19 import com.android.compose.animation.scene.BaseSceneTransitionLayoutState
20 import com.android.compose.animation.scene.SceneKey
21 import com.android.compose.animation.scene.SceneTransitionLayoutState
22 import com.android.compose.animation.scene.TransitionKey
23 import com.android.compose.animation.scene.TransitionState
24 
25 /** A link between a source (implicit) and [target] `SceneTransitionLayoutState`. */
26 class StateLink(target: SceneTransitionLayoutState, val transitionLinks: List<TransitionLink>) {
27 
28     internal val target = target as BaseSceneTransitionLayoutState
29 
30     /**
31      * Links two transitions (source and target) together.
32      *
33      * `null` can be passed to indicate that any SceneKey should match. e.g. passing `null`, `null`,
34      * `null`, `SceneA` means that any transition at the source will trigger a transition in the
35      * target to `SceneA` from any current scene.
36      */
37     class TransitionLink(
38         val sourceFrom: SceneKey?,
39         val sourceTo: SceneKey?,
40         val targetFrom: SceneKey?,
41         val targetTo: SceneKey,
42         val targetTransitionKey: TransitionKey? = null,
43     ) {
44         init {
45             if (
46                 (sourceFrom != null && sourceFrom == sourceTo) ||
47                     (targetFrom != null && targetFrom == targetTo)
48             )
49                 error("From and To can't be the same")
50         }
51 
isMatchingLinknull52         internal fun isMatchingLink(transition: TransitionState.Transition): Boolean {
53             return (sourceFrom == null || sourceFrom == transition.fromScene) &&
54                 (sourceTo == null || sourceTo == transition.toScene)
55         }
56 
targetIsInValidStatenull57         internal fun targetIsInValidState(targetCurrentScene: SceneKey): Boolean {
58             return (targetFrom == null || targetFrom == targetCurrentScene) &&
59                 targetTo != targetCurrentScene
60         }
61     }
62 }
63