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 package com.android.systemui.statusbar.notification.stack.ui.viewbinder
18 
19 import android.util.Log
20 import androidx.lifecycle.Lifecycle
21 import androidx.lifecycle.repeatOnLifecycle
22 import com.android.systemui.common.ui.ConfigurationState
23 import com.android.systemui.common.ui.view.onLayoutChanged
24 import com.android.systemui.dagger.SysUISingleton
25 import com.android.systemui.dagger.qualifiers.Main
26 import com.android.systemui.dump.DumpManager
27 import com.android.systemui.lifecycle.repeatWhenAttached
28 import com.android.systemui.res.R
29 import com.android.systemui.statusbar.notification.stack.ui.view.NotificationScrollView
30 import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationScrollViewModel
31 import com.android.systemui.util.kotlin.FlowDumperImpl
32 import com.android.systemui.util.kotlin.launchAndDispose
33 import javax.inject.Inject
34 import kotlinx.coroutines.CoroutineDispatcher
35 import kotlinx.coroutines.DisposableHandle
36 import kotlinx.coroutines.coroutineScope
37 import kotlinx.coroutines.flow.Flow
38 import kotlinx.coroutines.flow.MutableStateFlow
39 import kotlinx.coroutines.launch
40 
41 /** Binds the [NotificationScrollView]. */
42 @SysUISingleton
43 class NotificationScrollViewBinder
44 @Inject
45 constructor(
46     dumpManager: DumpManager,
47     @Main private val mainImmediateDispatcher: CoroutineDispatcher,
48     private val view: NotificationScrollView,
49     private val viewModel: NotificationScrollViewModel,
50     private val configuration: ConfigurationState,
51 ) : FlowDumperImpl(dumpManager) {
52 
53     private val viewLeftOffset = MutableStateFlow(0).dumpValue("viewLeftOffset")
54 
55     private fun updateViewPosition() {
56         val trueView = view.asView()
57         if (trueView.top != 0) {
58             Log.w("NSSL", "Expected $trueView to have top==0")
59         }
60         viewLeftOffset.value = trueView.left
61     }
62 
63     fun bindWhileAttached(): DisposableHandle {
64         return view.asView().repeatWhenAttached(mainImmediateDispatcher) {
65             repeatOnLifecycle(Lifecycle.State.CREATED) { bind() }
66         }
67     }
68 
69     suspend fun bind() = coroutineScope {
70         launchAndDispose {
71             updateViewPosition()
72             view.asView().onLayoutChanged { updateViewPosition() }
73         }
74 
75         launch {
76             viewModel
77                 .shadeScrimShape(cornerRadius = scrimRadius, viewLeftOffset = viewLeftOffset)
78                 .collect { view.setScrimClippingShape(it) }
79         }
80 
81         launch { viewModel.maxAlpha.collect { view.setMaxAlpha(it) } }
82         launch { viewModel.scrolledToTop.collect { view.setScrolledToTop(it) } }
83         launch { viewModel.expandFraction.collect { view.setExpandFraction(it.coerceIn(0f, 1f)) } }
84         launch { viewModel.isScrollable.collect { view.setScrollingEnabled(it) } }
85         launch { viewModel.isDozing.collect { isDozing -> view.setDozing(isDozing) } }
86 
87         launchAndDispose {
88             view.setSyntheticScrollConsumer(viewModel.syntheticScrollConsumer)
89             view.setCurrentGestureOverscrollConsumer(viewModel.currentGestureOverscrollConsumer)
90             DisposableHandle {
91                 view.setSyntheticScrollConsumer(null)
92                 view.setCurrentGestureOverscrollConsumer(null)
93             }
94         }
95     }
96 
97     /** flow of the scrim clipping radius */
98     private val scrimRadius: Flow<Int>
99         get() = configuration.getDimensionPixelOffset(R.dimen.notification_scrim_corner_radius)
100 }
101