init
This commit is contained in:
@@ -14,9 +14,7 @@ import cc.n0th1ng.tripmoney.data.dao.TripDao
|
||||
import cc.n0th1ng.tripmoney.data.entity.Category
|
||||
import cc.n0th1ng.tripmoney.data.entity.ExchangeRate
|
||||
import cc.n0th1ng.tripmoney.data.entity.Expense
|
||||
import cc.n0th1ng.tripmoney.data.entity.ExpenseDto
|
||||
import cc.n0th1ng.tripmoney.data.entity.Trip
|
||||
import cc.n0th1ng.tripmoney.screens.listexpense.toEpochMilli
|
||||
import cc.n0th1ng.tripmoney.utils.Currencies
|
||||
import cc.n0th1ng.tripmoney.utils.Icons
|
||||
import cc.n0th1ng.tripmoney.utils.colors
|
||||
@@ -26,9 +24,7 @@ import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Delay
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.Instant
|
||||
import java.time.LocalDate
|
||||
@@ -36,7 +32,6 @@ import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
import javax.inject.Singleton
|
||||
import kotlin.random.Random
|
||||
import kotlin.random.nextInt
|
||||
|
||||
@Database(
|
||||
entities = [Trip::class, Expense::class, Category::class, ExchangeRate::class],
|
||||
@@ -176,6 +171,51 @@ private class DatabasePrepopulator(
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy1",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy2",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy3",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy4",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy5",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy6",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy7",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy8",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
Category(
|
||||
name = "Zakupy9 ",
|
||||
icon = Icons.GROCERIES,
|
||||
color = colors.random()
|
||||
),
|
||||
)
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
@@ -193,7 +233,7 @@ private class DatabasePrepopulator(
|
||||
|
||||
|
||||
val expense = Expense(
|
||||
categoryId = Random.nextInt(1, 5),
|
||||
categoryId = Random.nextInt(1, sampleCategories.size),
|
||||
tripId = 1,
|
||||
amount = Random.nextDouble(0.1, 300.0),
|
||||
currency = Currencies.entries.random().name,
|
||||
|
||||
@@ -4,6 +4,7 @@ import androidx.paging.PagingSource
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Query
|
||||
import androidx.room.RewriteQueriesToDropUnusedColumns
|
||||
import androidx.room.Transaction
|
||||
import androidx.room.Upsert
|
||||
import cc.n0th1ng.tripmoney.data.entity.Category
|
||||
@@ -18,6 +19,8 @@ interface ExpenseDao {
|
||||
suspend fun insert(expense: Expense)
|
||||
|
||||
|
||||
@Transaction
|
||||
@RewriteQueriesToDropUnusedColumns
|
||||
@Query(
|
||||
"""
|
||||
SELECT * FROM expense
|
||||
@@ -87,13 +90,17 @@ interface ExpenseDao {
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT trip.budget - IFNULL(SUM(expense.amount * expense.rate), 0)
|
||||
SELECT
|
||||
CASE
|
||||
WHEN trip.budget = 0 THEN NULL
|
||||
ELSE trip.budget - IFNULL(SUM(expense.amount * expense.rate), 0)
|
||||
END
|
||||
FROM trip
|
||||
LEFT JOIN expense ON expense.trip_id = trip.id
|
||||
WHERE trip.id = :tripId
|
||||
"""
|
||||
)
|
||||
fun budgetLeft(tripId: Int): Double
|
||||
fun budgetLeft(tripId: Int): Double?
|
||||
|
||||
@Delete
|
||||
suspend fun delete(expense: Expense)
|
||||
|
||||
@@ -5,6 +5,7 @@ import androidx.room.ColumnInfo
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import androidx.room.Relation
|
||||
import java.time.LocalDateTime
|
||||
@@ -17,7 +18,8 @@ import java.time.LocalDateTime
|
||||
childColumns = arrayOf("category_id"),
|
||||
onUpdate = ForeignKey.CASCADE,
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)]
|
||||
)],
|
||||
indices = [Index(value = ["category_id"], unique = true)]
|
||||
)
|
||||
@Immutable
|
||||
data class Expense(
|
||||
|
||||
@@ -13,6 +13,7 @@ import cc.n0th1ng.tripmoney.data.entity.ExpenseDto
|
||||
import cc.n0th1ng.tripmoney.utils.Currencies
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import java.util.OptionalDouble
|
||||
import javax.inject.Inject
|
||||
|
||||
class ExpenseRepository @Inject constructor(
|
||||
@@ -20,7 +21,7 @@ class ExpenseRepository @Inject constructor(
|
||||
private val exchangeRateRepository: ExchangeRateRepository
|
||||
) {
|
||||
|
||||
fun getBudgetLeft(tripId: Int): Double {
|
||||
fun getBudgetLeft(tripId: Int): Double? {
|
||||
return expenseDao.budgetLeft(tripId)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user