Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #796 from mhss1/develop
Browse files Browse the repository at this point in the history
Converted Room Dao functions to suspend functions.
  • Loading branch information
ILIYANGERMANOV authored Apr 26, 2022
2 parents 3c26a65 + 4a7f9c0 commit e5ed4b4
Show file tree
Hide file tree
Showing 36 changed files with 204 additions and 193 deletions.
3 changes: 2 additions & 1 deletion app/src/androidTest/java/com/ivy/wallet/HiltTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import dagger.hilt.android.testing.HiltTestApplication
// A custom runner to set up the instrumented application class for tests.
class HiltTestRunner : AndroidJUnitRunner() {

override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context): Application {
IvyAndroidApp.appContext = context
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.ivy.wallet.ui.RootActivity
import com.ivy.wallet.utils.*
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -98,7 +99,7 @@ abstract class IvyComposeTest {
SharedPrefs(context()).removeAll()
}

private fun resetDatabase() {
private fun resetDatabase() = runTest {
ivyRoomDatabase.reset()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AccountCreator(
color = data.color.toArgb(),
icon = data.icon,
includeInBalance = data.includeBalance,
orderNum = accountDao.findMaxOrderNum() + 1,
orderNum = accountDao.findMaxOrderNum() + 1.0,
isSynced = false
)
accountDao.save(account.toEntity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CustomerJourneyLogic(
private val ivyContext: IvyWalletCtx
) {

fun loadCards(): List<CustomerJourneyCardData> {
suspend fun loadCards(): List<CustomerJourneyCardData> {
val trnCount = transactionDao.countHappenedTransactions()
val plannedPaymentsCount = plannedPaymentRuleDao.countPlannedPayments()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PlannedPaymentsGenerator(
private const val GENERATED_INSTANCES_LIMIT = 72
}

fun generate(rule: PlannedPaymentRule) {
suspend fun generate(rule: PlannedPaymentRule) {
//delete all not happened transactions
transactionDao.flagDeletedByRecurringRuleIdAndNoDateTime(
recurringRuleId = rule.id
Expand All @@ -26,15 +26,15 @@ class PlannedPaymentsGenerator(
}
}

private fun generateOneTime(rule: PlannedPaymentRule) {
private suspend fun generateOneTime(rule: PlannedPaymentRule) {
val trns = transactionDao.findAllByRecurringRuleId(recurringRuleId = rule.id)

if (trns.isEmpty()) {
generateTransaction(rule, rule.startDate!!)
}
}

private fun generateRecurring(rule: PlannedPaymentRule) {
private suspend fun generateRecurring(rule: PlannedPaymentRule) {
val startDate = rule.startDate!!
val endDate = startDate.plusYears(3)

Expand Down Expand Up @@ -69,7 +69,7 @@ class PlannedPaymentsGenerator(
}
}

private fun generateTransaction(rule: PlannedPaymentRule, dueDate: LocalDateTime) {
private suspend fun generateTransaction(rule: PlannedPaymentRule, dueDate: LocalDateTime) {
transactionDao.save(
Transaction(
type = rule.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PlannedPaymentsLogic(
private const val AVG_DAYS_IN_MONTH = 30.436875
}

fun plannedPaymentsAmountFor(range: FromToTimeRange): Double {
suspend fun plannedPaymentsAmountFor(range: FromToTimeRange): Double {
val baseCurrency = settingsDao.findFirst().currency
val accounts = accountDao.findAll()

Expand All @@ -53,11 +53,11 @@ class PlannedPaymentsLogic(
}
}

fun oneTime(): List<PlannedPaymentRule> {
suspend fun oneTime(): List<PlannedPaymentRule> {
return plannedPaymentRuleDao.findAllByOneTime(oneTime = true).map { it.toDomain() }
}

fun oneTimeIncome(): Double {
suspend fun oneTimeIncome(): Double {
return oneTime()
.filter { it.type == TransactionType.INCOME }
.sumByDoublePlannedInBaseCurrency(
Expand All @@ -67,7 +67,7 @@ class PlannedPaymentsLogic(
)
}

fun oneTimeExpenses(): Double {
suspend fun oneTimeExpenses(): Double {
return oneTime()
.filter { it.type == TransactionType.EXPENSE }
.sumByDoublePlannedInBaseCurrency(
Expand All @@ -77,22 +77,22 @@ class PlannedPaymentsLogic(
)
}

fun recurring(): List<PlannedPaymentRule> =
suspend fun recurring(): List<PlannedPaymentRule> =
plannedPaymentRuleDao.findAllByOneTime(oneTime = false).map { it.toDomain() }

fun recurringIncome(): Double {
suspend fun recurringIncome(): Double {
return recurring()
.filter { it.type == TransactionType.INCOME }
.sumByDoubleRecurringForMonthInBaseCurrency()
}

fun recurringExpenses(): Double {
suspend fun recurringExpenses(): Double {
return recurring()
.filter { it.type == TransactionType.EXPENSE }
.sumByDoubleRecurringForMonthInBaseCurrency()
}

private fun Iterable<PlannedPaymentRule>.sumByDoubleRecurringForMonthInBaseCurrency(): Double {
private suspend fun Iterable<PlannedPaymentRule>.sumByDoubleRecurringForMonthInBaseCurrency(): Double {
val accounts = accountDao.findAll()
val baseCurrency = settingsDao.findFirst().currency

Expand All @@ -105,7 +105,7 @@ class PlannedPaymentsLogic(
}
}

private fun amountForMonthInBaseCurrency(
private suspend fun amountForMonthInBaseCurrency(
plannedPayment: PlannedPaymentRule,
baseCurrency: String,
accounts: List<Account>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PreloadDataLogic(
return accounts.size < 2
}

fun preloadAccounts() {
suspend fun preloadAccounts() {
val cash = Account(
name = stringRes(R.string.cash),
currency = null,
Expand Down Expand Up @@ -71,7 +71,7 @@ class PreloadDataLogic(
),
)

fun preloadCategories() {
suspend fun preloadCategories() {
categoryOrderNum = 0.0

val categoriesToPreload = preloadCategoriesCreateData()
Expand Down Expand Up @@ -143,7 +143,7 @@ class PreloadDataLogic(
),
)

private fun preloadCategory(
private suspend fun preloadCategory(
data: CreateCategoryData
) {
val category = Category(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SmartTitleSuggestionsLogic(
* - most used titles for categories
* - if suggestions.size < SUGGESTIONS_LIMIT most used titles for accounts
*/
fun suggest(
suspend fun suggest(
title: String?,
categoryId: UUID?,
accountId: UUID?
Expand Down Expand Up @@ -100,7 +100,7 @@ private fun List<Transaction>.extractUniqueTitles(
}

@Deprecated("Use FP style, look into `domain.fp` package")
private fun Set<String>.sortedByMostUsedFirst(countUses: (String) -> Long): Set<String> {
private suspend fun Set<String>.sortedByMostUsedFirst(countUses: suspend (String) -> Long): Set<String> {
val titleCountMap = this
.map {
it to countUses(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ class WalletAccountLogic(
private val settingsDao: SettingsDao
) {

fun adjustBalance(
suspend fun adjustBalance(
account: Account,
actualBalance: Double = calculateAccountBalance(account),
actualBalance: Double? = null,
newBalance: Double,

adjustTransactionTitle: String = "Adjust balance",

isFiat: Boolean? = null,
trnIsSyncedFlag: Boolean = false, //TODO: Remove this once Bank Integration trn sync is properly implemented
) {
val diff = actualBalance - newBalance
val ab = actualBalance ?: calculateAccountBalance(account)
val diff = ab - newBalance

val finalDiff = if (isFiat == true && abs(diff) < 0.009) 0.0 else diff
when {
Expand Down Expand Up @@ -68,7 +69,7 @@ class WalletAccountLogic(
}
}

fun calculateAccountBalance(
suspend fun calculateAccountBalance(
account: Account,
before: LocalDateTime? = null
): Double {
Expand All @@ -81,7 +82,7 @@ class WalletAccountLogic(
)
}

private fun calculateIncomeWithTransfers(
private suspend fun calculateIncomeWithTransfers(
account: Account,
before: LocalDateTime?
): Double {
Expand All @@ -102,7 +103,7 @@ class WalletAccountLogic(
)
}

private fun calculateExpensesWithTransfers(
private suspend fun calculateExpensesWithTransfers(
account: Account,
before: LocalDateTime?
): Double {
Expand Down Expand Up @@ -135,7 +136,7 @@ class WalletAccountLogic(
}
}

fun calculateAccountIncome(account: Account, range: FromToTimeRange): Double =
suspend fun calculateAccountIncome(account: Account, range: FromToTimeRange): Double =
transactionDao
.findAllByTypeAndAccountBetween(
type = TransactionType.INCOME,
Expand All @@ -146,7 +147,7 @@ class WalletAccountLogic(
.filter { it.dateTime != null }
.sumOf { it.amount }

fun calculateAccountExpenses(account: Account, range: FromToTimeRange): Double =
suspend fun calculateAccountExpenses(account: Account, range: FromToTimeRange): Double =
transactionDao
.findAllByTypeAndAccountBetween(
type = TransactionType.EXPENSE,
Expand All @@ -157,27 +158,27 @@ class WalletAccountLogic(
.filter { it.dateTime != null }
.sumOf { it.amount }

fun calculateUpcomingIncome(account: Account, range: FromToTimeRange): Double =
suspend fun calculateUpcomingIncome(account: Account, range: FromToTimeRange): Double =
upcoming(account, range = range)
.filter { it.type == TransactionType.INCOME }
.sumOf { it.amount.toDouble() }

fun calculateUpcomingExpenses(account: Account, range: FromToTimeRange): Double =
suspend fun calculateUpcomingExpenses(account: Account, range: FromToTimeRange): Double =
upcoming(account = account, range = range)
.filter { it.type == TransactionType.EXPENSE }
.sumOf { it.amount.toDouble() }

fun calculateOverdueIncome(account: Account, range: FromToTimeRange): Double =
suspend fun calculateOverdueIncome(account: Account, range: FromToTimeRange): Double =
overdue(account, range = range)
.filter { it.type == TransactionType.INCOME }
.sumOf { it.amount.toDouble() }

fun calculateOverdueExpenses(account: Account, range: FromToTimeRange): Double =
suspend fun calculateOverdueExpenses(account: Account, range: FromToTimeRange): Double =
overdue(account, range = range)
.filter { it.type == TransactionType.EXPENSE }
.sumOf { it.amount.toDouble() }

fun upcoming(account: Account, range: FromToTimeRange): List<Transaction> {
suspend fun upcoming(account: Account, range: FromToTimeRange): List<Transaction> {
return transactionDao.findAllDueToBetweenByAccount(
accountId = account.id,
startDate = range.upcomingFrom(),
Expand All @@ -188,7 +189,7 @@ class WalletAccountLogic(
}


fun overdue(account: Account, range: FromToTimeRange): List<Transaction> {
suspend fun overdue(account: Account, range: FromToTimeRange): List<Transaction> {
return transactionDao.findAllDueToBetweenByAccount(
accountId = account.id,
startDate = range.from(),
Expand Down
Loading

0 comments on commit e5ed4b4

Please sign in to comment.