From 6c5890ffdb32333aede526be07bb0147d3484cce Mon Sep 17 00:00:00 2001 From: Vishwa Raghavendra K S Date: Thu, 12 May 2022 00:33:33 +0530 Subject: [PATCH] AccountsTransferCategory Display Improvements and BugFixes --- .../level2/ItemStatisticViewModel.kt | 61 ++++++------------- 1 file changed, 19 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/ivy/wallet/ui/statistic/level2/ItemStatisticViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/statistic/level2/ItemStatisticViewModel.kt index 7c1b13f7f3..6b29f224a1 100644 --- a/app/src/main/java/com/ivy/wallet/ui/statistic/level2/ItemStatisticViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/statistic/level2/ItemStatisticViewModel.kt @@ -1,5 +1,6 @@ package com.ivy.wallet.ui.statistic.level2 +import androidx.compose.ui.graphics.toArgb import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import arrow.core.toOption @@ -13,6 +14,7 @@ import com.ivy.wallet.domain.action.account.CalcAccIncomeExpenseAct import com.ivy.wallet.domain.action.category.CategoriesAct import com.ivy.wallet.domain.action.exchange.ExchangeAct import com.ivy.wallet.domain.action.settings.BaseCurrencyAct +import com.ivy.wallet.domain.action.transaction.CalcTrnsIncomeExpenseAct import com.ivy.wallet.domain.action.transaction.TrnsWithDateDivsAct import com.ivy.wallet.domain.data.TransactionHistoryItem import com.ivy.wallet.domain.data.TransactionType @@ -32,6 +34,7 @@ import com.ivy.wallet.ui.ItemStatistic import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.onboarding.model.TimePeriod import com.ivy.wallet.ui.onboarding.model.toCloseTimeRange +import com.ivy.wallet.ui.theme.RedLight import com.ivy.wallet.utils.* import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow @@ -66,6 +69,7 @@ class ItemStatisticViewModel @Inject constructor( private val baseCurrencyAct: BaseCurrencyAct, private val calcAccBalanceAct: CalcAccBalanceAct, private val calcAccIncomeExpenseAct: CalcAccIncomeExpenseAct, + private val calcTrnsIncomeExpenseAct: CalcTrnsIncomeExpenseAct, private val exchangeAct: ExchangeAct ) : ViewModel() { @@ -159,7 +163,8 @@ class ItemStatisticViewModel @Inject constructor( _categories.value = categoriesAct(Unit) _accounts.value = accountsAct(Unit) _initWithTransactions.value = false - _treatTransfersAsIncomeExpense.value = sharedPrefs.getBoolean(SharedPrefs.TRANSFERS_AS_INCOME_EXPENSE, false) + _treatTransfersAsIncomeExpense.value = + sharedPrefs.getBoolean(SharedPrefs.TRANSFERS_AS_INCOME_EXPENSE, false) when { screen.accountId != null -> { @@ -178,7 +183,7 @@ class ItemStatisticViewModel @Inject constructor( ) } screen.unspecifiedCategory == true && screen.transactions.isNotEmpty() -> { - initForUnspecifiedCategoryWithTransactions( + initForAccountTransfersCategory( screen.categoryId, screen.accountIdFilterList, screen.transactions @@ -453,59 +458,31 @@ class ItemStatisticViewModel @Inject constructor( _overdue.value = ioThread { categoryLogic.overdueUnspecified(range) } } - private suspend fun initForUnspecifiedCategoryWithTransactions( + private suspend fun initForAccountTransfersCategory( categoryId: UUID?, accountFilterList: List, transactions: List ) { _initWithTransactions.value = true + _category.value = Category(stringRes(R.string.account_transfers), RedLight.toArgb(), "transfer") val accountFilterIdSet = accountFilterList.toHashSet() - - val accountTransferCategoryEnabled = categoryId != null - if (accountTransferCategoryEnabled) - _category.value = Category(stringRes(R.string.account_transfers)) - val trans = transactions.filter { it.categoryId == null && (accountFilterIdSet.contains(it.accountId) || accountFilterIdSet.contains( it.toAccountId - )) - } - - val incomeTrans = trans.filter { - it.type == TransactionType.INCOME + )) && it.type == TransactionType.TRANSFER } - val expenseTrans = trans.filter { - it.type == TransactionType.EXPENSE - } - - _income.value = ioThread { - categoryLogic.calculateCategoryIncome( - incomeTransaction = incomeTrans, - accountFilterSet = accountFilterIdSet + val historyIncomeExpense = calcTrnsIncomeExpenseAct( + CalcTrnsIncomeExpenseAct.Input( + transactions = trans, + accounts = accountFilterList.mapNotNull { accID -> accounts.value.find { it.id == accID } }, + baseCurrency = baseCurrency.value ) - } - - _expenses.value = ioThread { - categoryLogic.calculateCategoryExpenses( - expenseTransactions = expenseTrans, - accountFilterSet = accountFilterIdSet - ) - } - - _balance.value = scopedIOThread { - _income.value - _expenses.value + - if (accountTransferCategoryEnabled) - trans.filter { it.type == TransactionType.TRANSFER } - .sumOf { - exchangeRatesLogic.toAmountBaseCurrency( - transaction = it, - baseCurrency = baseCurrency.value, - accounts = accountsAct(Unit) - ) - } else 0.0 - } + ) + _income.value = historyIncomeExpense.transferIncome.toDouble() + _expenses.value = historyIncomeExpense.transferExpense.toDouble() + _balance.value = _income.value - _expenses.value _history.value = trnsWithDateDivsAct( TrnsWithDateDivsAct.Input( baseCurrency = baseCurrency.value,