From f748d657eb9292c1998758c6f6aeb9b9f1d6833b Mon Sep 17 00:00:00 2001 From: Florian Date: Tue, 14 Jan 2025 13:40:49 +0100 Subject: [PATCH] AddViewModel updated --- .../at/xaxa/ledger/data/EntryRepository.kt | 16 +++++++++ .../at/xaxa/ledger/ui/add/AddViewModel.kt | 36 ++++++++++++++++++- .../java/at/xaxa/ledger/ui/home/HomeUI.kt | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/EntryRepository.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/EntryRepository.kt index ddbf3f2..bbb5cfe 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/EntryRepository.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/EntryRepository.kt @@ -1,9 +1,11 @@ package at.xaxa.ledger.data +import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity import at.xaxa.ledger.data.db.LedgerDao import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map +import java.util.Locale.Category class EntryRepository(private val ledgerDao: LedgerDao){ @@ -14,6 +16,11 @@ class EntryRepository(private val ledgerDao: LedgerDao){ it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryID) } } } + fun getAllCategories(): Flow> { + return ledgerDao.getAllCategory().map { + it.map {category -> CategoryEntity(category._id, category.categoryName, category.icon) } + } + } suspend fun findEntryById(id: Int): Entry { val entry = ledgerDao.findEntryById(id) @@ -21,9 +28,18 @@ class EntryRepository(private val ledgerDao: LedgerDao){ entry._id, entry.name, entry.amount, entry.date, entry.categoryID ) } + suspend fun findCategoryById(id: Int): CategoryEntity { + val category = ledgerDao.findCategoryById(id) + return CategoryEntity( + category._id, category.categoryName, category.icon + ) + } suspend fun insertEntry(entry: Entry) { ledgerDao.addEntry(EntryEntity(_id=0, entry.name, entry.amount, entry.date, entry.categoryID)) } + suspend fun insertCategory(category: CategoryEntity) { + ledgerDao.addCategory(CategoryEntity(_id=0, category.categoryName, category.icon)) + } suspend fun updateEntry(entry: Entry) { ledgerDao.updateEntry(EntryEntity(entry.id, entry.name, entry.amount, entry.date, entry.categoryID)) diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/add/AddViewModel.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/add/AddViewModel.kt index 850ec50..8ac3a5a 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/add/AddViewModel.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/add/AddViewModel.kt @@ -6,17 +6,24 @@ import androidx.compose.runtime.setValue import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.compose.viewModel import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.EntryRepository +import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity import at.xaxa.ledger.ui.home.EntryListUIState import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +data class CategoryListUIState(val entry: Flow> = flowOf(emptyList())) + +var categoryUiState by mutableStateOf(CategoryListUIState()) + private set // UI State to hold a list of games data class EntryListUi( @@ -51,4 +58,31 @@ class AddViewModel( } } } -} \ No newline at end of file + fun addCategory(category: CategoryEntity) { + viewModelScope.launch { + try { + val categoryEntity = CategoryEntity( + _id = category._id, + categoryName = category.categoryName, + icon = category.icon, + ) + + withContext(Dispatchers.IO) { + repository.insertCategory(categoryEntity) // Add game to the database + } + // Optionally, log or update UI state to reflect that the game was added + } catch (e: Exception) { + } + } + } + fun getAllCategories(){ + viewModelScope.launch { + val categories = withContext(Dispatchers.IO){ + repository.getAllCategories() + } + categoryUiState = CategoryListUIState(categories) + + } + + } + } diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeUI.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeUI.kt index fc88644..90affcf 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeUI.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeUI.kt @@ -62,3 +62,4 @@ fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewMode } } } +