Create view models for chat and dialog UI

This commit is contained in:
Lonami Exo 2022-10-11 20:58:40 +02:00
parent aeaf831edb
commit 3a8b2b4e32
7 changed files with 71 additions and 8 deletions

View File

@ -49,6 +49,7 @@ android {
dependencies { dependencies {
implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
implementation 'androidx.activity:activity-compose:1.6.0' implementation 'androidx.activity:activity-compose:1.6.0'
implementation "androidx.compose.ui:ui:$compose_ui_version" implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"

View File

@ -7,11 +7,13 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Card import androidx.compose.material.Card
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import dev.lonami.talaria.data.MessageSource import androidx.lifecycle.viewmodel.compose.viewModel
import dev.lonami.talaria.model.Message import dev.lonami.talaria.model.Message
import dev.lonami.talaria.ui.theme.TalariaTheme import dev.lonami.talaria.ui.theme.TalariaTheme
@ -41,12 +43,16 @@ fun MessageList(messages: List<Message>) {
} }
} }
@Composable
fun ChatScreen(chatViewModel: ChatViewModel = viewModel()) {
val chatUiState by chatViewModel.uiState.collectAsState()
MessageList(chatUiState.messages)
}
@Preview @Preview
@Composable @Composable
fun ChatPreview() { fun ChatPreview() {
TalariaTheme { TalariaTheme {
MessageList( ChatScreen()
MessageSource.loadMessages()
)
} }
} }

View File

@ -0,0 +1,5 @@
package dev.lonami.talaria.ui
import dev.lonami.talaria.model.Message
data class ChatUiState(val messages: List<Message> = listOf())

View File

@ -0,0 +1,20 @@
package dev.lonami.talaria.ui
import androidx.lifecycle.ViewModel
import dev.lonami.talaria.data.MessageSource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
class ChatViewModel : ViewModel() {
private val _uiState = MutableStateFlow(ChatUiState())
val uiState: StateFlow<ChatUiState> = _uiState.asStateFlow()
private fun loadMessages() {
_uiState.value = ChatUiState(MessageSource.loadMessages())
}
init {
loadMessages()
}
}

View File

@ -10,14 +10,16 @@ import androidx.compose.material.Card
import androidx.compose.material.Switch import androidx.compose.material.Switch
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import dev.lonami.talaria.R import dev.lonami.talaria.R
import dev.lonami.talaria.data.DialogSource
import dev.lonami.talaria.model.Dialog import dev.lonami.talaria.model.Dialog
import dev.lonami.talaria.ui.theme.TalariaTheme import dev.lonami.talaria.ui.theme.TalariaTheme
@ -49,12 +51,16 @@ fun DialogList(dialogs: List<Dialog>) {
} }
} }
@Composable
fun DialogScreen(dialogViewModel: DialogViewModel = viewModel()) {
val dialogUiState by dialogViewModel.uiState.collectAsState()
DialogList(dialogUiState.dialogs)
}
@Preview @Preview
@Composable @Composable
fun DialogPreview() { fun DialogPreview() {
TalariaTheme { TalariaTheme {
DialogList( DialogScreen()
DialogSource.loadDialogs()
)
} }
} }

View File

@ -0,0 +1,5 @@
package dev.lonami.talaria.ui
import dev.lonami.talaria.model.Dialog
data class DialogUiState(val dialogs: List<Dialog> = listOf())

View File

@ -0,0 +1,20 @@
package dev.lonami.talaria.ui
import androidx.lifecycle.ViewModel
import dev.lonami.talaria.data.DialogSource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
class DialogViewModel : ViewModel() {
private val _uiState = MutableStateFlow(DialogUiState())
val uiState: StateFlow<DialogUiState> = _uiState.asStateFlow()
private fun loadDialogs() {
_uiState.value = DialogUiState(DialogSource.loadDialogs())
}
init {
loadDialogs()
}
}