findCategoryByID in EditVM

This commit is contained in:
Florian 2025-01-15 10:06:49 +01:00
parent 3ca097a804
commit e3bc4f6b74

View File

@ -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<List<CategoryEntity>> = 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)
}
}
}