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 2c75876..670d200 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,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> { - return ledgerDao.getAllEntries().map { - it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryID) } + fun getAllBigEntries(): Flow> { + 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> { 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)) diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/BigEntity/BigEntity.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/BigEntity/BigEntity.kt new file mode 100644 index 0000000..868597c --- /dev/null +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/BigEntity/BigEntity.kt @@ -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 +) \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Entry/EntryEntity.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Entry/EntryEntity.kt index a263807..62c05c4 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Entry/EntryEntity.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Entry/EntryEntity.kt @@ -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 ) \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDao.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDao.kt index e446b3f..2e3e82e 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDao.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDao.kt @@ -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> - - @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> + @Query("SELECT * FROM _transaction INNER JOIN category ON _transaction.categoryID = category._id") + fun getAllBigEntries(): Flow> } diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDatabase.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDatabase.kt index b0a5e80..a572d6c 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDatabase.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/LedgerDatabase.kt @@ -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 diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/LedgerUI.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/LedgerUI.kt index 160a009..b66a4b6 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/LedgerUI.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/LedgerUI.kt @@ -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 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 22b0e15..fc301a3 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 @@ -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) } ) } } diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeViewModel.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeViewModel.kt index 4a04241..11d4361 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeViewModel.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/home/HomeViewModel.kt @@ -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> = flowOf(emptyList())) +data class EntryListUIState(val entry: Flow> = 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) + } + }*/ } \ No newline at end of file