1 /*
<lambda>null2  * 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.statusbar.notification.ui.viewbinder
18 
19 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow
20 import com.android.systemui.statusbar.notification.shared.HeadsUpRowKey
21 import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout
22 import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationListViewModel
23 import com.android.systemui.util.kotlin.sample
24 import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
25 import javax.inject.Inject
26 import kotlinx.coroutines.channels.awaitClose
27 import kotlinx.coroutines.coroutineScope
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.launch
30 
31 class HeadsUpNotificationViewBinder
32 @Inject
33 constructor(private val viewModel: NotificationListViewModel) {
34     suspend fun bindHeadsUpNotifications(parentView: NotificationStackScrollLayout): Unit =
35         coroutineScope {
36             launch {
37                 var previousKeys = emptySet<HeadsUpRowKey>()
38                 viewModel.pinnedHeadsUpRows
39                     .sample(viewModel.headsUpAnimationsEnabled, ::Pair)
40                     .collect { (newKeys, animationsEnabled) ->
41                         val added = newKeys - previousKeys
42                         val removed = previousKeys - newKeys
43                         previousKeys = newKeys
44 
45                         if (animationsEnabled) {
46                             added.forEach { key ->
47                                 parentView.generateHeadsUpAnimation(
48                                     obtainView(key),
49                                     /* isHeadsUp = */ true
50                                 )
51                             }
52                             removed.forEach { key ->
53                                 val row = obtainView(key)
54                                 parentView.generateHeadsUpAnimation(row, /* isHeadsUp = */ false)
55                                 row.setHeadsUpIsVisible()
56                             }
57                         }
58                     }
59             }
60             launch {
61                 viewModel.topHeadsUpRow.collect { key ->
62                     parentView.setTopHeadsUpRow(key?.let(::obtainView))
63                 }
64             }
65             launch {
66                 viewModel.hasPinnedHeadsUpRow.collect { parentView.setInHeadsUpPinnedMode(it) }
67             }
68             launch {
69                 parentView.isHeadsUpAnimatingAway.collect { viewModel.setHeadsUpAnimatingAway(it) }
70             }
71         }
72 
73     private fun obtainView(key: HeadsUpRowKey): ExpandableNotificationRow {
74         return viewModel.elementKeyFor(key) as ExpandableNotificationRow
75     }
76 }
77 
78 private val NotificationStackScrollLayout.isHeadsUpAnimatingAway: Flow<Boolean>
<lambda>null79     get() = conflatedCallbackFlow {
80         setHeadsUpAnimatingAwayListener { animatingAway -> trySend(animatingAway) }
81         awaitClose { setHeadsUpAnimatingAwayListener(null) }
82     }
83