Scroll to the bottom on sending a message
This commit is contained in:
parent
29c164c4ae
commit
57cc2119dd
|
@ -2,6 +2,8 @@ package dev.lonami.talaria.ui
|
|||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyListState
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.Button
|
||||
|
@ -19,6 +21,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
|||
import dev.lonami.talaria.R
|
||||
import dev.lonami.talaria.model.Message
|
||||
import dev.lonami.talaria.ui.theme.TalariaTheme
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun MessageCard(message: Message) {
|
||||
|
@ -40,8 +43,8 @@ fun MessageCard(message: Message) {
|
|||
}
|
||||
|
||||
@Composable
|
||||
fun MessageList(messages: List<Message>, modifier: Modifier) {
|
||||
LazyColumn(modifier) {
|
||||
fun MessageList(messages: List<Message>, modifier: Modifier, listState: LazyListState) {
|
||||
LazyColumn(modifier, state = listState) {
|
||||
items(messages.size) { MessageCard(messages[it]) }
|
||||
}
|
||||
}
|
||||
|
@ -77,14 +80,23 @@ fun MessageInputField(
|
|||
fun ChatScreen(chatViewModel: ChatViewModel = viewModel()) {
|
||||
val chatUiState by chatViewModel.uiState.collectAsState()
|
||||
var messageText by remember { mutableStateOf("") }
|
||||
val messageListState = rememberLazyListState()
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
Column(modifier = Modifier.fillMaxSize()) {
|
||||
MessageList(chatUiState.messages, modifier = Modifier.weight(1.0f))
|
||||
MessageList(
|
||||
chatUiState.messages,
|
||||
modifier = Modifier.weight(1.0f),
|
||||
listState = messageListState
|
||||
)
|
||||
MessageInputField(messageText, onMessageChanged = {
|
||||
messageText = it
|
||||
}, onSendMessage = {
|
||||
chatViewModel.sendMessage(messageText)
|
||||
messageText = ""
|
||||
coroutineScope.launch {
|
||||
messageListState.animateScrollToItem(chatUiState.messages.size - 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue