-
Notifications
You must be signed in to change notification settings - Fork 98
/
AvatarCarousel.kt
204 lines (200 loc) · 9.36 KB
/
AvatarCarousel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.microsoft.fluentui.tokenized.persona
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.persona.R
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens
import com.microsoft.fluentui.theme.token.controlTokens.AvatarCarouselInfo
import com.microsoft.fluentui.theme.token.controlTokens.AvatarCarouselSize
import com.microsoft.fluentui.theme.token.controlTokens.AvatarCarouselTokens
import com.microsoft.fluentui.theme.token.controlTokens.AvatarTokens
import com.microsoft.fluentui.util.getStringResource
import kotlinx.coroutines.launch
import kotlin.math.max
/**
* Generate an AvatarCarousel. This is a horizontally scrollable bar which is made up of [AvatarCarouselItem].
* Avatar Carousel internally is a group of [AvatarCarouselItem] which can be used to create onClick based Avatar buttons.
*
* @param avatarList List of Avatars to be created in a carousel
* @param modifier Optional Modifier for Avatar carousel
* @param size size of the carousel
* @param enablePresence enable/disable presence indicator on avatar
* @param avatarTokens Token to provide appearance values to Avatar
* @param textMinMaxLines Provide [Pair] of minLines and maxLines for text. First value is minLines and second value is maxLines
* @param subTextMinMaxLines Provide [Pair] of minLines and maxLines for subText. First value is minLines and second value is maxLines
* @param avatarCarouselTokens Token to provide appearance values to Avatar Carousel
*/
@Composable
fun AvatarCarousel(
avatarList: List<AvatarCarouselItem>,
modifier: Modifier = Modifier,
size: AvatarCarouselSize = AvatarCarouselSize.Small,
enablePresence: Boolean = false,
avatarTokens: AvatarTokens? = null,
textMinMaxLines: Pair<Int, Int> = Pair(1, 1),
subTextMinMaxLines: Pair<Int, Int> = Pair(1, 1),
avatarCarouselTokens: AvatarCarouselTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = avatarCarouselTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.AvatarCarouselControlType] as AvatarCarouselTokens
val avatarCarouselInfo = AvatarCarouselInfo(size)
val statusString = getStringResource(R.string.Status)
val outOfOfficeString = getStringResource(R.string.Out_Of_Office)
val activeString = getStringResource(R.string.Active)
val inActiveString = getStringResource(R.string.Inactive)
val scope = rememberCoroutineScope()
val lazyListState = rememberLazyListState()
val avatarSize = token.avatarSize(avatarCarouselInfo)
val textStyle =
token.textTypography(avatarCarouselInfo)
val subTextStyle =
token.subTextTypography(avatarCarouselInfo)
val avatarTextPadding = token.padding(avatarCarouselInfo)
val bottomPadding = if (size == AvatarCarouselSize.Small) 8.dp else 0.dp
val textColor = token.textColor(avatarCarouselInfo)
val subTextColor =
token.subTextColor(avatarCarouselInfo)
LazyRow(
state = lazyListState,
modifier = modifier.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState { delta ->
scope.launch {
lazyListState.scrollBy(-delta)
}
},
)
) {
itemsIndexed(avatarList) { index, item ->
val backgroundColor =
token.backgroundBrush(avatarCarouselInfo)
.getBrushByState(
enabled = item.enabled,
selected = false,
interactionSource = remember { MutableInteractionSource() }
)
val nameString =
if (size == AvatarCarouselSize.Large) "${item.person.getName()}. " else "${item.person.firstName}. "
Column(
Modifier
.onFocusEvent { focusState ->
if (focusState.isFocused) {
scope.launch {
lazyListState.animateScrollToItem(
max(
0,
index - 2
)
)
}
}
}
.background(backgroundColor)
.requiredWidth(88.dp)
.alpha(if (item.enabled) 1f else 0.7f)
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(),
onClickLabel = null,
enabled = item.enabled,
onClick = item.onItemClick ?: {},
role = Role.Button
)
.testTag("item $index")
.clearAndSetSemantics {
contentDescription =
nameString + "${if (enablePresence) "${statusString}, ${item.person.status}," else ""} " +
"${if (enablePresence && item.person.isOOO) "${outOfOfficeString}," else ""} " +
if (item.enableActivityRing) {
if (item.person.isActive) activeString else inActiveString
} else ""
},
horizontalAlignment = Alignment.CenterHorizontally
) {
Avatar(
modifier = Modifier
.padding(top = 8.dp),
person = item.person,
size = avatarSize,
avatarToken = avatarTokens,
enablePresence = enablePresence,
enableActivityRings = item.enableActivityRing
)
Row(
Modifier
.padding(
start = 2.dp,
end = 2.dp,
top = avatarTextPadding,
bottom = bottomPadding
)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
BasicText(
modifier = Modifier.clearAndSetSemantics { },
text = item.person.firstName,
style = textStyle.merge(
TextStyle(
color = if (item.enabled) textColor.rest else textColor.disabled
)
),
minLines = textMinMaxLines.first,
maxLines = textMinMaxLines.second,
overflow = TextOverflow.Ellipsis
)
}
if (size == AvatarCarouselSize.Large) {
Row(
Modifier
.fillMaxWidth()
.padding(start = 2.dp, end = 2.dp, bottom = 8.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
BasicText(
modifier = Modifier.clearAndSetSemantics { },
text = item.person.lastName,
style = subTextStyle.merge(
TextStyle(
color = if (item.enabled) subTextColor.rest else subTextColor.disabled
)
),
minLines = subTextMinMaxLines.first,
maxLines = subTextMinMaxLines.second,
overflow = TextOverflow.Ellipsis
)
}
}
}
}
}
}