Database done?!

This commit is contained in:
Florian 2025-01-14 10:31:02 +01:00
parent 6d8bd7224d
commit 15a3b38f30
11 changed files with 68 additions and 30 deletions

View File

@ -1,6 +1,8 @@
plugins { plugins {
alias(libs.plugins.android.application) alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android) 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 { android {
@ -50,6 +52,11 @@ android {
} }
dependencies { 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.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.lifecycle.runtime.ktx)
@ -59,9 +66,8 @@ dependencies {
implementation(libs.androidx.ui.graphics) implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview) implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3) implementation(libs.androidx.material3)
implementation(libs.androidx.room.common)
implementation(libs.androidx.room.ktx)
implementation(libs.androidx.navigation.compose) implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.lifecycle.runtime.compose.android)
testImplementation(libs.junit) testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.espresso.core)

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<application <application
android:name=".LedgerApplication"
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"

View File

@ -5,5 +5,5 @@ class Entry (
val name: String, val name: String,
val amount: Float, val amount: Float,
val date: Long, val date: Long,
val categoryName: Int val categoryID: Int
) )

View File

@ -11,25 +11,25 @@ class EntryRepository(private val ledgerDao: LedgerDao){
fun getAllEntries(): Flow<List<Entry>> { fun getAllEntries(): Flow<List<Entry>> {
return ledgerDao.getAllEntries().map { 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 { 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.categoryName entry._id, entry.name, entry.amount, entry.date, entry.categoryID
) )
} }
suspend fun insertEntry(entry: Entry) { 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) { 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) { 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))
} }
} }

View File

@ -10,5 +10,5 @@ data class EntryEntity(
val name: String, val name: String,
val amount: Float, val amount: Float,
val date: Long, val date: Long,
val categoryName: Int val categoryID: Int,
) )

View File

@ -2,14 +2,14 @@ package at.xaxa.ledger.data.db
import androidx.room.Embedded import androidx.room.Embedded
import androidx.room.Relation import androidx.room.Relation
import at.xaxa.ledger.data.Entry import at.xaxa.ledger.data.db.Entry.EntryEntity
import java.util.Locale.Category import at.xaxa.ledger.data.db.Category.CategoryEntity
data class EntryCategoryRelation( data class EntryCategoryRelation(
@Embedded val category: Category, @Embedded val entry: EntryEntity,
@Relation( @Relation(
parentColumn = "categoryName", parentColumn = "categoryID",
entityColumn = "categoryName" entityColumn = "_id"
) )
val entry: Entry val category: CategoryEntity
) )

View File

@ -20,8 +20,8 @@ interface LedgerDao {
suspend fun addEntry(entryEntity: EntryEntity) suspend fun addEntry(entryEntity: EntryEntity)
@Transaction @Transaction
@Query("SELECT * FROM _transaction WHERE categoryName = :categoryName") @Query("SELECT * FROM _transaction WHERE categoryID = :categoryID")
suspend fun getEntryWithCategoryName(categoryName: String): List<EntryCategoryRelation> suspend fun getEntryWithCategoryID(categoryID: String): List<EntryCategoryRelation>
@Update @Update
suspend fun updateCategory(categoryEntity: CategoryEntity) suspend fun updateCategory(categoryEntity: CategoryEntity)

View File

@ -4,10 +4,11 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
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], version = 1) @Database(entities = [EntryEntity::class, CategoryEntity::class], version = 1, exportSchema = false)
abstract class LedgerDatabase : RoomDatabase() { abstract class LedgerDatabase : RoomDatabase() {
abstract fun ledgerDao(): LedgerDao abstract fun ledgerDao(): LedgerDao
@ -16,12 +17,15 @@ abstract class LedgerDatabase : RoomDatabase() {
private var Instance: LedgerDatabase? = null private var Instance: LedgerDatabase? = null
fun getDatabase(context: Context): LedgerDatabase { fun getDatabase(context: Context): LedgerDatabase {
println("DATADATADATADATA")
return Instance ?: synchronized(this){ return Instance ?: synchronized(this){
val instance = val instance =
Room.databaseBuilder(context, LedgerDatabase::class.java, "ledger_database") Room.databaseBuilder(
.fallbackToDestructiveMigration() context.applicationContext,
LedgerDatabase::class.java,
"ledger_database"
).fallbackToDestructiveMigration()
.build() .build()
Instance = instance Instance = instance
return instance return instance
} }

View File

@ -9,16 +9,21 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp 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.HeaderCard
import at.xaxa.ledger.ui.HorizontalCard import at.xaxa.ledger.ui.HorizontalCard
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @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( Column(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),

View File

@ -1,22 +1,38 @@
package at.xaxa.ledger.ui.home 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.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 kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
data class EntryListUIState(val entry: List<Entry>) data class EntryListUIState(val entry: Flow<List<Entry>> = flowOf(emptyList()))
class HomeViewModel(private val repository: EntryRepository): ViewModel() { 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)} .map {EntryListUIState(it)}
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),
initialValue = EntryListUIState(emptyList()) initialValue = EntryListUIState(emptyList())
) )*/
} }

View File

@ -9,11 +9,16 @@ lifecycleRuntimeKtx = "2.8.7"
activityCompose = "1.9.3" activityCompose = "1.9.3"
composeBom = "2024.04.01" composeBom = "2024.04.01"
roomCommon = "2.6.1" roomCommon = "2.6.1"
roomCompiler = "2.6.1"
roomKtx = "2.6.1" roomKtx = "2.6.1"
navigationCompose = "2.8.5" navigationCompose = "2.8.5"
lifecycleRuntimeComposeAndroid = "2.8.7"
roomRuntime = "2.6.1"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } 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" } junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } 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-common = { group = "androidx.room", name = "room-common", version.ref = "roomCommon" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "roomKtx" } 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-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] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }