Rename Source to Repository

This is more in line with the repository
google-developer-training/basic-android-kotlin-compose-training-mars-photos
which contains good practices.
This commit is contained in:
Lonami Exo 2022-10-21 20:23:51 +02:00
parent 189c1e8db8
commit fc7dc68b62
5 changed files with 11 additions and 10 deletions

View File

@ -3,11 +3,11 @@ package dev.lonami.talaria.data
import dev.lonami.talaria.bindings.Native import dev.lonami.talaria.bindings.Native
import dev.lonami.talaria.model.Dialog import dev.lonami.talaria.model.Dialog
interface DialogSource { interface DialogRepository {
fun loadDialogs(): List<Dialog>; fun loadDialogs(): List<Dialog>;
} }
class NativeDialogSource : DialogSource { class NativeDialogRepository : DialogRepository {
override fun loadDialogs(): List<Dialog> { override fun loadDialogs(): List<Dialog> {
val dialogs = mutableListOf<Dialog>() val dialogs = mutableListOf<Dialog>()
@ -28,7 +28,7 @@ class NativeDialogSource : DialogSource {
} }
} }
class MockDialogSource : DialogSource { class MockDialogRepository : DialogRepository {
override fun loadDialogs(): List<Dialog> { override fun loadDialogs(): List<Dialog> {
val dialogs = mutableListOf<Dialog>() val dialogs = mutableListOf<Dialog>()
for (i in 0 until 10) { for (i in 0 until 10) {

View File

@ -2,7 +2,7 @@ package dev.lonami.talaria.data
import dev.lonami.talaria.model.Message import dev.lonami.talaria.model.Message
object MessageSource { object MessageRepository {
fun loadMessages(): List<Message> { fun loadMessages(): List<Message> {
return generateSequence { return generateSequence {
Message("Alice", "Testing") Message("Alice", "Testing")

View File

@ -2,7 +2,7 @@ package dev.lonami.talaria.ui
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import dev.lonami.talaria.bindings.Native import dev.lonami.talaria.bindings.Native
import dev.lonami.talaria.data.MessageSource import dev.lonami.talaria.data.MessageRepository
import dev.lonami.talaria.model.Message 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
@ -14,7 +14,7 @@ class ChatViewModel : ViewModel() {
val uiState: StateFlow<ChatUiState> = _uiState.asStateFlow() val uiState: StateFlow<ChatUiState> = _uiState.asStateFlow()
private fun loadMessages() { private fun loadMessages() {
_uiState.value = ChatUiState(MessageSource.loadMessages().toMutableList()) _uiState.value = ChatUiState(MessageRepository.loadMessages().toMutableList())
} }
fun sendMessage(dialog: String, message: String) { fun sendMessage(dialog: String, message: String) {

View File

@ -21,7 +21,7 @@ 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 androidx.lifecycle.viewmodel.compose.viewModel
import dev.lonami.talaria.R import dev.lonami.talaria.R
import dev.lonami.talaria.data.MockDialogSource import dev.lonami.talaria.data.MockDialogRepository
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
@ -70,7 +70,7 @@ fun DialogScreen(
@Preview @Preview
@Composable @Composable
fun DialogPreview() { fun DialogPreview() {
val viewModel = DialogViewModel(MockDialogSource()) val viewModel = DialogViewModel(MockDialogRepository())
TalariaTheme { TalariaTheme {
DialogScreen(onDialogSelected = { }, dialogViewModel = viewModel) DialogScreen(onDialogSelected = { }, dialogViewModel = viewModel)
} }

View File

@ -2,12 +2,13 @@ package dev.lonami.talaria.ui
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import dev.lonami.talaria.data.DialogSource import dev.lonami.talaria.data.DialogSource
import dev.lonami.talaria.data.NativeDialogSource import dev.lonami.talaria.data.NativeDialogRepository
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
class DialogViewModel(private val repository: DialogSource = NativeDialogSource()) : ViewModel() { class DialogViewModel(private val repository: DialogSource = NativeDialogRepository()) :
ViewModel() {
private val _uiState = MutableStateFlow(DialogUiState()) private val _uiState = MutableStateFlow(DialogUiState())
val uiState: StateFlow<DialogUiState> = _uiState.asStateFlow() val uiState: StateFlow<DialogUiState> = _uiState.asStateFlow()