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 18 package com.android.systemui.keyboard.backlight.ui 19 20 import android.content.Context 21 import com.android.systemui.dagger.SysUISingleton 22 import com.android.systemui.dagger.qualifiers.Application 23 import com.android.systemui.keyboard.backlight.ui.view.KeyboardBacklightDialog 24 import com.android.systemui.keyboard.backlight.ui.viewmodel.BacklightDialogViewModel 25 import javax.inject.Inject 26 import kotlinx.coroutines.CoroutineScope 27 import kotlinx.coroutines.launch 28 29 /** 30 * Based on the state produced from [BacklightDialogViewModel] shows or hides keyboard backlight 31 * indicator 32 */ 33 @SysUISingleton 34 class KeyboardBacklightDialogCoordinator 35 @Inject 36 constructor( 37 @Application private val applicationScope: CoroutineScope, 38 private val context: Context, 39 private val viewModel: BacklightDialogViewModel, 40 ) { 41 42 var dialog: KeyboardBacklightDialog? = null 43 44 fun startListening() { 45 applicationScope.launch { 46 viewModel.dialogContent.collect { dialogViewModel -> 47 if (dialogViewModel != null) { 48 if (dialog == null) { 49 dialog = 50 KeyboardBacklightDialog( 51 context, 52 initialCurrentLevel = dialogViewModel.currentValue, 53 initialMaxLevel = dialogViewModel.maxValue 54 ) 55 dialog?.show() 56 } else { 57 dialog?.updateState(dialogViewModel.currentValue, dialogViewModel.maxValue) 58 } 59 } else { 60 dialog?.dismiss() 61 dialog = null 62 } 63 } 64 } 65 } 66 } 67