Skip to content

Commit d9d6161

Browse files
committed
Add a revered parameter to AvatarRow to be able to stack avatar the other way.
1 parent 2ee4b4f commit d9d6161

File tree

1 file changed

+53
-9
lines changed
  • libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/avatar

1 file changed

+53
-9
lines changed

libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/components/avatar/AvatarRow.kt

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ import kotlinx.collections.immutable.toImmutableList
3838
* @param modifier Jetpack Compose modifier
3939
* @param overlapRatio the overlap ration. When 0f, avatars will render without overlap, when 1f
4040
* only the first avatar will be visible
41+
* @param lastOnTop if true, the last visible avatar will be rendered on top.
4142
*/
4243
@Composable
4344
fun AvatarRow(
4445
avatarDataList: ImmutableList<AvatarData>,
4546
avatarType: AvatarType,
4647
modifier: Modifier = Modifier,
4748
overlapRatio: Float = 0.5f,
49+
lastOnTop: Boolean = false,
4850
) {
4951
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
5052
Box(
@@ -54,23 +56,35 @@ fun AvatarRow(
5456
val avatarSize = avatarDataList.firstOrNull()?.size?.dp ?: return
5557
val avatarSizePx = avatarSize.toPx()
5658
avatarDataList
57-
.reversed()
59+
.let {
60+
if (lastOnTop) {
61+
it
62+
} else {
63+
it.reversed()
64+
}
65+
}
5866
.forEachIndexed { index, avatarData ->
67+
val startPadding = if (lastOnTop) {
68+
avatarSize * (1 - overlapRatio) * index
69+
} else {
70+
avatarSize * (1 - overlapRatio) * (lastItemIndex - index)
71+
}
5972
Avatar(
6073
modifier = Modifier
61-
.padding(start = avatarSize * (1 - overlapRatio) * (lastItemIndex - index))
74+
.padding(start = startPadding)
6275
.graphicsLayer {
6376
compositingStrategy = CompositingStrategy.Offscreen
6477
}
6578
.drawWithContent {
66-
// Draw content and clear the pixels for the avatar on the left (right in RTL).
79+
// Draw content and clear the pixels for the avatar on the left (right in RTL) or when lastOnTop is true on
80+
// the right (left in RTL).
6781
drawContent()
68-
val xOffset = if (isRtl) {
69-
size.width - avatarSizePx * (overlapRatio - 0.5f)
70-
} else {
71-
0f + avatarSizePx * (overlapRatio - 0.5f)
72-
}
7382
if (index < lastItemIndex) {
83+
val xOffset = if (isRtl == lastOnTop) {
84+
avatarSizePx * (overlapRatio - 0.5f)
85+
} else {
86+
size.width - avatarSizePx * (overlapRatio - 0.5f)
87+
}
7488
drawCircle(
7589
color = Color.Black,
7690
center = Offset(
@@ -101,6 +115,17 @@ internal fun AvatarRowPreview(@PreviewParameter(OverlapRatioProvider::class) ove
101115
}
102116
}
103117

118+
@Composable
119+
@PreviewsDayNight
120+
internal fun AvatarRowLastOnTopPreview(@PreviewParameter(OverlapRatioProvider::class) overlapRatio: Float) {
121+
ElementPreview {
122+
ContentToPreview(
123+
overlapRatio = overlapRatio,
124+
lastOnTop = true,
125+
)
126+
}
127+
}
128+
104129
@Composable
105130
@PreviewsDayNight
106131
internal fun AvatarRowRtlPreview(@PreviewParameter(OverlapRatioProvider::class) overlapRatio: Float) {
@@ -114,7 +139,25 @@ internal fun AvatarRowRtlPreview(@PreviewParameter(OverlapRatioProvider::class)
114139
}
115140

116141
@Composable
117-
private fun ContentToPreview(overlapRatio: Float) {
142+
@PreviewsDayNight
143+
internal fun AvatarRowLastOnTopRtlPreview(@PreviewParameter(OverlapRatioProvider::class) overlapRatio: Float) {
144+
CompositionLocalProvider(
145+
LocalLayoutDirection provides LayoutDirection.Rtl,
146+
) {
147+
ElementPreview {
148+
ContentToPreview(
149+
overlapRatio = overlapRatio,
150+
lastOnTop = true,
151+
)
152+
}
153+
}
154+
}
155+
156+
@Composable
157+
private fun ContentToPreview(
158+
overlapRatio: Float,
159+
lastOnTop: Boolean = false,
160+
) {
118161
AvatarRow(
119162
avatarDataList = listOf("A", "B", "C").map {
120163
AvatarData(
@@ -125,5 +168,6 @@ private fun ContentToPreview(overlapRatio: Float) {
125168
}.toImmutableList(),
126169
avatarType = AvatarType.User,
127170
overlapRatio = overlapRatio,
171+
lastOnTop = lastOnTop,
128172
)
129173
}

0 commit comments

Comments
 (0)