category structure

This commit is contained in:
Xaver 2025-01-16 12:16:33 +01:00
parent 120a24bd3e
commit d6ff6de339
3 changed files with 45 additions and 1 deletions

View File

@ -23,7 +23,7 @@ fun CategoryOverview(
onButtonClick: () -> Unit,
modifier: Modifier = Modifier,
onCardClick: (Int) -> Unit,
categoryViewModel: CategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
categoryViewModel: OverviewCategoryViewModel = viewModel(factory = AppViewModelProvider.Factory)
){
val categories by categoryViewModel.categoryUiState.categories.collectAsState(initial = emptyList())

View File

@ -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)
}
}
}