Use atomic state update in chat view model

This commit is contained in:
Lonami Exo 2022-10-11 21:37:42 +02:00
parent 57cc2119dd
commit 6e856fa70f
1 changed files with 5 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import dev.lonami.talaria.model.Message
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
class ChatViewModel : ViewModel() { class ChatViewModel : ViewModel() {
private val _uiState = MutableStateFlow(ChatUiState()) private val _uiState = MutableStateFlow(ChatUiState())
@ -16,7 +17,10 @@ class ChatViewModel : ViewModel() {
} }
fun sendMessage(message: String) { fun sendMessage(message: String) {
_uiState.value.messages.add(Message("You", message)) _uiState.update { state ->
state.messages.add(Message("You", message))
state
}
} }
init { init {