init
This commit is contained in:
@@ -120,6 +120,7 @@ private class DatabasePrepopulator(
|
||||
Trip(
|
||||
name = "Włochy",
|
||||
startDate = LocalDate.parse("2026-03-01"),
|
||||
endDate = LocalDate.parse("2026-03-15"),
|
||||
currency = "PLN"
|
||||
)
|
||||
)
|
||||
@@ -127,6 +128,7 @@ private class DatabasePrepopulator(
|
||||
Trip(
|
||||
name = "Szwajcaria",
|
||||
startDate = LocalDate.parse("2025-03-01"),
|
||||
endDate = LocalDate.parse("2025-03-15"),
|
||||
currency = "EUR"
|
||||
)
|
||||
)
|
||||
@@ -134,6 +136,7 @@ private class DatabasePrepopulator(
|
||||
Trip(
|
||||
name = "Portugalia",
|
||||
startDate = LocalDate.parse("2025-03-01"),
|
||||
endDate = LocalDate.parse("2025-03-15"),
|
||||
currency = "USD"
|
||||
)
|
||||
)
|
||||
|
||||
@@ -49,6 +49,14 @@ interface ExpenseDao {
|
||||
)
|
||||
fun expenseDto(tripId: Int, filter: String): Flow<List<ExpenseDto>>
|
||||
|
||||
@Query("""
|
||||
SELECT trip.budget - IFNULL(SUM(expense.amount * expense.rate), 0)
|
||||
FROM trip
|
||||
LEFT JOIN expense ON expense.trip_id = trip.id
|
||||
WHERE trip.id = :tripId
|
||||
""")
|
||||
fun budgetLeft(tripId: Int): Double
|
||||
|
||||
@Delete
|
||||
suspend fun delete(expense: Expense)
|
||||
}
|
||||
|
||||
@@ -15,11 +15,17 @@ data class Trip(
|
||||
@PrimaryKey(autoGenerate = true) val id: Int = 0,
|
||||
@ColumnInfo("name") val name: String,
|
||||
@ColumnInfo("start_date") val startDate: LocalDate,
|
||||
@ColumnInfo("end_date") val endDate: LocalDate,
|
||||
@ColumnInfo("currency") val currency: String,
|
||||
@ColumnInfo("budget") val budget: Double
|
||||
){
|
||||
@ColumnInfo("budget") val budget: Double = 0.0
|
||||
) {
|
||||
companion object {
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
val DUMMY = Trip(-1, "", LocalDate.now(), Currencies.default().name, budget = 0.0)
|
||||
val DUMMY = Trip(
|
||||
-1,
|
||||
"",
|
||||
LocalDate.now(),
|
||||
endDate = LocalDate.now(), Currencies.default().name, budget = 0.0,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,10 @@ class ExpenseRepository @Inject constructor(
|
||||
private val exchangeRateRepository: ExchangeRateRepository
|
||||
) {
|
||||
|
||||
fun getBudgetLeft(tripId: Int): Double {
|
||||
return expenseDao.budgetLeft(tripId)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
suspend fun save(expense: Expense) {
|
||||
expenseDao.insert(expense)
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package cc.n0th1ng.tripmoney.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import cc.n0th1ng.tripmoney.data.repository.PreferenceKeys.ADD_EXPENSE_SWITCH
|
||||
import cc.n0th1ng.tripmoney.data.repository.PreferenceKeys.APP_THEME
|
||||
import cc.n0th1ng.tripmoney.data.repository.PreferenceKeys.CURRENT_TRIP
|
||||
import cc.n0th1ng.tripmoney.data.repository.PreferenceKeys.DEFAULT_CURRENCY
|
||||
@@ -23,6 +25,7 @@ object PreferenceKeys {
|
||||
val APP_THEME = intPreferencesKey("app_theme")
|
||||
val CURRENT_TRIP = intPreferencesKey("current_trip")
|
||||
val DEFAULT_CURRENCY = stringPreferencesKey("default_currency")
|
||||
val ADD_EXPENSE_SWITCH = booleanPreferencesKey("add_expense_switch")
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +37,13 @@ class PreferencesRepository @Inject constructor(@ApplicationContext private val
|
||||
AppTheme.fromValue(value)
|
||||
}
|
||||
|
||||
val currentAddExpenseSwitchFlow: Flow<Boolean> =
|
||||
context.preferencesDataStore.data.map { prefs ->
|
||||
val value = prefs[ADD_EXPENSE_SWITCH]
|
||||
?: false
|
||||
value
|
||||
}
|
||||
|
||||
val currentTripFlow: Flow<Int> =
|
||||
context.preferencesDataStore.data.map { prefs ->
|
||||
prefs[CURRENT_TRIP] ?: -1
|
||||
@@ -61,6 +71,12 @@ class PreferencesRepository @Inject constructor(@ApplicationContext private val
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveAddExpenseSwitch(value: Boolean) {
|
||||
context.preferencesDataStore.edit { prefs ->
|
||||
prefs[ADD_EXPENSE_SWITCH] = value
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum class AppTheme(val value: Int) {
|
||||
|
||||
Reference in New Issue
Block a user