From 189c1e8db8f243b754a3600f7b866c544a203c37 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 21 Oct 2022 14:19:34 +0200 Subject: [PATCH] Use mock dialog values in preview --- .../dev/lonami/talaria/data/DialogSource.kt | 18 ++++++++++++++++-- .../java/dev/lonami/talaria/ui/DialogScreen.kt | 4 +++- .../dev/lonami/talaria/ui/DialogViewModel.kt | 5 +++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/dev/lonami/talaria/data/DialogSource.kt b/app/src/main/java/dev/lonami/talaria/data/DialogSource.kt index beb5e5f..ff9af59 100644 --- a/app/src/main/java/dev/lonami/talaria/data/DialogSource.kt +++ b/app/src/main/java/dev/lonami/talaria/data/DialogSource.kt @@ -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 { +interface DialogSource { + fun loadDialogs(): List; +} + +class NativeDialogSource : DialogSource { + override fun loadDialogs(): List { val dialogs = mutableListOf() val dialogPtr = Native.getDialogs() @@ -23,3 +27,13 @@ object DialogSource { return dialogs } } + +class MockDialogSource : DialogSource { + override fun loadDialogs(): List { + val dialogs = mutableListOf() + for (i in 0 until 10) { + dialogs.add(Dialog("Sample Dialog $i", "Sample Message $i", i == 0)) + } + return dialogs + } +} diff --git a/app/src/main/java/dev/lonami/talaria/ui/DialogScreen.kt b/app/src/main/java/dev/lonami/talaria/ui/DialogScreen.kt index 116b672..994bcb9 100644 --- a/app/src/main/java/dev/lonami/talaria/ui/DialogScreen.kt +++ b/app/src/main/java/dev/lonami/talaria/ui/DialogScreen.kt @@ -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) } } diff --git a/app/src/main/java/dev/lonami/talaria/ui/DialogViewModel.kt b/app/src/main/java/dev/lonami/talaria/ui/DialogViewModel.kt index 8b0ee1f..ef48ad7 100644 --- a/app/src/main/java/dev/lonami/talaria/ui/DialogViewModel.kt +++ b/app/src/main/java/dev/lonami/talaria/ui/DialogViewModel.kt @@ -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 = _uiState.asStateFlow() private fun loadDialogs() { - _uiState.value = DialogUiState(DialogSource.loadDialogs()) + _uiState.value = DialogUiState(repository.loadDialogs()) } init {