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.startedKeyguardState.sample(shadeInteractor.isAnyFullyExpanded) 38 39 /** 40 * Decide which flow to use depending on the shade expansion state at the start of the last 41 * keyguard state transition. 42 */ 43 fun <T> transitionFlow( 44 flowWhenShadeIsExpanded: Flow<T>, 45 flowWhenShadeIsNotExpanded: Flow<T>, 46 ): Flow<T> { 47 val filteredFlowWhenShadeIsExpanded = 48 flowWhenShadeIsExpanded 49 .sample(lastStartedTransitionHadShadeFullyExpanded, ::Pair) 50 .filter { (_, shadeFullyExpanded) -> shadeFullyExpanded } 51 .map { (valueWhenShadeIsExpanded, _) -> valueWhenShadeIsExpanded } 52 val filteredFlowWhenShadeIsNotExpanded = 53 flowWhenShadeIsNotExpanded 54 .sample(lastStartedTransitionHadShadeFullyExpanded, ::Pair) 55 .filter { (_, shadeFullyExpanded) -> !shadeFullyExpanded } 56 .map { (valueWhenShadeIsNotExpanded, _) -> valueWhenShadeIsNotExpanded } 57 return merge(filteredFlowWhenShadeIsExpanded, filteredFlowWhenShadeIsNotExpanded) 58 } 59 } 60