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