added ViewModels
This commit is contained in:
parent
c66f711a23
commit
560c427610
@ -5,12 +5,12 @@ plugins {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "at.xaxa.ledger"
|
namespace = "at.xaxa.ledger"
|
||||||
compileSdk = 34
|
compileSdk = 35
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = "at.xaxa.ledger"
|
applicationId = "at.xaxa.ledger"
|
||||||
minSdk = 29
|
minSdk = 29
|
||||||
targetSdk = 34
|
targetSdk = 35
|
||||||
versionCode = 1
|
versionCode = 1
|
||||||
versionName = "1.0"
|
versionName = "1.0"
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import at.xaxa.ledger.data.db.LedgerDatabase
|
|||||||
class LedgerApplication : Application(){
|
class LedgerApplication : Application(){
|
||||||
val entryRepository by lazy {
|
val entryRepository by lazy {
|
||||||
EntryRepository(
|
EntryRepository(
|
||||||
DatabaseInstance.getDatabase(this).LedgerDao()
|
LedgerDatabase.getDatabase(this).ledgerDao()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,20 +16,20 @@ class EntryRepository(private val ledgerDao: LedgerDao){
|
|||||||
}
|
}
|
||||||
|
|
||||||
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.categoryName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
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.categoryName))
|
||||||
}
|
}
|
||||||
|
|
||||||
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.categoryName))
|
||||||
}
|
}
|
||||||
suspend fun deletePokemon(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.categoryName))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,7 +3,7 @@ package at.xaxa.ledger.data.db.Entry
|
|||||||
import androidx.room.Entity
|
import androidx.room.Entity
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
@Entity(tableName = "transaction")
|
@Entity(tableName = "_transaction")
|
||||||
data class EntryEntity(
|
data class EntryEntity(
|
||||||
@PrimaryKey(autoGenerate = true)
|
@PrimaryKey(autoGenerate = true)
|
||||||
val _id: Int = 0,
|
val _id: Int = 0,
|
||||||
|
@ -13,20 +13,20 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
@Dao
|
@Dao
|
||||||
interface LedgerDao {
|
interface LedgerDao {
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
suspend fun addCategory(entryEntity: CategoryEntity)
|
suspend fun addCategory(categoryEntity: CategoryEntity)
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
suspend fun addEntry(entryEntity: EntryEntity)
|
suspend fun addEntry(entryEntity: EntryEntity)
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
suspend fun updateCategory(category: CharCategory)
|
suspend fun updateCategory(categoryEntity: CategoryEntity)
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
suspend fun updateEntry(entryEntity: EntryEntity)
|
suspend fun updateEntry(entryEntity: EntryEntity)
|
||||||
|
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
suspend fun deleteCategory(category: CharCategory)
|
suspend fun deleteCategory(categoryEntity: CategoryEntity)
|
||||||
|
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
@ -41,10 +41,11 @@ interface LedgerDao {
|
|||||||
fun getAllCategory(): Flow<List<CategoryEntity>>
|
fun getAllCategory(): Flow<List<CategoryEntity>>
|
||||||
|
|
||||||
|
|
||||||
@Query("SELECT * FROM transaction WHERE _id = :id")
|
@Query("SELECT * FROM _transaction WHERE _id = :id")
|
||||||
suspend fun findEntryById(id: Int) : EntryEntity
|
suspend fun findEntryById(id: Int) : EntryEntity
|
||||||
|
|
||||||
|
|
||||||
@Query("SELECT * FROM transaction")
|
@Query("SELECT * FROM _transaction")
|
||||||
fun getAllEntries(): Flow<List<EntryEntity>>
|
fun getAllEntries(): Flow<List<EntryEntity>>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,31 @@
|
|||||||
package at.xaxa.ledger.data.db
|
package at.xaxa.ledger.data.db
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
|
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.Category.CategoryEntity
|
||||||
import at.xaxa.ledger.data.db.Entry.EntryEntity
|
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 class LedgerDatabase : RoomDatabase() {
|
||||||
abstract fun entryDao(): EntryDao
|
abstract fun ledgerDao(): LedgerDao
|
||||||
abstract fun categoryDao(): CategoryDao
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,25 @@
|
|||||||
package at.xaxa.ledger.ui
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,2 +1,54 @@
|
|||||||
package at.xaxa.ledger.ui.edit
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,2 +1,25 @@
|
|||||||
package at.xaxa.ledger.ui.home
|
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<Entry>)
|
||||||
|
|
||||||
|
class HomeViewModel(private val repository: EntryRepository): ViewModel() {
|
||||||
|
val entryUIState = repository.getAllEntries()
|
||||||
|
.map {EntryListUIState(it)}
|
||||||
|
.stateIn(
|
||||||
|
scope = viewModelScope,
|
||||||
|
started = SharingStarted.WhileSubscribed(5000),
|
||||||
|
initialValue = EntryListUIState(emptyList())
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user