1 /* 2 * Copyright (C) 2022 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 package com.android.systemui.shade.data.repository 17 18 import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging 19 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.shade.ShadeExpansionChangeEvent 22 import com.android.systemui.shade.ShadeExpansionListener 23 import com.android.systemui.shade.ShadeExpansionStateManager 24 import com.android.systemui.shade.domain.model.ShadeModel 25 import javax.inject.Inject 26 import kotlinx.coroutines.channels.awaitClose 27 import kotlinx.coroutines.flow.Flow 28 import kotlinx.coroutines.flow.distinctUntilChanged 29 30 interface ShadeRepository { 31 /** ShadeModel information regarding shade expansion events */ 32 val shadeModel: Flow<ShadeModel> 33 } 34 35 /** Business logic for shade interactions */ 36 @SysUISingleton 37 class ShadeRepositoryImpl 38 @Inject 39 constructor(shadeExpansionStateManager: ShadeExpansionStateManager) : ShadeRepository { 40 override val shadeModel: Flow<ShadeModel> = <lambda>null41 conflatedCallbackFlow { 42 val callback = 43 object : ShadeExpansionListener { 44 override fun onPanelExpansionChanged(event: ShadeExpansionChangeEvent) { 45 // Don't propagate ShadeExpansionChangeEvent.dragDownPxAmount field. 46 // It is too noisy and produces extra events that consumers won't care 47 // about 48 val info = 49 ShadeModel( 50 expansionAmount = event.fraction, 51 isExpanded = event.expanded, 52 isUserDragging = event.tracking 53 ) 54 trySendWithFailureLogging(info, TAG, "updated shade expansion info") 55 } 56 } 57 58 shadeExpansionStateManager.addExpansionListener(callback) 59 trySendWithFailureLogging(ShadeModel(), TAG, "initial shade expansion info") 60 61 awaitClose { shadeExpansionStateManager.removeExpansionListener(callback) } 62 } 63 .distinctUntilChanged() 64 65 companion object { 66 private const val TAG = "ShadeRepository" 67 } 68 } 69