• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.flow.Flow
26 import kotlinx.coroutines.flow.flowOf
27 import kotlinx.coroutines.flow.mapLatest
28 
29 class StatusBarUserChipViewModel
30 @Inject
31 constructor(private val interactor: UserSwitcherInteractor) {
32     /** Whether the status bar chip ui should be available */
33     val chipEnabled: Boolean
34         get() = interactor.isStatusBarUserChipEnabled
35 
36     /** Whether or not the chip should be showing, based on the number of users */
37     val isChipVisible: Flow<Boolean> =
38         if (!chipEnabled) {
39             flowOf(false)
40         } else {
41             interactor.users.mapLatest { users -> users.size > 1 }
42         }
43 
44     /** The display name of the current user */
45     val userName: Flow<Text> = interactor.selectedUser.mapLatest { userModel -> userModel.name }
46 
47     /** Avatar for the current user */
48     val userAvatar: Flow<Drawable> =
49         interactor.selectedUser.mapLatest { userModel -> userModel.image }
50 
51     /** Action to execute on click. Should launch the user switcher */
52     val onClick: (Expandable) -> Unit = { interactor.showUserSwitcher(it) }
53 }
54