• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.systemui.keyguard.ui.viewmodel
18 
19 import com.android.systemui.keyguard.domain.interactor.KeyguardTransitionInteractor
20 import com.android.systemui.shade.domain.interactor.ShadeInteractor
21 import com.android.systemui.util.kotlin.sample
22 import javax.inject.Inject
23 import kotlinx.coroutines.flow.Flow
24 import kotlinx.coroutines.flow.filter
25 import kotlinx.coroutines.flow.map
26 import kotlinx.coroutines.flow.merge
27 
28 /** Helper for flows that depend on the shade expansion */
29 class ShadeDependentFlows
30 @Inject
31 constructor(
32     transitionInteractor: KeyguardTransitionInteractor,
33     shadeInteractor: ShadeInteractor,
34 ) {
35     /** When the last keyguard state transition started, was the shade fully expanded? */
36     private val lastStartedTransitionHadShadeFullyExpanded: Flow<Boolean> =
37         transitionInteractor.startedKeyguardTransitionStep.sample(
38             shadeInteractor.isAnyFullyExpanded
39         )
40 
41     /**
42      * Decide which flow to use depending on the shade expansion state at the start of the last
43      * keyguard state transition.
44      */
45     fun <T> transitionFlow(
46         flowWhenShadeIsExpanded: Flow<T>,
47         flowWhenShadeIsNotExpanded: Flow<T>,
48     ): Flow<T> {
49         val filteredFlowWhenShadeIsExpanded =
50             flowWhenShadeIsExpanded
51                 .sample(lastStartedTransitionHadShadeFullyExpanded, ::Pair)
52                 .filter { (_, shadeFullyExpanded) -> shadeFullyExpanded }
53                 .map { (valueWhenShadeIsExpanded, _) -> valueWhenShadeIsExpanded }
54         val filteredFlowWhenShadeIsNotExpanded =
55             flowWhenShadeIsNotExpanded
56                 .sample(lastStartedTransitionHadShadeFullyExpanded, ::Pair)
57                 .filter { (_, shadeFullyExpanded) -> !shadeFullyExpanded }
58                 .map { (valueWhenShadeIsNotExpanded, _) -> valueWhenShadeIsNotExpanded }
59         return merge(filteredFlowWhenShadeIsExpanded, filteredFlowWhenShadeIsNotExpanded)
60     }
61 }
62