forked from Lonami/Talaria
Use mock dialog values in preview
This commit is contained in:
parent
7bfbcc955c
commit
189c1e8db8
|
@ -3,8 +3,12 @@ package dev.lonami.talaria.data
|
|||
import dev.lonami.talaria.bindings.Native
|
||||
import dev.lonami.talaria.model.Dialog
|
||||
|
||||
object DialogSource {
|
||||
fun loadDialogs(): List<Dialog> {
|
||||
interface DialogSource {
|
||||
fun loadDialogs(): List<Dialog>;
|
||||
}
|
||||
|
||||
class NativeDialogSource : DialogSource {
|
||||
override fun loadDialogs(): List<Dialog> {
|
||||
val dialogs = mutableListOf<Dialog>()
|
||||
|
||||
val dialogPtr = Native.getDialogs()
|
||||
|
@ -23,3 +27,13 @@ object DialogSource {
|
|||
return dialogs
|
||||
}
|
||||
}
|
||||
|
||||
class MockDialogSource : DialogSource {
|
||||
override fun loadDialogs(): List<Dialog> {
|
||||
val dialogs = mutableListOf<Dialog>()
|
||||
for (i in 0 until 10) {
|
||||
dialogs.add(Dialog("Sample Dialog $i", "Sample Message $i", i == 0))
|
||||
}
|
||||
return dialogs
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import androidx.compose.ui.tooling.preview.Preview
|
|||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import dev.lonami.talaria.R
|
||||
import dev.lonami.talaria.data.MockDialogSource
|
||||
import dev.lonami.talaria.model.Dialog
|
||||
import dev.lonami.talaria.ui.theme.TalariaTheme
|
||||
|
||||
|
@ -69,7 +70,8 @@ fun DialogScreen(
|
|||
@Preview
|
||||
@Composable
|
||||
fun DialogPreview() {
|
||||
val viewModel = DialogViewModel(MockDialogSource())
|
||||
TalariaTheme {
|
||||
DialogScreen(onDialogSelected = { })
|
||||
DialogScreen(onDialogSelected = { }, dialogViewModel = viewModel)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,17 @@ package dev.lonami.talaria.ui
|
|||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dev.lonami.talaria.data.DialogSource
|
||||
import dev.lonami.talaria.data.NativeDialogSource
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
class DialogViewModel : ViewModel() {
|
||||
class DialogViewModel(private val repository: DialogSource = NativeDialogSource()) : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(DialogUiState())
|
||||
val uiState: StateFlow<DialogUiState> = _uiState.asStateFlow()
|
||||
|
||||
private fun loadDialogs() {
|
||||
_uiState.value = DialogUiState(DialogSource.loadDialogs())
|
||||
_uiState.value = DialogUiState(repository.loadDialogs())
|
||||
}
|
||||
|
||||
init {
|
||||
|
|
Loading…
Reference in New Issue