1 /*
2  * Copyright (C) 2022 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.wifi.ui
18 
19 import android.view.ViewGroup
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.phone.StatusBarIconController
25 import com.android.systemui.statusbar.phone.StatusBarLocation
26 import com.android.systemui.statusbar.pipeline.wifi.ui.model.WifiIcon
27 import com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.LocationBasedWifiViewModel
28 import com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.LocationBasedWifiViewModel.Companion.viewModelForLocation
29 import com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.WifiViewModel
30 import javax.inject.Inject
31 import kotlinx.coroutines.flow.collect
32 import kotlinx.coroutines.launch
33 
34 /**
35  * This class serves as a bridge between the old UI classes and the new data pipeline.
36  *
37  * Once the new pipeline notifies [wifiViewModel] that the wifi icon should be visible, this class
38  * notifies [iconController] to inflate the wifi icon (if needed). After that, the [wifiViewModel]
39  * has sole responsibility for updating the wifi icon drawable, visibility, etc. and the
40  * [iconController] will not do any updates to the icon.
41  */
42 @SysUISingleton
43 class WifiUiAdapter
44 @Inject
45 constructor(
46     private val iconController: StatusBarIconController,
47     private val wifiViewModel: WifiViewModel,
48 ) {
49     /**
50      * Binds the container for all the status bar icons to a view model, so that we inflate the wifi
51      * view once we receive a valid icon from the data pipeline.
52      *
53      * NOTE: This should go away as we better integrate the data pipeline with the UI.
54      *
55      * @return the view model used for this particular group in the given [location].
56      */
57     fun bindGroup(
58         statusBarIconGroup: ViewGroup,
59         location: StatusBarLocation,
60     ): LocationBasedWifiViewModel {
61         val locationViewModel = viewModelForLocation(wifiViewModel, location)
62 
63         statusBarIconGroup.repeatWhenAttached {
64             repeatOnLifecycle(Lifecycle.State.STARTED) {
65                 launch {
66                     locationViewModel.wifiIcon.collect { wifiIcon ->
67                         // Only notify the icon controller if we want to *render* the new icon.
68                         if (wifiIcon is WifiIcon.Visible) {
69                             iconController.setNewWifiIcon()
70                         }
71                     }
72                 }
73             }
74         }
75 
76         return locationViewModel
77     }
78 }
79