working on data files
This commit is contained in:
parent
5aae7b45dc
commit
c66f711a23
13
Ledger/app/src/main/java/at/xaxa/ledger/LedgerApplication.kt
Normal file
13
Ledger/app/src/main/java/at/xaxa/ledger/LedgerApplication.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package at.xaxa.ledger
|
||||
|
||||
import android.app.Application
|
||||
import at.xaxa.ledger.data.EntryRepository
|
||||
import at.xaxa.ledger.data.db.LedgerDatabase
|
||||
|
||||
class LedgerApplication : Application(){
|
||||
val entryRepository by lazy {
|
||||
EntryRepository(
|
||||
DatabaseInstance.getDatabase(this).LedgerDao()
|
||||
)
|
||||
}
|
||||
}
|
@ -1,4 +1,9 @@
|
||||
package at.xaxa.ledger.data
|
||||
|
||||
class Entry {
|
||||
}
|
||||
class Entry (
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val amount: Float,
|
||||
val date: Long,
|
||||
val categoryName: Int
|
||||
)
|
@ -1,4 +1,35 @@
|
||||
package at.xaxa.ledger.data
|
||||
|
||||
class EntryRepository {
|
||||
import at.xaxa.ledger.data.db.Entry.EntryEntity
|
||||
import at.xaxa.ledger.data.db.LedgerDao
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
|
||||
class EntryRepository(private val ledgerDao: LedgerDao){
|
||||
|
||||
|
||||
fun getAllEntries(): Flow<List<Entry>> {
|
||||
return ledgerDao.getAllEntries().map {
|
||||
it.map {entry -> Entry(entry._id, entry.name, entry.amount, entry.date, entry.categoryName) }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun findEntryById(id: Int): Entry {
|
||||
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))
|
||||
}
|
||||
|
||||
suspend fun updateEntry(entry: Entry) {
|
||||
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))
|
||||
}
|
||||
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package at.xaxa.ledger.data.db.Category
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import at.xaxa.ledger.data.db.Entry.EntryDao
|
||||
|
||||
@Database(entities = [CategoryEntity::class], version = 1)
|
||||
abstract class CategoryDatabase : RoomDatabase() {
|
||||
abstract fun CategoryDao(): EntryDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var Instance: CategoryDatabase? = null
|
||||
|
||||
fun getDatabase(context: Context): CategoryDatabase {
|
||||
return Instance ?: synchronized(this){
|
||||
val instance =
|
||||
Room.databaseBuilder(context, CategoryDatabase::class.java, "finance_database")
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
Instance = instance
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,6 @@ import androidx.room.PrimaryKey
|
||||
data class CategoryEntity(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val _id: Int = 0,
|
||||
val name: String,
|
||||
val categoryname: String,
|
||||
val icon: Int
|
||||
)
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package at.xaxa.ledger.data.db.Entry
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface EntryDao {
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun addEntry(entryEntity: EntryEntity)
|
||||
|
||||
@Update
|
||||
suspend fun updateEntry(entryEntity: EntryEntity)
|
||||
|
||||
@Delete
|
||||
suspend fun deleteEntry(entryEntity: EntryEntity)
|
||||
|
||||
|
||||
@Query("SELECT * FROM transaction WHERE _id = :id")
|
||||
suspend fun findEntryById(id: Int) : EntryEntity
|
||||
|
||||
|
||||
@Query("SELECT * FROM transaction")
|
||||
fun getAllEntries(): Flow<List<EntryEntity>>
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package at.xaxa.ledger.data.db.Entry
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
@Database(entities = [EntryEntity::class], version = 1)
|
||||
abstract class EntryDatabase : RoomDatabase() {
|
||||
abstract fun EntryDao(): EntryDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var Instance: EntryDatabase? = null
|
||||
|
||||
fun getDatabase(context: Context): EntryDatabase {
|
||||
return Instance ?: synchronized(this){
|
||||
val instance =
|
||||
Room.databaseBuilder(context, EntryDatabase::class.java, "finance_database")
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
Instance = instance
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -10,5 +10,5 @@ data class EntryEntity(
|
||||
val name: String,
|
||||
val amount: Float,
|
||||
val date: Long,
|
||||
val category: Int
|
||||
val categoryName: Int
|
||||
)
|
@ -1,4 +1,4 @@
|
||||
package at.xaxa.ledger.data.db.Category
|
||||
package at.xaxa.ledger.data.db
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
@ -6,25 +6,45 @@ import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import at.xaxa.ledger.data.db.Category.CategoryEntity
|
||||
import at.xaxa.ledger.data.db.Entry.EntryEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CategoryDao {
|
||||
interface LedgerDao {
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun addCategory(entryEntity: CategoryEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun addEntry(entryEntity: EntryEntity)
|
||||
|
||||
@Update
|
||||
suspend fun updateCategory(category: CharCategory)
|
||||
|
||||
@Update
|
||||
suspend fun updateEntry(entryEntity: EntryEntity)
|
||||
|
||||
|
||||
@Delete
|
||||
suspend fun deleteCategory(category: CharCategory)
|
||||
|
||||
|
||||
@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<List<CategoryEntity>>
|
||||
|
||||
|
||||
@Query("SELECT * FROM transaction WHERE _id = :id")
|
||||
suspend fun findEntryById(id: Int) : EntryEntity
|
||||
|
||||
|
||||
@Query("SELECT * FROM transaction")
|
||||
fun getAllEntries(): Flow<List<EntryEntity>>
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package at.xaxa.ledger.data.db
|
||||
|
||||
import androidx.room.Database
|
||||
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)
|
||||
abstract class LedgerDatabase : RoomDatabase() {
|
||||
abstract fun entryDao(): EntryDao
|
||||
abstract fun categoryDao(): CategoryDao
|
||||
}
|
Loading…
Reference in New Issue
Block a user