From 560c427610a0cad7fd45863b325c86acf29e1a73 Mon Sep 17 00:00:00 2001 From: Florian Date: Mon, 13 Jan 2025 17:03:54 +0100 Subject: [PATCH 1/3] added ViewModels --- Ledger/app/build.gradle.kts | 4 +- .../java/at/xaxa/ledger/LedgerApplication.kt | 2 +- .../at/xaxa/ledger/data/EntryRepository.kt | 10 ++-- .../xaxa/ledger/data/db/DatabaseInstance.kt | 20 ------- .../xaxa/ledger/data/db/Entry/EntryEntity.kt | 2 +- .../java/at/xaxa/ledger/data/db/LedgerDao.kt | 11 ++-- .../at/xaxa/ledger/data/db/LedgerDatabase.kt | 25 +++++++-- .../at/xaxa/ledger/ui/AppViewModelProvider.kt | 22 +++++++- .../at/xaxa/ledger/ui/edit/EditViewModel.kt | 52 +++++++++++++++++++ .../at/xaxa/ledger/ui/home/HomeViewModel.kt | 23 ++++++++ 10 files changed, 133 insertions(+), 38 deletions(-) delete mode 100644 Ledger/app/src/main/java/at/xaxa/ledger/data/db/DatabaseInstance.kt diff --git a/Ledger/app/build.gradle.kts b/Ledger/app/build.gradle.kts index cc2088a..8e4824d 100644 --- a/Ledger/app/build.gradle.kts +++ b/Ledger/app/build.gradle.kts @@ -5,12 +5,12 @@ plugins { android { namespace = "at.xaxa.ledger" - compileSdk = 34 + compileSdk = 35 defaultConfig { applicationId = "at.xaxa.ledger" minSdk = 29 - targetSdk = 34 + targetSdk = 35 versionCode = 1 versionName = "1.0" diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/LedgerApplication.kt b/Ledger/app/src/main/java/at/xaxa/ledger/LedgerApplication.kt index ac08f41..14d51e0 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/LedgerApplication.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/LedgerApplication.kt @@ -7,7 +7,7 @@ import at.xaxa.ledger.data.db.LedgerDatabase class LedgerApplication : Application(){ val entryRepository by lazy { EntryRepository( - DatabaseInstance.getDatabase(this).LedgerDao() + LedgerDatabase.getDatabase(this).ledgerDao() ) } } \ No newline at end of file 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 27b5128..d95de29 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 @@ -16,20 +16,20 @@ class EntryRepository(private val ledgerDao: LedgerDao){ } suspend fun findEntryById(id: Int): Entry { - val entry = LedgerDao.findEntryById(id) + val entry = ledgerDao.findEntryById(id) return Entry( entry._id, entry.name, entry.amount, entry.date, entry.categoryName ) } suspend fun insertEntry(entry: Entry) { - LedgerDao.addEntry(EntryEntity(_id=0, entry.name, entry.amount, entry.date, entry.categoryName)) + ledgerDao.addEntry(EntryEntity(_id=0, entry.name, entry.amount, entry.date, entry.categoryName)) } suspend fun updateEntry(entry: Entry) { - LedgerDao.updateEntry(EntryEntity(entry.id, entry.name, entry.amount, entry.date, entry.categoryName)) + ledgerDao.updateEntry(EntryEntity(entry.id, entry.name, entry.amount, entry.date, entry.categoryName)) } - suspend fun deletePokemon(entry: Entry) { - LedgerDao.deleteEntry(EntryEntity(_id = entry.id, entry.name, entry.amount, entry.date, entry.categoryName)) + suspend fun deleteEntry(entry: Entry) { + ledgerDao.deleteEntry(EntryEntity(_id = entry.id, entry.name, entry.amount, entry.date, entry.categoryName)) } } \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/DatabaseInstance.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/DatabaseInstance.kt deleted file mode 100644 index 2ca0847..0000000 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/DatabaseInstance.kt +++ /dev/null @@ -1,20 +0,0 @@ -import android.content.Context -import androidx.room.Room -import at.xaxa.ledger.data.db.LedgerDatabase - -object DatabaseInstance { - @Volatile - private var INSTANCE: LedgerDatabase? = null - - fun getDatabase(context: Context): LedgerDatabase { - return INSTANCE ?: synchronized(this) { - val instance = Room.databaseBuilder( - context.applicationContext, - LedgerDatabase::class.java, - "ledger_database" - ).build() - INSTANCE = instance - instance - } - } -} \ 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 aaa68c2..37616fc 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 @@ -3,7 +3,7 @@ package at.xaxa.ledger.data.db.Entry import androidx.room.Entity import androidx.room.PrimaryKey -@Entity(tableName = "transaction") +@Entity(tableName = "_transaction") data class EntryEntity( @PrimaryKey(autoGenerate = true) val _id: Int = 0, 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 de68f4c..1a6fb22 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 @@ -13,20 +13,20 @@ import kotlinx.coroutines.flow.Flow @Dao interface LedgerDao { @Insert(onConflict = OnConflictStrategy.IGNORE) - suspend fun addCategory(entryEntity: CategoryEntity) + suspend fun addCategory(categoryEntity: CategoryEntity) @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun addEntry(entryEntity: EntryEntity) @Update - suspend fun updateCategory(category: CharCategory) + suspend fun updateCategory(categoryEntity: CategoryEntity) @Update suspend fun updateEntry(entryEntity: EntryEntity) @Delete - suspend fun deleteCategory(category: CharCategory) + suspend fun deleteCategory(categoryEntity: CategoryEntity) @Delete @@ -41,10 +41,11 @@ interface LedgerDao { fun getAllCategory(): Flow> - @Query("SELECT * FROM transaction WHERE _id = :id") + @Query("SELECT * FROM _transaction WHERE _id = :id") suspend fun findEntryById(id: Int) : EntryEntity - @Query("SELECT * FROM transaction") + @Query("SELECT * FROM _transaction") fun getAllEntries(): 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 f3744fd..d52fe49 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 @@ -1,12 +1,31 @@ package at.xaxa.ledger.data.db +import android.content.Context import androidx.room.Database +import androidx.room.Room import androidx.room.RoomDatabase 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], version = 1) abstract class LedgerDatabase : RoomDatabase() { - abstract fun entryDao(): EntryDao - abstract fun categoryDao(): CategoryDao + abstract fun ledgerDao(): LedgerDao + + companion object { + @Volatile + private var Instance: LedgerDatabase? = null + + fun getDatabase(context: Context): LedgerDatabase { + println("DATADATADATADATA") + return Instance ?: synchronized(this){ + val instance = + Room.databaseBuilder(context, LedgerDatabase::class.java, "ledger_database") + .fallbackToDestructiveMigration() + .build() + Instance = instance + return instance + } + } + } } \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt index 51c31cc..57c0e6a 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt @@ -1,5 +1,25 @@ package at.xaxa.ledger.ui -object AppViewModelProvider { +import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY +import androidx.lifecycle.createSavedStateHandle +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory +import at.xaxa.ledger.LedgerApplication +import at.xaxa.ledger.ui.home.HomeViewModel + +object AppViewModelProvider { + val Factory = viewModelFactory { + initializer { + HomeViewModel((this[APPLICATION_KEY] as LedgerApplication).entryRepository) + } + + initializer { + AddViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository) + } + + initializer { + EditViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository) + } + } } \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt index 772c30d..a028e10 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/edit/EditViewModel.kt @@ -1,2 +1,54 @@ package at.xaxa.ledger.ui.edit +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 kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + + +data class EditUI( + val entry: Entry = Entry(0, "", 0.0f, 0, 0) +) + +class EditViewModel(private val savedStateHandle: SavedStateHandle, + private val entryRepository: EntryRepository) : ViewModel() { + + private val entryId: Int = checkNotNull(savedStateHandle["entryId"]) + + var editUiState by mutableStateOf(EditUI()) + private set + + init { + viewModelScope.launch { + val entry = withContext(Dispatchers.IO) { + entryRepository.findEntryById(entryId) + } + editUiState = EditUI(entry) + } + } + + fun updateEntry(entry: Entry) { + editUiState = editUiState.copy(entry=entry) + } + fun onDeleteEntry(entry: Entry) { + viewModelScope.launch { + entryRepository.deleteEntry(entry) + } + } + fun saveEntry() { + viewModelScope.launch { + entryRepository.updateEntry(editUiState.entry) + } + } + +} \ No newline at end of file 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 1928f38..9ca3a14 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 @@ -1,2 +1,25 @@ package at.xaxa.ledger.ui.home +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 kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch + +data class EntryListUIState(val entry: List) + +class HomeViewModel(private val repository: EntryRepository): ViewModel() { + val entryUIState = repository.getAllEntries() + .map {EntryListUIState(it)} + .stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(5000), + initialValue = EntryListUIState(emptyList()) + ) +} \ No newline at end of file From 7726ed13f04354ec8e1966a5d92f547a19fb6852 Mon Sep 17 00:00:00 2001 From: Florian Date: Tue, 14 Jan 2025 09:22:47 +0100 Subject: [PATCH 2/3] CategoryEntryRelation --- .../ledger/data/db/Category/CategoryEntity.kt | 2 +- .../xaxa/ledger/data/db/EntryCategoryRelation.kt | 15 +++++++++++++++ .../main/java/at/xaxa/ledger/data/db/LedgerDao.kt | 5 +++++ .../java/at/xaxa/ledger/data/db/LedgerDatabase.kt | 1 - .../at/xaxa/ledger/ui/AppViewModelProvider.kt | 5 +++-- .../java/at/xaxa/ledger/ui/home/HomeViewModel.kt | 5 +---- 6 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Category/CategoryEntity.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Category/CategoryEntity.kt index d1aed3c..ec3649b 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Category/CategoryEntity.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/Category/CategoryEntity.kt @@ -7,6 +7,6 @@ import androidx.room.PrimaryKey data class CategoryEntity( @PrimaryKey(autoGenerate = true) val _id: Int = 0, - val categoryname: String, + 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/EntryCategoryRelation.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt new file mode 100644 index 0000000..f5438bd --- /dev/null +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt @@ -0,0 +1,15 @@ +package at.xaxa.ledger.data.db + +import androidx.room.Embedded +import androidx.room.Relation +import at.xaxa.ledger.data.Entry +import java.util.Locale.Category + +data class EntryCategoryRelation( + @Embedded val category: Category, + @Relation( + parentColumn = "categoryName", + entityColumn = "categoryName" + ) + val entry: Entry +) \ 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 1a6fb22..605e7cf 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 @@ -5,6 +5,7 @@ import androidx.room.Delete import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Transaction import androidx.room.Update import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity @@ -18,6 +19,10 @@ interface LedgerDao { @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun addEntry(entryEntity: EntryEntity) + @Transaction + @Query("SELECT * FROM _transaction WHERE categoryName = :categoryName") + suspend fun getEntryWithCategoryName(categoryName: String): List + @Update suspend fun updateCategory(categoryEntity: CategoryEntity) 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 d52fe49..db13dcc 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 @@ -4,7 +4,6 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase -import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt b/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt index 57c0e6a..c8b83d7 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/ui/AppViewModelProvider.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.createSavedStateHandle import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import at.xaxa.ledger.LedgerApplication +import at.xaxa.ledger.ui.edit.EditViewModel import at.xaxa.ledger.ui.home.HomeViewModel @@ -14,9 +15,9 @@ object AppViewModelProvider { HomeViewModel((this[APPLICATION_KEY] as LedgerApplication).entryRepository) } - initializer { + /*initializer { AddViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository) - } + }*/ initializer { EditViewModel(this.createSavedStateHandle(), (this[APPLICATION_KEY] as LedgerApplication).entryRepository) 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 9ca3a14..f16fc53 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 @@ -1,16 +1,13 @@ package at.xaxa.ledger.ui.home -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 kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn -import kotlinx.coroutines.flow.update -import kotlinx.coroutines.launch + data class EntryListUIState(val entry: List) From 15a3b38f30efd2fd49d6d824d37157ceb78cadd0 Mon Sep 17 00:00:00 2001 From: Florian Date: Tue, 14 Jan 2025 10:31:02 +0100 Subject: [PATCH 3/3] Database done?! --- Ledger/app/build.gradle.kts | 10 +++++-- Ledger/app/src/main/AndroidManifest.xml | 1 + .../main/java/at/xaxa/ledger/data/Entry.kt | 2 +- .../at/xaxa/ledger/data/EntryRepository.kt | 10 +++---- .../xaxa/ledger/data/db/Entry/EntryEntity.kt | 2 +- .../ledger/data/db/EntryCategoryRelation.kt | 14 +++++----- .../java/at/xaxa/ledger/data/db/LedgerDao.kt | 4 +-- .../at/xaxa/ledger/data/db/LedgerDatabase.kt | 12 +++++--- .../java/at/xaxa/ledger/ui/home/HomeUI.kt | 9 ++++-- .../at/xaxa/ledger/ui/home/HomeViewModel.kt | 28 +++++++++++++++---- Ledger/gradle/libs.versions.toml | 6 ++++ 11 files changed, 68 insertions(+), 30 deletions(-) diff --git a/Ledger/app/build.gradle.kts b/Ledger/app/build.gradle.kts index 63bb144..c15a50c 100644 --- a/Ledger/app/build.gradle.kts +++ b/Ledger/app/build.gradle.kts @@ -1,6 +1,8 @@ plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) + id("com.google.devtools.ksp") version "1.9.0-1.0.12" + id("org.jetbrains.kotlin.plugin.serialization") version "1.9.10" } android { @@ -50,6 +52,11 @@ android { } dependencies { + implementation(libs.androidx.room.runtime) + implementation(libs.androidx.room.ktx) + ksp(libs.androidx.room.compiler) + + //implementation(libs.androidx.room.common) implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) @@ -59,9 +66,8 @@ dependencies { implementation(libs.androidx.ui.graphics) implementation(libs.androidx.ui.tooling.preview) implementation(libs.androidx.material3) - implementation(libs.androidx.room.common) - implementation(libs.androidx.room.ktx) implementation(libs.androidx.navigation.compose) + implementation(libs.androidx.lifecycle.runtime.compose.android) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/Ledger/app/src/main/AndroidManifest.xml b/Ledger/app/src/main/AndroidManifest.xml index f6b6eec..4fdf835 100644 --- a/Ledger/app/src/main/AndroidManifest.xml +++ b/Ledger/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools"> > { return ledgerDao.getAllEntries().map { - it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryName) } + it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryID) } } } suspend fun findEntryById(id: Int): Entry { val entry = ledgerDao.findEntryById(id) return Entry( - entry._id, entry.name, entry.amount, entry.date, entry.categoryName + entry._id, entry.name, entry.amount, entry.date, entry.categoryID ) } suspend fun insertEntry(entry: Entry) { - ledgerDao.addEntry(EntryEntity(_id=0, entry.name, entry.amount, entry.date, entry.categoryName)) + ledgerDao.addEntry(EntryEntity(_id=0, entry.name, entry.amount, entry.date, entry.categoryID)) } suspend fun updateEntry(entry: Entry) { - ledgerDao.updateEntry(EntryEntity(entry.id, entry.name, entry.amount, entry.date, entry.categoryName)) + ledgerDao.updateEntry(EntryEntity(entry.id, entry.name, entry.amount, entry.date, entry.categoryID)) } suspend fun deleteEntry(entry: Entry) { - ledgerDao.deleteEntry(EntryEntity(_id = entry.id, entry.name, entry.amount, entry.date, entry.categoryName)) + ledgerDao.deleteEntry(EntryEntity(_id = entry.id, entry.name, entry.amount, entry.date, entry.categoryID)) } } \ 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 37616fc..a263807 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 @@ -10,5 +10,5 @@ data class EntryEntity( val name: String, val amount: Float, val date: Long, - val categoryName: Int + val categoryID: Int, ) \ No newline at end of file diff --git a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt index f5438bd..ec3fc20 100644 --- a/Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt +++ b/Ledger/app/src/main/java/at/xaxa/ledger/data/db/EntryCategoryRelation.kt @@ -2,14 +2,14 @@ package at.xaxa.ledger.data.db import androidx.room.Embedded import androidx.room.Relation -import at.xaxa.ledger.data.Entry -import java.util.Locale.Category +import at.xaxa.ledger.data.db.Entry.EntryEntity +import at.xaxa.ledger.data.db.Category.CategoryEntity data class EntryCategoryRelation( - @Embedded val category: Category, + @Embedded val entry: EntryEntity, @Relation( - parentColumn = "categoryName", - entityColumn = "categoryName" + parentColumn = "categoryID", + entityColumn = "_id" ) - val entry: Entry -) \ No newline at end of file + val category: CategoryEntity +) 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 605e7cf..2cc5db6 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 @@ -20,8 +20,8 @@ interface LedgerDao { suspend fun addEntry(entryEntity: EntryEntity) @Transaction - @Query("SELECT * FROM _transaction WHERE categoryName = :categoryName") - suspend fun getEntryWithCategoryName(categoryName: String): List + @Query("SELECT * FROM _transaction WHERE categoryID = :categoryID") + suspend fun getEntryWithCategoryID(categoryID: String): List @Update suspend fun updateCategory(categoryEntity: CategoryEntity) 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 db13dcc..b0a5e80 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 @@ -4,10 +4,11 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase +import at.xaxa.ledger.data.db.Category.CategoryEntity import at.xaxa.ledger.data.db.Entry.EntryEntity -@Database(entities = [EntryEntity::class], version = 1) +@Database(entities = [EntryEntity::class, CategoryEntity::class], version = 1, exportSchema = false) abstract class LedgerDatabase : RoomDatabase() { abstract fun ledgerDao(): LedgerDao @@ -16,12 +17,15 @@ abstract class LedgerDatabase : RoomDatabase() { private var Instance: LedgerDatabase? = null fun getDatabase(context: Context): LedgerDatabase { - println("DATADATADATADATA") return Instance ?: synchronized(this){ val instance = - Room.databaseBuilder(context, LedgerDatabase::class.java, "ledger_database") - .fallbackToDestructiveMigration() + Room.databaseBuilder( + context.applicationContext, + LedgerDatabase::class.java, + "ledger_database" + ).fallbackToDestructiveMigration() .build() + Instance = instance return instance } 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 28e25df..12c8f81 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 @@ -9,16 +9,21 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.lifecycle.viewmodel.compose.viewModel +import at.xaxa.ledger.ui.AppViewModelProvider import at.xaxa.ledger.ui.HeaderCard import at.xaxa.ledger.ui.HorizontalCard @OptIn(ExperimentalFoundationApi::class) @Composable -fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit) { - +fun Home(modifier: Modifier = Modifier, onCardClick: (Int) -> Unit, HomeViewModel : HomeViewModel = viewModel(factory = AppViewModelProvider.Factory)) { + val state by HomeViewModel.entryUIState.entry.collectAsState(initial = emptyList()) Column( modifier = Modifier.fillMaxSize(), 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 f16fc53..4a04241 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 @@ -1,22 +1,38 @@ package at.xaxa.ledger.ui.home +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.EntryRepository -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext -data class EntryListUIState(val entry: List) +data class EntryListUIState(val entry: Flow> = flowOf(emptyList())) class HomeViewModel(private val repository: EntryRepository): ViewModel() { - val entryUIState = repository.getAllEntries() + var entryUIState by mutableStateOf(EntryListUIState()) + init{ + viewModelScope.launch { + val entries = withContext(Dispatchers.IO){ + repository.getAllEntries() + } + entryUIState = EntryListUIState(entries) + + } + } + + /* val entryUIState = repository.getAllEntries() .map {EntryListUIState(it)} .stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000), initialValue = EntryListUIState(emptyList()) - ) + )*/ } \ No newline at end of file diff --git a/Ledger/gradle/libs.versions.toml b/Ledger/gradle/libs.versions.toml index e9314ca..64d5eaf 100644 --- a/Ledger/gradle/libs.versions.toml +++ b/Ledger/gradle/libs.versions.toml @@ -9,11 +9,16 @@ lifecycleRuntimeKtx = "2.8.7" activityCompose = "1.9.3" composeBom = "2024.04.01" roomCommon = "2.6.1" +roomCompiler = "2.6.1" roomKtx = "2.6.1" navigationCompose = "2.8.5" +lifecycleRuntimeComposeAndroid = "2.8.7" +roomRuntime = "2.6.1" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } +androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomCompiler" } +androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } @@ -30,6 +35,7 @@ androidx-material3 = { group = "androidx.compose.material3", name = "material3" androidx-room-common = { group = "androidx.room", name = "room-common", version.ref = "roomCommon" } androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomKtx" } androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" } +androidx-lifecycle-runtime-compose-android = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose-android", version.ref = "lifecycleRuntimeComposeAndroid" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" }