AddViewModel updated

This commit is contained in:
Florian 2025-01-14 13:40:49 +01:00
parent 4e414be7fb
commit f748d657eb
3 changed files with 52 additions and 1 deletions

View File

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

View File

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

View File

@ -62,3 +62,4 @@ fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewMode
}
}
}