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 #837 from Vishwa-Raghavendra/PieChartScreen
Browse files Browse the repository at this point in the history
Pie chart screen Refactor
  • Loading branch information
ILIYANGERMANOV authored May 3, 2022
2 parents 49d8741 + 9e7c1eb commit 8971a96
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 377 deletions.
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 @@ -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

0 comments on commit 8971a96

Please sign in to comment.