-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathMessages.kt
More file actions
74 lines (71 loc) · 2.54 KB
/
Copy pathMessages.kt
File metadata and controls
74 lines (71 loc) · 2.54 KB
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
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
@Composable
internal inline fun Messages(
messages: List<Message>,
contentPadding: PaddingValues = PaddingValues(0.dp),
) {
val listState = rememberLazyListState()
if (messages.isNotEmpty()) {
LaunchedEffect(messages.last()) {
listState.animateScrollToItem(messages.lastIndex, scrollOffset = 2)
}
}
LazyColumn(
modifier = Modifier.fillMaxSize().padding(start = 4.dp, end = 4.dp),
contentPadding = contentPadding,
verticalArrangement = Arrangement.spacedBy(8.dp),
state = listState,
) {
item { Spacer(Modifier.size(20.dp)) }
items(messages, key = { it.id }) {
ChatMessage(isMyMessage = it.user == myUser, it)
}
item {
Box(Modifier.height(70.dp))
}
}
}
@OptIn(ExperimentalResourceApi::class)
@Composable
fun UserPic(user: User) {
val imageSize = 48f
val painter = user.picture?.let {
painterResource(it)
} ?: object : Painter() {
override val intrinsicSize: Size = Size(imageSize, imageSize)
override fun DrawScope.onDraw() {
drawRect(user.color, size = Size(imageSize * 4, imageSize * 4))
}
}
Image(
modifier = Modifier
.size(imageSize.dp)
.clip(CircleShape),
contentScale = ContentScale.Crop,
painter = painter,
contentDescription = "User picture"
)
}