BigEntity added, HomeViewModel & UI updated
This commit is contained in:
parent
6c9c2117fe
commit
908f3307e6
@ -1,6 +1,7 @@
|
||||
package at.xaxa.ledger.data
|
||||
|
||||
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.Entry.EntryEntity
|
||||
import at.xaxa.ledger.data.db.LedgerDao
|
||||
@ -11,11 +12,12 @@ import kotlinx.coroutines.flow.map
|
||||
class EntryRepository(private val ledgerDao: LedgerDao){
|
||||
|
||||
|
||||
fun getAllEntries(): Flow<List<Entry>> {
|
||||
return ledgerDao.getAllEntries().map {
|
||||
it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryID) }
|
||||
fun getAllBigEntries(): Flow<List<BigEntity>> {
|
||||
return ledgerDao.getAllBigEntries().map {
|
||||
it.map {entry -> BigEntity(entry.entryId, entry.name, entry.amount, entry.date, entry.categoryID, entry.categoryName, entry.icon) }
|
||||
}
|
||||
}
|
||||
|
||||
fun getAllCategories(): Flow<List<CategoryEntity>> {
|
||||
return ledgerDao.getAllCategory().map {
|
||||
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 {
|
||||
val entry = ledgerDao.findEntryById(id)
|
||||
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 {
|
||||
@ -39,7 +41,7 @@ class EntryRepository(private val ledgerDao: LedgerDao){
|
||||
)
|
||||
}
|
||||
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) {
|
||||
ledgerDao.addCategory(CategoryEntity(_id=0, category.categoryName, category.icon))
|
||||
@ -53,7 +55,7 @@ class EntryRepository(private val ledgerDao: LedgerDao){
|
||||
}
|
||||
|
||||
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) {
|
||||
ledgerDao.deleteCategory(CategoryEntity(_id = category._id, category.categoryName, category.icon))
|
||||
|
@ -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
|
||||
)
|
@ -6,9 +6,9 @@ import androidx.room.PrimaryKey
|
||||
@Entity(tableName = "_transaction")
|
||||
data class EntryEntity(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val _id: Int = 0,
|
||||
val entryId: Int = 0,
|
||||
val name: String,
|
||||
val amount: Float,
|
||||
val date: Long,
|
||||
val categoryID: Int,
|
||||
val categoryID: Int
|
||||
)
|
@ -7,6 +7,7 @@ import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
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.Entry.EntryEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@ -29,24 +30,19 @@ interface LedgerDao {
|
||||
@Update
|
||||
suspend fun updateEntry(entryEntity: EntryEntity)
|
||||
|
||||
|
||||
@Delete
|
||||
suspend fun deleteCategory(categoryEntity: CategoryEntity)
|
||||
|
||||
|
||||
@Delete
|
||||
suspend fun deleteEntry(entryEntity: EntryEntity)
|
||||
|
||||
|
||||
@Query("SELECT * FROM category WHERE _id = :id")
|
||||
suspend fun findCategoryById(id: Int) : CategoryEntity
|
||||
|
||||
|
||||
@Query("SELECT * FROM category")
|
||||
fun getAllCategory(): Flow<List<CategoryEntity>>
|
||||
|
||||
|
||||
@Query("SELECT * FROM _transaction WHERE _id = :id")
|
||||
@Query("SELECT * FROM _transaction WHERE entryId = :id")
|
||||
suspend fun findEntryById(id: Int) : EntryEntity
|
||||
|
||||
@Query("SELECT COUNT (*) FROM _transaction WHERE categoryID = :id LIMIT 1")
|
||||
@ -55,4 +51,6 @@ interface LedgerDao {
|
||||
@Query("SELECT * FROM _transaction")
|
||||
fun getAllEntries(): Flow<List<EntryEntity>>
|
||||
|
||||
@Query("SELECT * FROM _transaction INNER JOIN category ON _transaction.categoryID = category._id")
|
||||
fun getAllBigEntries(): Flow<List<BigEntity>>
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||
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 fun ledgerDao(): LedgerDao
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package at.xaxa.ledger.ui
|
||||
|
||||
import android.graphics.drawable.Icon
|
||||
import android.icu.text.SimpleDateFormat
|
||||
import android.util.Log
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
@ -137,7 +138,7 @@ private fun HeaderCardPreview() {
|
||||
|
||||
// region Horizontal Card
|
||||
@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(
|
||||
onClick = onClick,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
@ -151,7 +152,7 @@ fun HorizontalCard(modifier: Modifier = Modifier, name: String, date: String, am
|
||||
.fillMaxSize()
|
||||
.padding(16.dp)
|
||||
) {
|
||||
LayoutMediaText(modifier, name, date, amount)
|
||||
LayoutMediaText(modifier, name, date, amount, iconId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -185,12 +186,19 @@ fun CategoryCard(
|
||||
|
||||
|
||||
@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(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
) {
|
||||
Column {
|
||||
Icon(
|
||||
icons[iconId],
|
||||
contentDescription = "$name Icon",
|
||||
modifier = Modifier.padding(end = 8.dp) // Add padding to the right of the icon
|
||||
)
|
||||
}
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(4.dp, Alignment.Top),
|
||||
modifier = Modifier
|
||||
@ -231,6 +239,7 @@ fun LayoutMediaText(modifier: Modifier = Modifier, name: String, date: String, a
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LayoutMediaText(modifier: Modifier = Modifier, name: String, iconId: Int) {
|
||||
Row(
|
||||
@ -263,7 +272,7 @@ fun LayoutMediaText(modifier: Modifier = Modifier, name: String, iconId: Int) {
|
||||
@Preview(widthDp = 360, heightDp = 80)
|
||||
@Composable
|
||||
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
|
||||
|
||||
|
@ -26,6 +26,7 @@ import at.xaxa.ledger.ui.convertMillisToDate
|
||||
@Composable
|
||||
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 category = HomeViewModel.categoryUi.category
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@ -45,12 +46,14 @@ fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, onButtonClic
|
||||
Column(
|
||||
modifier = Modifier.padding(vertical = 4.dp)
|
||||
) {
|
||||
|
||||
HorizontalCard(
|
||||
modifier = modifier,
|
||||
name = item.name,
|
||||
date = convertMillisToDate(item.date),
|
||||
amount = item.amount.toString()+"€",
|
||||
onClick = { onCardClick(item.id) }
|
||||
iconId = item.icon,
|
||||
onClick = { onCardClick(item.entryId) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -7,32 +7,44 @@ 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.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.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.forEach
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
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() {
|
||||
|
||||
var categoryUi by mutableStateOf(CategoryUIState())
|
||||
private set
|
||||
var entryUIState by mutableStateOf(EntryListUIState())
|
||||
|
||||
|
||||
init{
|
||||
viewModelScope.launch {
|
||||
val entries = withContext(Dispatchers.IO){
|
||||
repository.getAllEntries()
|
||||
repository.getAllBigEntries()
|
||||
}
|
||||
entryUIState = EntryListUIState(entries)
|
||||
|
||||
}
|
||||
//findCategoryByID()
|
||||
|
||||
}
|
||||
|
||||
/* val entryUIState = repository.getAllEntries()
|
||||
.map {EntryListUIState(it)}
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000),
|
||||
initialValue = EntryListUIState(emptyList())
|
||||
)*/
|
||||
/* fun findCategoryByID() {
|
||||
viewModelScope.launch {
|
||||
val category = withContext(Dispatchers.IO) {
|
||||
repository.findCategoryById(categoryId)
|
||||
}
|
||||
categoryUi = CategoryUIState(category)
|
||||
}
|
||||
}*/
|
||||
}
|
Loading…
Reference in New Issue
Block a user