feat: Add User and Expense entities with DAOs
This commit introduces the `User` and `Expense` data classes as Room entities. Each entity is accompanied by its corresponding Data Access Object (DAO) with methods for `getAll`, `insert`, and `delete` operations. Additionally, unused comments were removed from `MainViewModel` and an unnecessary preview was removed from `MainActivity`.
This commit is contained in:
@@ -37,6 +37,7 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.miaurizius.shap_planner.UserPreferences
|
||||
import de.miaurizius.shap_planner.entities.Account
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
package de.miaurizius.shap_planner.entities
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(tableName = "expenses")
|
||||
data class Expense (
|
||||
val id: UUID,
|
||||
val amt: Double,
|
||||
@@ -10,3 +18,15 @@ data class Expense (
|
||||
val payerId: UUID,
|
||||
val debtors: List<User>
|
||||
)
|
||||
|
||||
@Dao
|
||||
interface ExpenseDao {
|
||||
@Query("SELECT * FROM expenses")
|
||||
fun getAllExpenses(): Flow<List<Expense>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertExpense(expense: Expense)
|
||||
|
||||
@Delete
|
||||
suspend fun deleteExpense(expense: Expense)
|
||||
}
|
||||
@@ -1,8 +1,28 @@
|
||||
package de.miaurizius.shap_planner.entities
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(tableName = "users")
|
||||
data class User (
|
||||
val id: UUID,
|
||||
val name: String,
|
||||
)
|
||||
|
||||
@Dao
|
||||
interface UserDao {
|
||||
@Query("SELECT * FROM users")
|
||||
fun getAllUsers(): Flow<List<User>>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertUser(user: User)
|
||||
|
||||
@Delete
|
||||
suspend fun deleteUser(user: User)
|
||||
}
|
||||
@@ -15,11 +15,9 @@ import kotlin.collections.emptyList
|
||||
|
||||
class MainViewModel(private val accountDao: AccountDao) : ViewModel() {
|
||||
|
||||
// Das ist der State, den dein SetContent beobachtet
|
||||
val accounts: StateFlow<List<Account>> = accountDao.getAllAccounts()
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
|
||||
// Funktion zum Hinzufügen (z.B. nach Login)
|
||||
fun addAccount(account: Account) {
|
||||
viewModelScope.launch {
|
||||
accountDao.insertAccount(account)
|
||||
|
||||
Reference in New Issue
Block a user