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.BacklightDialogContentViewModel 25 import com.android.systemui.keyboard.backlight.ui.viewmodel.BacklightDialogViewModel 26 import javax.inject.Inject 27 import kotlinx.coroutines.CoroutineScope 28 import kotlinx.coroutines.launch 29 30 private fun defaultCreateDialog(context: Context): (Int, Int) -> KeyboardBacklightDialog { 31 return { currentLevel: Int, maxLevel: Int -> 32 KeyboardBacklightDialog(context, currentLevel, maxLevel) 33 } 34 } 35 36 /** 37 * Based on the state produced from [BacklightDialogViewModel] shows or hides keyboard backlight 38 * indicator 39 */ 40 @SysUISingleton 41 class KeyboardBacklightDialogCoordinator 42 constructor( 43 @Application private val applicationScope: CoroutineScope, 44 private val viewModel: BacklightDialogViewModel, 45 private val createDialog: (Int, Int) -> KeyboardBacklightDialog 46 ) { 47 48 @Inject 49 constructor( 50 @Application applicationScope: CoroutineScope, 51 context: Context, 52 viewModel: BacklightDialogViewModel 53 ) : this(applicationScope, viewModel, defaultCreateDialog(context)) 54 55 var dialog: KeyboardBacklightDialog? = null 56 startListeningnull57 fun startListening() { 58 applicationScope.launch { 59 viewModel.dialogContent.collect { contentModel -> 60 if (contentModel != null) { 61 showDialog(contentModel) 62 } else { 63 dialog?.dismiss() 64 dialog = null 65 } 66 } 67 } 68 } 69 showDialognull70 private fun showDialog(model: BacklightDialogContentViewModel) { 71 if (dialog == null) { 72 dialog = createDialog(model.currentValue, model.maxValue) 73 } else { 74 dialog?.updateState(model.currentValue, model.maxValue) 75 } 76 // let's always show dialog - even if we're just updating it, it might have been dismissed 77 // externally by tapping finger outside of it 78 dialog?.show() 79 } 80 } 81