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 package com.android.systemui.statusbar.notification.stack.ui.viewbinder
17 
18 import androidx.core.view.doOnDetach
19 import com.android.systemui.statusbar.notification.stack.DisplaySwitchNotificationsHiderTracker
20 import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayoutController
21 import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationListViewModel
22 import kotlinx.coroutines.CoroutineScope
23 import kotlinx.coroutines.flow.SharingStarted.Companion.Lazily
24 import kotlinx.coroutines.flow.shareIn
25 import kotlinx.coroutines.launch
26 
27 /**
28  * Binds a [NotificationStackScrollLayoutController] to its [view model][NotificationListViewModel].
29  */
30 object HideNotificationsBinder {
31     fun CoroutineScope.bindHideList(
32         viewController: NotificationStackScrollLayoutController,
33         viewModel: NotificationListViewModel,
34         hiderTracker: DisplaySwitchNotificationsHiderTracker
35     ) {
36         viewController.view.doOnDetach { viewController.bindHideState(shouldHide = false) }
37 
38         val hideListFlow = viewModel.hideListViewModel.shouldHideListForPerformance
39             .shareIn(this, started = Lazily)
40 
41         launch {
42             hideListFlow.collect { shouldHide ->
43                 viewController.bindHideState(shouldHide)
44             }
45         }
46 
47         launch { hiderTracker.trackNotificationHideTime(hideListFlow) }
48         launch { hiderTracker.trackNotificationHideTimeWhenVisible(hideListFlow) }
49     }
50 
51     private fun NotificationStackScrollLayoutController.bindHideState(shouldHide: Boolean) {
52         if (shouldHide) {
53             updateNotificationsContainerVisibility(/* visible= */ false, /* animate=*/ false)
54             setSuppressChildrenMeasureAndLayout(true)
55         } else {
56             setSuppressChildrenMeasureAndLayout(false)
57 
58             // Show notifications back only after layout has finished because we need
59             // to wait until they have resized to the new display size
60             addOneShotPreDrawListener {
61                 updateNotificationsContainerVisibility(/* visible= */ true, /* animate=*/ true)
62             }
63         }
64     }
65 }
66