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 @file:OptIn(ExperimentalCoroutinesApi::class)
18 
19 package com.android.systemui.scene.ui.composable
20 
21 import com.android.compose.animation.scene.MutableSceneTransitionLayoutState
22 import com.android.compose.animation.scene.SceneKey
23 import com.android.compose.animation.scene.TransitionKey
24 import com.android.compose.animation.scene.observableTransitionState
25 import com.android.systemui.scene.shared.model.SceneDataSource
26 import kotlinx.coroutines.CoroutineScope
27 import kotlinx.coroutines.ExperimentalCoroutinesApi
28 import kotlinx.coroutines.flow.SharingStarted
29 import kotlinx.coroutines.flow.StateFlow
30 import kotlinx.coroutines.flow.flatMapLatest
31 import kotlinx.coroutines.flow.stateIn
32 
33 /**
34  * An implementation of [SceneDataSource] that's backed by a [MutableSceneTransitionLayoutState].
35  */
36 class SceneTransitionLayoutDataSource(
37     private val state: MutableSceneTransitionLayoutState,
38 
39     /**
40      * The [CoroutineScope] of the @Composable that's using this, it's critical that this is *not*
41      * the application scope.
42      */
43     private val coroutineScope: CoroutineScope,
44 ) : SceneDataSource {
45     override val currentScene: StateFlow<SceneKey> =
46         state
47             .observableTransitionState()
<lambda>null48             .flatMapLatest { it.currentScene() }
49             .stateIn(
50                 scope = coroutineScope,
51                 started = SharingStarted.WhileSubscribed(),
52                 initialValue = state.transitionState.currentScene,
53             )
54 
changeScenenull55     override fun changeScene(
56         toScene: SceneKey,
57         transitionKey: TransitionKey?,
58     ) {
59         state.setTargetScene(
60             targetScene = toScene,
61             transitionKey = transitionKey,
62             coroutineScope = coroutineScope,
63         )
64     }
65 
snapToScenenull66     override fun snapToScene(toScene: SceneKey) {
67         state.snapToScene(
68             scene = toScene,
69         )
70     }
71 }
72