findCategoryByID in EditVM
This commit is contained in:
parent
3ca097a804
commit
e3bc4f6b74
@ -9,17 +9,14 @@ import androidx.lifecycle.viewModelScope
|
|||||||
import at.xaxa.ledger.data.Entry
|
import at.xaxa.ledger.data.Entry
|
||||||
import at.xaxa.ledger.data.EntryRepository
|
import at.xaxa.ledger.data.EntryRepository
|
||||||
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||||
import at.xaxa.ledger.ui.add.CategoryListUIState
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
|
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
|
||||||
|
data class CategoryUIState(val categories: CategoryEntity = CategoryEntity(0,"",0))
|
||||||
|
|
||||||
|
|
||||||
data class EditUI(
|
data class EditUI(
|
||||||
@ -30,9 +27,13 @@ class EditViewModel(private val savedStateHandle: SavedStateHandle,
|
|||||||
private val entryRepository: EntryRepository) : ViewModel() {
|
private val entryRepository: EntryRepository) : ViewModel() {
|
||||||
|
|
||||||
private val entryId: Int = checkNotNull(savedStateHandle["entryId"])
|
private val entryId: Int = checkNotNull(savedStateHandle["entryId"])
|
||||||
|
private val categoryId: Int = checkNotNull(savedStateHandle["categoryId"])
|
||||||
|
|
||||||
|
|
||||||
var editUiState by mutableStateOf(EditUI())
|
var editUiState by mutableStateOf(EditUI())
|
||||||
var categoryUiState by mutableStateOf(CategoryListUIState())
|
var categoryUiState by mutableStateOf(CategoryListUIState())
|
||||||
|
var categoryui by mutableStateOf(CategoryUIState())
|
||||||
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user