1 /*
2  * Copyright (C) 2024 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.qs.panels.ui.viewmodel
18 
19 import com.android.systemui.animation.Expandable
20 import com.android.systemui.plugins.qs.QSTile
21 import com.android.systemui.qs.pipeline.shared.TileSpec
22 import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
23 import kotlinx.coroutines.channels.awaitClose
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.distinctUntilChanged
26 import kotlinx.coroutines.flow.onStart
27 
28 class TileViewModel(private val tile: QSTile, val spec: TileSpec) {
29     val state: Flow<QSTile.State> =
<lambda>null30         conflatedCallbackFlow {
31                 val callback = QSTile.Callback { trySend(it.copy()) }
32 
33                 tile.addCallback(callback)
34 
35                 awaitClose { tile.removeCallback(callback) }
36             }
<lambda>null37             .onStart { emit(tile.state) }
38             .distinctUntilChanged()
39 
40     val currentState: QSTile.State
41         get() = tile.state
42 
onClicknull43     fun onClick(expandable: Expandable?) {
44         tile.click(expandable)
45     }
46 
onLongClicknull47     fun onLongClick(expandable: Expandable?) {
48         tile.longClick(expandable)
49     }
50 
onSecondaryClicknull51     fun onSecondaryClick(expandable: Expandable?) {
52         tile.secondaryClick(expandable)
53     }
54 
startListeningnull55     fun startListening(token: Any) = tile.setListening(token, true)
56 
57     fun stopListening(token: Any) = tile.setListening(token, false)
58 }
59