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.pipeline.shared.ui.binder
18 
19 import android.view.View
20 import androidx.lifecycle.Lifecycle
21 import androidx.lifecycle.repeatOnLifecycle
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.lifecycle.repeatWhenAttached
24 import com.android.systemui.statusbar.pipeline.shared.ui.viewmodel.CollapsedStatusBarViewModel
25 import javax.inject.Inject
26 import kotlinx.coroutines.launch
27 
28 /**
29  * Interface to assist with binding the [CollapsedStatusBarFragment] to
30  * [CollapsedStatusBarViewModel]. Used only to enable easy testing of [CollapsedStatusBarFragment].
31  */
32 interface CollapsedStatusBarViewBinder {
33     /**
34      * Binds the view to the view-model. [listener] will be notified whenever an event that may
35      * change the status bar visibility occurs.
36      */
37     fun bind(
38         view: View,
39         viewModel: CollapsedStatusBarViewModel,
40         listener: StatusBarVisibilityChangeListener,
41     )
42 }
43 
44 @SysUISingleton
45 class CollapsedStatusBarViewBinderImpl @Inject constructor() : CollapsedStatusBarViewBinder {
46     override fun bind(
47         view: View,
48         viewModel: CollapsedStatusBarViewModel,
49         listener: StatusBarVisibilityChangeListener,
50     ) {
51         view.repeatWhenAttached {
52             repeatOnLifecycle(Lifecycle.State.CREATED) {
53                 launch {
54                     viewModel.isTransitioningFromLockscreenToOccluded.collect {
55                         listener.onStatusBarVisibilityMaybeChanged()
56                     }
57                 }
58 
59                 launch {
60                     viewModel.transitionFromLockscreenToDreamStartedEvent.collect {
61                         listener.onTransitionFromLockscreenToDreamStarted()
62                     }
63                 }
64             }
65         }
66     }
67 }
68 
69 /** Listener for various events that may affect the status bar's visibility. */
70 interface StatusBarVisibilityChangeListener {
71     /**
72      * Called when the status bar visibility might have changed due to the device moving to a
73      * different state.
74      */
75     fun onStatusBarVisibilityMaybeChanged()
76 
77     /** Called when a transition from lockscreen to dream has started. */
78     fun onTransitionFromLockscreenToDreamStarted()
79 }
80