BigEntity added, HomeViewModel & UI updated

This commit is contained in:
Florian 2025-01-17 09:28:17 +01:00
parent 6c9c2117fe
commit 908f3307e6
8 changed files with 67 additions and 30 deletions

View File

@ -1,6 +1,7 @@
package at.xaxa.ledger.data package at.xaxa.ledger.data
import android.util.Log import android.util.Log
import at.xaxa.ledger.data.db.BigEntity.BigEntity
import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Category.CategoryEntity
import at.xaxa.ledger.data.db.Entry.EntryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity
import at.xaxa.ledger.data.db.LedgerDao import at.xaxa.ledger.data.db.LedgerDao
@ -11,11 +12,12 @@ import kotlinx.coroutines.flow.map
class EntryRepository(private val ledgerDao: LedgerDao){ class EntryRepository(private val ledgerDao: LedgerDao){
fun getAllEntries(): Flow<List<Entry>> { fun getAllBigEntries(): Flow<List<BigEntity>> {
return ledgerDao.getAllEntries().map { return ledgerDao.getAllBigEntries().map {
it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryID) } it.map {entry -> BigEntity(entry.entryId, entry.name, entry.amount, entry.date, entry.categoryID, entry.categoryName, entry.icon) }
} }
} }
fun getAllCategories(): Flow<List<CategoryEntity>> { fun getAllCategories(): Flow<List<CategoryEntity>> {
return ledgerDao.getAllCategory().map { return ledgerDao.getAllCategory().map {
it.map {category -> CategoryEntity(category._id, category.categoryName, category.icon) } it.map {category -> CategoryEntity(category._id, category.categoryName, category.icon) }
@ -28,7 +30,7 @@ class EntryRepository(private val ledgerDao: LedgerDao){
suspend fun findEntryById(id: Int): Entry { suspend fun findEntryById(id: Int): Entry {
val entry = ledgerDao.findEntryById(id) val entry = ledgerDao.findEntryById(id)
return Entry( return Entry(
entry._id, entry.name, entry.amount, entry.date, entry.categoryID entry.entryId, entry.name, entry.amount, entry.date, entry.categoryID
) )
} }
suspend fun findCategoryById(id: Int): CategoryEntity { suspend fun findCategoryById(id: Int): CategoryEntity {
@ -39,7 +41,7 @@ class EntryRepository(private val ledgerDao: LedgerDao){
) )
} }
suspend fun insertEntry(entry: Entry) { suspend fun insertEntry(entry: Entry) {
ledgerDao.addEntry(EntryEntity(_id=0, entry.name, entry.amount, entry.date, entry.categoryID)) ledgerDao.addEntry(EntryEntity(entryId=0, entry.name, entry.amount, entry.date, entry.categoryID))
} }
suspend fun insertCategory(category: CategoryEntity) { suspend fun insertCategory(category: CategoryEntity) {
ledgerDao.addCategory(CategoryEntity(_id=0, category.categoryName, category.icon)) ledgerDao.addCategory(CategoryEntity(_id=0, category.categoryName, category.icon))
@ -53,7 +55,7 @@ class EntryRepository(private val ledgerDao: LedgerDao){
} }
suspend fun deleteEntry(entry: Entry) { suspend fun deleteEntry(entry: Entry) {
ledgerDao.deleteEntry(EntryEntity(_id = entry.id, entry.name, entry.amount, entry.date, entry.categoryID)) ledgerDao.deleteEntry(EntryEntity(entryId = entry.id, entry.name, entry.amount, entry.date, entry.categoryID))
} }
suspend fun deleteCategory(category: CategoryEntity) { suspend fun deleteCategory(category: CategoryEntity) {
ledgerDao.deleteCategory(CategoryEntity(_id = category._id, category.categoryName, category.icon)) ledgerDao.deleteCategory(CategoryEntity(_id = category._id, category.categoryName, category.icon))

View File

@ -0,0 +1,13 @@
package at.xaxa.ledger.data.db.BigEntity
import androidx.room.PrimaryKey
data class BigEntity (
val entryId: Int,
val name: String,
val amount: Float,
val date: Long,
val categoryID: Int,
val categoryName: String,
val icon: Int
)

View File

@ -6,9 +6,9 @@ import androidx.room.PrimaryKey
@Entity(tableName = "_transaction") @Entity(tableName = "_transaction")
data class EntryEntity( data class EntryEntity(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val _id: Int = 0, val entryId: Int = 0,
val name: String, val name: String,
val amount: Float, val amount: Float,
val date: Long, val date: Long,
val categoryID: Int, val categoryID: Int
) )

View File

@ -7,6 +7,7 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import androidx.room.Transaction import androidx.room.Transaction
import androidx.room.Update import androidx.room.Update
import at.xaxa.ledger.data.db.BigEntity.BigEntity
import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Category.CategoryEntity
import at.xaxa.ledger.data.db.Entry.EntryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -29,24 +30,19 @@ interface LedgerDao {
@Update @Update
suspend fun updateEntry(entryEntity: EntryEntity) suspend fun updateEntry(entryEntity: EntryEntity)
@Delete @Delete
suspend fun deleteCategory(categoryEntity: CategoryEntity) suspend fun deleteCategory(categoryEntity: CategoryEntity)
@Delete @Delete
suspend fun deleteEntry(entryEntity: EntryEntity) suspend fun deleteEntry(entryEntity: EntryEntity)
@Query("SELECT * FROM category WHERE _id = :id") @Query("SELECT * FROM category WHERE _id = :id")
suspend fun findCategoryById(id: Int) : CategoryEntity suspend fun findCategoryById(id: Int) : CategoryEntity
@Query("SELECT * FROM category") @Query("SELECT * FROM category")
fun getAllCategory(): Flow<List<CategoryEntity>> fun getAllCategory(): Flow<List<CategoryEntity>>
@Query("SELECT * FROM _transaction WHERE entryId = :id")
@Query("SELECT * FROM _transaction WHERE _id = :id")
suspend fun findEntryById(id: Int) : EntryEntity suspend fun findEntryById(id: Int) : EntryEntity
@Query("SELECT COUNT (*) FROM _transaction WHERE categoryID = :id LIMIT 1") @Query("SELECT COUNT (*) FROM _transaction WHERE categoryID = :id LIMIT 1")
@ -55,4 +51,6 @@ interface LedgerDao {
@Query("SELECT * FROM _transaction") @Query("SELECT * FROM _transaction")
fun getAllEntries(): Flow<List<EntryEntity>> fun getAllEntries(): Flow<List<EntryEntity>>
@Query("SELECT * FROM _transaction INNER JOIN category ON _transaction.categoryID = category._id")
fun getAllBigEntries(): Flow<List<BigEntity>>
} }

View File

@ -8,7 +8,7 @@ import at.xaxa.ledger.data.db.Category.CategoryEntity
import at.xaxa.ledger.data.db.Entry.EntryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity
@Database(entities = [EntryEntity::class, CategoryEntity::class], version = 1, exportSchema = false) @Database(entities = [EntryEntity::class, CategoryEntity::class], version = 2, exportSchema = false)
abstract class LedgerDatabase : RoomDatabase() { abstract class LedgerDatabase : RoomDatabase() {
abstract fun ledgerDao(): LedgerDao abstract fun ledgerDao(): LedgerDao

View File

@ -1,5 +1,6 @@
package at.xaxa.ledger.ui package at.xaxa.ledger.ui
import android.graphics.drawable.Icon
import android.icu.text.SimpleDateFormat import android.icu.text.SimpleDateFormat
import android.util.Log import android.util.Log
import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.BorderStroke
@ -137,7 +138,7 @@ private fun HeaderCardPreview() {
// region Horizontal Card // region Horizontal Card
@Composable @Composable
fun HorizontalCard(modifier: Modifier = Modifier, name: String, date: String, amount:String, onClick: () -> Unit ) { fun HorizontalCard(modifier: Modifier = Modifier, name: String, date: String, amount:String, onClick: () -> Unit, iconId: Int ) {
Surface( Surface(
onClick = onClick, onClick = onClick,
shape = RoundedCornerShape(12.dp), shape = RoundedCornerShape(12.dp),
@ -151,7 +152,7 @@ fun HorizontalCard(modifier: Modifier = Modifier, name: String, date: String, am
.fillMaxSize() .fillMaxSize()
.padding(16.dp) .padding(16.dp)
) { ) {
LayoutMediaText(modifier, name, date, amount) LayoutMediaText(modifier, name, date, amount, iconId)
} }
} }
} }
@ -185,12 +186,19 @@ fun CategoryCard(
@Composable @Composable
fun LayoutMediaText(modifier: Modifier = Modifier, name: String, date: String, amount:String) { fun LayoutMediaText(modifier: Modifier = Modifier, name: String, date: String, amount: String, iconId: Int) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
modifier = modifier modifier = modifier
.fillMaxWidth() .fillMaxWidth()
) { ) {
Column {
Icon(
icons[iconId],
contentDescription = "$name Icon",
modifier = Modifier.padding(end = 8.dp) // Add padding to the right of the icon
)
}
Column( Column(
verticalArrangement = Arrangement.spacedBy(4.dp, Alignment.Top), verticalArrangement = Arrangement.spacedBy(4.dp, Alignment.Top),
modifier = Modifier modifier = Modifier
@ -231,6 +239,7 @@ fun LayoutMediaText(modifier: Modifier = Modifier, name: String, date: String, a
} }
} }
} }
@Composable @Composable
fun LayoutMediaText(modifier: Modifier = Modifier, name: String, iconId: Int) { fun LayoutMediaText(modifier: Modifier = Modifier, name: String, iconId: Int) {
Row( Row(
@ -263,7 +272,7 @@ fun LayoutMediaText(modifier: Modifier = Modifier, name: String, iconId: Int) {
@Preview(widthDp = 360, heightDp = 80) @Preview(widthDp = 360, heightDp = 80)
@Composable @Composable
private fun HorizontalCardPreview() { private fun HorizontalCardPreview() {
HorizontalCard(Modifier, "McDonald's", "12th Feb, 23:32", "-124234.00€", onClick = { println("success") }) HorizontalCard(Modifier, "McDonald's", "12th Feb, 23:32", "-124234.00€", onClick = { println("success") }, 2)
} }
// endregion // endregion

View File

@ -26,6 +26,7 @@ import at.xaxa.ledger.ui.convertMillisToDate
@Composable @Composable
fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClick: () -> Unit, onCatButtonClick: () -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) { fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClick: () -> Unit, onCatButtonClick: () -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
val state by HomeViewModel.entryUIState.entry.collectAsState(initial = emptyList()) val state by HomeViewModel.entryUIState.entry.collectAsState(initial = emptyList())
val category = HomeViewModel.categoryUi.category
Column( Column(
modifier = Modifier modifier = Modifier
@ -45,12 +46,14 @@ fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClic
Column( Column(
modifier = Modifier.padding(vertical = 4.dp) modifier = Modifier.padding(vertical = 4.dp)
) { ) {
HorizontalCard( HorizontalCard(
modifier = modifier, modifier = modifier,
name = item.name, name = item.name,
date = convertMillisToDate(item.date), date = convertMillisToDate(item.date),
amount = item.amount.toString()+"", amount = item.amount.toString()+"",
onClick = { onCardClick(item.id) } iconId = item.icon,
onClick = { onCardClick(item.entryId) }
) )
} }
} }

View File

@ -7,32 +7,44 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.Entry
import at.xaxa.ledger.data.EntryRepository import at.xaxa.ledger.data.EntryRepository
import at.xaxa.ledger.data.db.BigEntity.BigEntity
import at.xaxa.ledger.ui.category.add.CategoryListUIState
import at.xaxa.ledger.ui.category.edit.CategoryUIState
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.forEach
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
data class EntryListUIState(val entry: Flow<List<Entry>> = flowOf(emptyList())) data class EntryListUIState(val entry: Flow<List<BigEntity>> = flowOf(emptyList()))
class HomeViewModel(private val repository: EntryRepository): ViewModel() { class HomeViewModel(private val repository: EntryRepository): ViewModel() {
var categoryUi by mutableStateOf(CategoryUIState())
private set
var entryUIState by mutableStateOf(EntryListUIState()) var entryUIState by mutableStateOf(EntryListUIState())
init{ init{
viewModelScope.launch { viewModelScope.launch {
val entries = withContext(Dispatchers.IO){ val entries = withContext(Dispatchers.IO){
repository.getAllEntries() repository.getAllBigEntries()
} }
entryUIState = EntryListUIState(entries) entryUIState = EntryListUIState(entries)
}
//findCategoryByID()
} }
}
/* val entryUIState = repository.getAllEntries() /* fun findCategoryByID() {
.map {EntryListUIState(it)} viewModelScope.launch {
.stateIn( val category = withContext(Dispatchers.IO) {
scope = viewModelScope, repository.findCategoryById(categoryId)
started = SharingStarted.WhileSubscribed(5000), }
initialValue = EntryListUIState(emptyList()) categoryUi = CategoryUIState(category)
)*/ }
}*/
} }