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 
18 package com.android.systemui.user.ui.binder
19 
20 import androidx.core.view.isVisible
21 import androidx.lifecycle.Lifecycle
22 import androidx.lifecycle.repeatOnLifecycle
23 import com.android.systemui.animation.Expandable
24 import com.android.systemui.common.ui.binder.TextViewBinder
25 import com.android.systemui.lifecycle.repeatWhenAttached
26 import com.android.systemui.statusbar.phone.userswitcher.StatusBarUserSwitcherContainer
27 import com.android.systemui.user.ui.viewmodel.StatusBarUserChipViewModel
28 import kotlinx.coroutines.InternalCoroutinesApi
29 import kotlinx.coroutines.flow.collect
30 import kotlinx.coroutines.launch
31 
32 @OptIn(InternalCoroutinesApi::class)
33 object StatusBarUserChipViewBinder {
34     /** Binds the status bar user chip view model to the given view */
35     @JvmStatic
36     fun bind(
37         view: StatusBarUserSwitcherContainer,
38         viewModel: StatusBarUserChipViewModel,
39     ) {
40         view.repeatWhenAttached {
41             repeatOnLifecycle(Lifecycle.State.STARTED) {
42                 launch {
43                     viewModel.isChipVisible.collect { isVisible -> view.isVisible = isVisible }
44                 }
45 
46                 launch {
47                     viewModel.userName.collect { name -> TextViewBinder.bind(view.text, name) }
48                 }
49 
50                 launch {
51                     viewModel.userAvatar.collect { avatar -> view.avatar.setImageDrawable(avatar) }
52                 }
53 
54                 bindButton(view, viewModel)
55             }
56         }
57     }
58 
59     private fun bindButton(
60         view: StatusBarUserSwitcherContainer,
61         viewModel: StatusBarUserChipViewModel,
62     ) {
63         view.setOnClickListener { viewModel.onClick(Expandable.fromView(view)) }
64     }
65 }
66