1 /*
2  * 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 package com.android.systemui.statusbar.notification.shelf.ui.viewbinder
18 
19 import com.android.app.tracing.traceSection
20 import com.android.systemui.plugins.FalsingManager
21 import com.android.systemui.statusbar.NotificationShelf
22 import com.android.systemui.statusbar.notification.icon.ui.viewbinder.NotificationIconContainerShelfViewBinder
23 import com.android.systemui.statusbar.notification.row.ui.viewbinder.ActivatableNotificationViewBinder
24 import com.android.systemui.statusbar.notification.shelf.ui.viewmodel.NotificationShelfViewModel
25 import kotlinx.coroutines.awaitCancellation
26 import kotlinx.coroutines.coroutineScope
27 import com.android.app.tracing.coroutines.launchTraced as launch
28 
29 /** Binds a [NotificationShelf] to its [view model][NotificationShelfViewModel]. */
30 object NotificationShelfViewBinder {
bindnull31     suspend fun bind(
32         shelf: NotificationShelf,
33         viewModel: NotificationShelfViewModel,
34         falsingManager: FalsingManager,
35         nicBinder: NotificationIconContainerShelfViewBinder,
36     ): Unit = coroutineScope {
37         ActivatableNotificationViewBinder.bind(viewModel, shelf, falsingManager)
38         shelf.apply {
39             traceSection("NotifShelf#bindShelfIcons") { launch { nicBinder.bind(shelfIcons) } }
40             launch {
41                 viewModel.canModifyColorOfNotifications.collect(::setCanModifyColorOfNotifications)
42             }
43             launch { viewModel.isClickable.collect(::setCanInteract) }
44             registerViewListenersWhileAttached(shelf, viewModel)
45         }
46     }
47 
registerViewListenersWhileAttachednull48     private suspend fun registerViewListenersWhileAttached(
49         shelf: NotificationShelf,
50         viewModel: NotificationShelfViewModel,
51     ) {
52         try {
53             shelf.setOnClickListener { viewModel.onShelfClicked() }
54             awaitCancellation()
55         } finally {
56             shelf.setOnClickListener(null)
57         }
58     }
59 }
60