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.viewmodel
19 
20 import android.graphics.drawable.Drawable
21 import com.android.systemui.animation.Expandable
22 import com.android.systemui.common.shared.model.Text
23 import com.android.systemui.user.domain.interactor.UserSwitcherInteractor
24 import javax.inject.Inject
25 import kotlinx.coroutines.ExperimentalCoroutinesApi
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.flowOf
28 import kotlinx.coroutines.flow.mapLatest
29 
30 @OptIn(ExperimentalCoroutinesApi::class)
31 class StatusBarUserChipViewModel
32 @Inject
33 constructor(
34     interactor: UserSwitcherInteractor,
35 ) {
36     /** Whether the status bar chip ui should be available */
37     val chipEnabled: Boolean = interactor.isStatusBarUserChipEnabled
38 
39     /** Whether or not the chip should be showing, based on the number of users */
40     val isChipVisible: Flow<Boolean> =
41         if (!chipEnabled) {
42             flowOf(false)
43         } else {
44             interactor.users.mapLatest { users -> users.size > 1 }
45         }
46 
47     /** The display name of the current user */
48     val userName: Flow<Text> = interactor.selectedUser.mapLatest { userModel -> userModel.name }
49 
50     /** Avatar for the current user */
51     val userAvatar: Flow<Drawable> =
52         interactor.selectedUser.mapLatest { userModel -> userModel.image }
53 
54     /** Action to execute on click. Should launch the user switcher */
55     val onClick: (Expandable) -> Unit = { interactor.showUserSwitcher(it) }
56 }
57