Merge remote-tracking branch 'origin/Florian'

This commit is contained in:
Xaver 2025-01-14 11:25:52 +01:00
commit 1d025b4947

View File

@ -1,2 +1,54 @@
package at.xaxa.ledger.ui.add package at.xaxa.ledger.ui.add
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.Entry
import at.xaxa.ledger.data.EntryRepository
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.launch
import kotlinx.coroutines.withContext
// UI State to hold a list of games
data class EntryListUi(
val entries: Flow<List<Entry>> = flow{emit(emptyList()) }
)
class AddViewModel(
private val savedStateHandle: SavedStateHandle,
private val repository: EntryRepository
) : ViewModel() {
var addUiState by mutableStateOf(EntryListUi())
private set
fun addEntryToDB(entry: Entry){
viewModelScope.launch {
try {
val entryEntity = Entry(
id = entry.id,
name = entry.name,
date = entry.date,
amount = entry.amount,
categoryID = entry.categoryID
)
withContext(Dispatchers.IO) {
repository.insertEntry(entryEntity) // Add game to the database
}
// Optionally, log or update UI state to reflect that the game was added
} catch (e: Exception) {
}
}
}
}