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