category structure
This commit is contained in:
parent
120a24bd3e
commit
d6ff6de339
@ -23,7 +23,7 @@ fun CategoryOverview(
|
|||||||
onButtonClick: () -> Unit,
|
onButtonClick: () -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
onCardClick: (Int) -> Unit,
|
onCardClick: (Int) -> Unit,
|
||||||
categoryViewModel: CategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
categoryViewModel: OverviewCategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||||
){
|
){
|
||||||
|
|
||||||
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
|
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())
|
@ -0,0 +1,44 @@
|
|||||||
|
package at.xaxa.ledger.ui.category
|
||||||
|
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.lifecycle.SavedStateHandle
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import at.xaxa.ledger.data.EntryRepository
|
||||||
|
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||||
|
import at.xaxa.ledger.ui.category.add.CategoryListUIState
|
||||||
|
import at.xaxa.ledger.ui.category.add.CategoryUIState
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flowOf
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
data class CategoryUIState(val category: CategoryEntity = CategoryEntity(0,"",0))
|
||||||
|
data class CategoryListUIState(val categories: Flow<List<CategoryEntity>> = flowOf(emptyList()))
|
||||||
|
|
||||||
|
|
||||||
|
class OverviewCategoryViewModel(
|
||||||
|
private val savedStateHandle: SavedStateHandle,
|
||||||
|
private val repository: EntryRepository
|
||||||
|
) : ViewModel() {
|
||||||
|
var categoryUiState by mutableStateOf(CategoryListUIState())
|
||||||
|
var categoryUi by mutableStateOf(CategoryUIState())
|
||||||
|
private set
|
||||||
|
|
||||||
|
init {
|
||||||
|
getAllCategories()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getAllCategories() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
val categories = withContext(Dispatchers.IO) {
|
||||||
|
repository.getAllCategories()
|
||||||
|
}
|
||||||
|
categoryUiState = CategoryListUIState(categories)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user