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