@@ -38,13 +38,15 @@ import kotlinx.collections.immutable.toImmutableList
38
38
* @param modifier Jetpack Compose modifier
39
39
* @param overlapRatio the overlap ration. When 0f, avatars will render without overlap, when 1f
40
40
* only the first avatar will be visible
41
+ * @param lastOnTop if true, the last visible avatar will be rendered on top.
41
42
*/
42
43
@Composable
43
44
fun AvatarRow (
44
45
avatarDataList : ImmutableList <AvatarData >,
45
46
avatarType : AvatarType ,
46
47
modifier : Modifier = Modifier ,
47
48
overlapRatio : Float = 0.5f,
49
+ lastOnTop : Boolean = false,
48
50
) {
49
51
val isRtl = LocalLayoutDirection .current == LayoutDirection .Rtl
50
52
Box (
@@ -54,23 +56,35 @@ fun AvatarRow(
54
56
val avatarSize = avatarDataList.firstOrNull()?.size?.dp ? : return
55
57
val avatarSizePx = avatarSize.toPx()
56
58
avatarDataList
57
- .reversed()
59
+ .let {
60
+ if (lastOnTop) {
61
+ it
62
+ } else {
63
+ it.reversed()
64
+ }
65
+ }
58
66
.forEachIndexed { index, avatarData ->
67
+ val startPadding = if (lastOnTop) {
68
+ avatarSize * (1 - overlapRatio) * index
69
+ } else {
70
+ avatarSize * (1 - overlapRatio) * (lastItemIndex - index)
71
+ }
59
72
Avatar (
60
73
modifier = Modifier
61
- .padding(start = avatarSize * ( 1 - overlapRatio) * (lastItemIndex - index) )
74
+ .padding(start = startPadding )
62
75
.graphicsLayer {
63
76
compositingStrategy = CompositingStrategy .Offscreen
64
77
}
65
78
.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).
67
81
drawContent()
68
- val xOffset = if (isRtl) {
69
- size.width - avatarSizePx * (overlapRatio - 0.5f )
70
- } else {
71
- 0f + avatarSizePx * (overlapRatio - 0.5f )
72
- }
73
82
if (index < lastItemIndex) {
83
+ val xOffset = if (isRtl == lastOnTop) {
84
+ avatarSizePx * (overlapRatio - 0.5f )
85
+ } else {
86
+ size.width - avatarSizePx * (overlapRatio - 0.5f )
87
+ }
74
88
drawCircle(
75
89
color = Color .Black ,
76
90
center = Offset (
@@ -101,6 +115,17 @@ internal fun AvatarRowPreview(@PreviewParameter(OverlapRatioProvider::class) ove
101
115
}
102
116
}
103
117
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
+
104
129
@Composable
105
130
@PreviewsDayNight
106
131
internal fun AvatarRowRtlPreview (@PreviewParameter(OverlapRatioProvider ::class ) overlapRatio : Float ) {
@@ -114,7 +139,25 @@ internal fun AvatarRowRtlPreview(@PreviewParameter(OverlapRatioProvider::class)
114
139
}
115
140
116
141
@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
+ ) {
118
161
AvatarRow (
119
162
avatarDataList = listOf (" A" , " B" , " C" ).map {
120
163
AvatarData (
@@ -125,5 +168,6 @@ private fun ContentToPreview(overlapRatio: Float) {
125
168
}.toImmutableList(),
126
169
avatarType = AvatarType .User ,
127
170
overlapRatio = overlapRatio,
171
+ lastOnTop = lastOnTop,
128
172
)
129
173
}
0 commit comments