forked from Lonami/Talaria
Use atomic state update in chat view model
This commit is contained in:
parent
57cc2119dd
commit
6e856fa70f
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue