From e3bc4f6b742b7698ebd237468c2b722d2f733674 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 15 Jan 2025 10:06:49 +0100 Subject: [PATCH] findCategoryByID in EditVM --- .../at/xaxa/ledger/ui/edit/EditViewModel.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt index 76b715e..7f2b004 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt @@ -9,17 +9,14 @@ import androidx.lifecycle.viewModelScope import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.EntryRepository import at.xaxa.ledger.data.db.Category.CategoryEntity -import at.xaxa.ledger.ui.add.CategoryListUIState import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.flowOf -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext data class CategoryListUIState(val categories: Flow> = flowOf(emptyList())) +data class CategoryUIState(val categories: CategoryEntity = CategoryEntity(0,"",0)) data class EditUI( @@ -30,9 +27,13 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle, private val entryRepository: EntryRepository) : ViewModel() { private val entryId: Int = checkNotNull(savedStateHandle["entryId"]) + private val categoryId: Int = checkNotNull(savedStateHandle["categoryId"]) + var editUiState by mutableStateOf(EditUI()) var categoryUiState by mutableStateOf(CategoryListUIState()) + var categoryui by mutableStateOf(CategoryUIState()) + init { viewModelScope.launch { @@ -66,5 +67,15 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle, } } + fun findCategoryByID() { + viewModelScope.launch { + val category = withContext(Dispatchers.IO) { + entryRepository.findCategoryById(categoryId) + } + categoryui = CategoryUIState(category) + + } + + } } \ No newline at end of file