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

Develop #847

Merged
merged 7 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ class PieChartAct @Inject constructor(
val accountsUsed = it.first
val accountIdFilterSet = it.second

val transactions = trnsWithRangeAndAccFiltersAct(
TrnsWithRangeAndAccFiltersAct.Input(
range = range,
accountIdFilterSet = accountIdFilterSet
val transactions = existingTransactions.ifEmpty {
trnsWithRangeAndAccFiltersAct(
TrnsWithRangeAndAccFiltersAct.Input(
range = range,
accountIdFilterSet = accountIdFilterSet
)
)
)
}

Pair(accountsUsed, transactions)
} then {
Expand All @@ -67,17 +69,30 @@ class PieChartAct @Inject constructor(
)
)

val categoryAmounts = calculateCategoryAmounts(
type = type,
baseCurrency = baseCurrency,
allCategories = suspend {
categoriesAct(Unit).plus(null) //for unspecified
},
transactions = suspend { transactions },
accountsUsed = suspend { accountsUsed }
)
val categoryAmounts = suspend {
calculateCategoryAmounts(
type = type,
baseCurrency = baseCurrency,
allCategories = suspend {
categoriesAct(Unit).plus(null) //for unspecified
},
transactions = suspend { transactions },
accountsUsed = suspend { accountsUsed },
addAssociatedTransToCategoryAmt = existingTransactions.isNotEmpty()
)
} then {
addAccountTransfersCategory(
showAccountTransfersCategory = showAccountTransfersCategory,
type = type,
accountTransfersCategory = accountTransfersCategory,
accountIdFilterSet = accountIdFilterList.toHashSet(),
incomeExpenseTransfer = suspend { incomeExpenseTransfer },
categoryAmounts = suspend { it },
transactions = suspend { transactions }
)
}

Pair(incomeExpenseTransfer, categoryAmounts)
Pair(incomeExpenseTransfer, categoryAmounts())
} then {

val totalAmount = calculateTotalAmount(
Expand All @@ -86,13 +101,7 @@ class PieChartAct @Inject constructor(
incomeExpenseTransfer = suspend { it.first }
)

val catAmountList = addAccountTransfersCategory(
treatTransferAsIncExp = treatTransferAsIncExp,
type = type,
incomeExpenseTransfer = suspend { it.first },
accountTransfersCategory = accountTransfersCategory,
categoryAmounts = suspend { it.second }
)
val catAmountList = it.second

Pair(totalAmount, catAmountList)
} then {
Expand Down Expand Up @@ -123,6 +132,7 @@ class PieChartAct @Inject constructor(
private suspend fun calculateCategoryAmounts(
type: TransactionType,
baseCurrency: String,
addAssociatedTransToCategoryAmt: Boolean = false,

@SideEffect
allCategories: suspend () -> List<Category?>,
Expand All @@ -137,6 +147,15 @@ class PieChartAct @Inject constructor(
val accUsed = accountsUsed()

val catAmtList = allCategories thenMap { category ->
val categoryTransactions = asyncIo {
if (addAssociatedTransToCategoryAmt)
trans.filter {
it.type == type && it.categoryId == category?.id
}
else
emptyList()
}

val catIncomeExpense = categoryIncomeWithAccountFiltersAct(
CategoryIncomeWithAccountFiltersAct.Input(
transactions = trans,
Expand All @@ -152,7 +171,9 @@ class PieChartAct @Inject constructor(
TransactionType.INCOME -> catIncomeExpense.income.toDouble()
TransactionType.EXPENSE -> catIncomeExpense.expense.toDouble()
else -> error("not supported transactionType - $type")
}
},
associatedTransactions = categoryTransactions.await(),
isCategoryUnspecified = category == null
)
} thenFilter { catAmt ->
catAmt.amount != 0.0
Expand Down Expand Up @@ -193,9 +214,13 @@ class PieChartAct @Inject constructor(

@Pure
private suspend fun addAccountTransfersCategory(
treatTransferAsIncExp: Boolean,
showAccountTransfersCategory: Boolean,
type: TransactionType,
accountTransfersCategory: Category,
accountIdFilterSet: Set<UUID>,

@SideEffect
transactions: suspend () -> List<Transaction>,

@SideEffect
incomeExpenseTransfer: suspend () -> IncomeExpenseTransferPair,
Expand All @@ -207,7 +232,7 @@ class PieChartAct @Inject constructor(
val incExpQuad = incomeExpenseTransfer()

val catAmtList =
if (!treatTransferAsIncExp || incExpQuad.transferIncome == BigDecimal.ZERO && incExpQuad.transferExpense == BigDecimal.ZERO)
if (!showAccountTransfersCategory || incExpQuad.transferIncome == BigDecimal.ZERO && incExpQuad.transferExpense == BigDecimal.ZERO)
categoryAmounts then { it.sortedByDescending { ca -> ca.amount } }
else {

Expand All @@ -216,10 +241,23 @@ class PieChartAct @Inject constructor(
else
incExpQuad.transferExpense.toDouble()

val categoryTrans = transactions().filter {
it.type == TransactionType.TRANSFER && it.categoryId == null
}.filter {
if (type == TransactionType.EXPENSE)
accountIdFilterSet.contains(it.accountId)
else
accountIdFilterSet.contains(it.toAccountId)
}

categoryAmounts then {
it.plus(
CategoryAmount(accountTransfersCategory, amt)
CategoryAmount(
category = accountTransfersCategory,
amount = amt,
associatedTransactions = categoryTrans,
isCategoryUnspecified = true
)
)
} then {
it.sortedByDescending { ca -> ca.amount }
Expand All @@ -234,7 +272,9 @@ class PieChartAct @Inject constructor(
val range: FromToTimeRange,
val type: TransactionType,
val accountIdFilterList: List<UUID>,
val treatTransferAsIncExp: Boolean = false
val treatTransferAsIncExp: Boolean = false,
val showAccountTransfersCategory: Boolean = treatTransferAsIncExp,
val existingTransactions: List<Transaction> = emptyList()
)

data class Output(val totalAmount: Double, val categoryAmounts: List<CategoryAmount>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.ivy.design.navigation.Navigation
import com.ivy.fp.filterSuspend
import com.ivy.fp.sumOfSuspend
import com.ivy.fp.viewmodel.IvyViewModel
import com.ivy.wallet.R
import com.ivy.wallet.domain.action.account.AccountsAct
import com.ivy.wallet.domain.action.category.CategoriesAct
import com.ivy.wallet.domain.action.exchange.ExchangeAct
Expand All @@ -25,6 +26,7 @@ import com.ivy.wallet.domain.pure.transaction.trnCurrency
import com.ivy.wallet.domain.pure.util.orZero
import com.ivy.wallet.io.persistence.dao.SettingsDao
import com.ivy.wallet.io.persistence.dao.TransactionDao
import com.ivy.wallet.stringRes
import com.ivy.wallet.ui.IvyWalletCtx
import com.ivy.wallet.ui.RootActivity
import com.ivy.wallet.ui.onboarding.model.TimePeriod
Expand Down Expand Up @@ -56,7 +58,8 @@ class ReportViewModel @Inject constructor(
override val mutableState: MutableStateFlow<ReportScreenState> = MutableStateFlow(
ReportScreenState()
)
private val unSpecifiedCategory = Category("UnSpecified", color = Gray.toArgb())
private val unSpecifiedCategory =
Category(stringRes(R.string.unspecified), color = Gray.toArgb())

private val _period = MutableLiveData<TimePeriod>()
val period = _period.asLiveData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import com.ivy.wallet.domain.data.core.Transaction
data class CategoryAmount(
val category: Category?,
val amount: Double,
val associatedTransactions: List<Transaction> = emptyList()
val associatedTransactions: List<Transaction> = emptyList(),
val isCategoryUnspecified: Boolean = false
)
Loading