This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #794 from Vishwa-Raghavendra/PieChartScreen
PieChartScreen Fix
- Loading branch information
Showing
7 changed files
with
211 additions
and
81 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...rc/main/java/com/ivy/wallet/domain/action/category/CategoryIncomeWithAccountFiltersAct.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.ivy.wallet.domain.action.category | ||
|
||
import com.ivy.fp.action.FPAction | ||
import com.ivy.fp.action.then | ||
import com.ivy.wallet.domain.action.transaction.CalcTrnsIncomeExpenseAct | ||
import com.ivy.wallet.domain.data.core.Account | ||
import com.ivy.wallet.domain.data.core.Category | ||
import com.ivy.wallet.domain.data.core.Transaction | ||
import com.ivy.wallet.domain.pure.data.IncomeExpensePair | ||
import javax.inject.Inject | ||
|
||
class CategoryIncomeWithAccountFiltersAct @Inject constructor( | ||
val calcTrnsIncomeExpenseAct: CalcTrnsIncomeExpenseAct | ||
) : FPAction<CategoryIncomeWithAccountFiltersAct.Input, IncomeExpensePair>() { | ||
|
||
override suspend fun Input.compose(): suspend () -> IncomeExpensePair = { | ||
val accountFilterSet = accountFilterList.map { it.id }.toHashSet() | ||
transactions.filter { | ||
it.categoryId == category?.id | ||
}.filter { | ||
if (accountFilterSet.isEmpty()) | ||
true | ||
else | ||
accountFilterSet.contains(it.accountId) | ||
} | ||
} then { | ||
CalcTrnsIncomeExpenseAct.Input( | ||
transactions = it, | ||
baseCurrency = baseCurrency, | ||
accounts = accountFilterList | ||
) | ||
} then calcTrnsIncomeExpenseAct | ||
|
||
data class Input( | ||
val transactions: List<Transaction>, | ||
val accountFilterList: List<Account>, | ||
val category: Category?, | ||
val baseCurrency: String | ||
) | ||
} |
108 changes: 108 additions & 0 deletions
108
app/src/main/java/com/ivy/wallet/domain/action/charts/PieChartAct.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.ivy.wallet.domain.action.charts | ||
|
||
import com.ivy.fp.action.FPAction | ||
import com.ivy.fp.action.then | ||
import com.ivy.wallet.domain.action.account.AccountsAct | ||
import com.ivy.wallet.domain.action.category.CategoriesAct | ||
import com.ivy.wallet.domain.action.category.CategoryIncomeWithAccountFiltersAct | ||
import com.ivy.wallet.domain.action.transaction.CalcTrnsIncomeExpenseAct | ||
import com.ivy.wallet.domain.action.transaction.TrnsWithRangeAndAccFiltersAct | ||
import com.ivy.wallet.domain.data.TransactionType | ||
import com.ivy.wallet.domain.pure.account.filterExcluded | ||
import com.ivy.wallet.ui.onboarding.model.FromToTimeRange | ||
import com.ivy.wallet.ui.statistic.level1.CategoryAmount | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
class PieChartAct @Inject constructor( | ||
private val accountsAct: AccountsAct, | ||
private val trnsWithRangeAndAccFiltersAct: TrnsWithRangeAndAccFiltersAct, | ||
private val calcTrnsIncomeExpenseAct: CalcTrnsIncomeExpenseAct, | ||
private val categoriesAct: CategoriesAct, | ||
private val categoryIncomeWithAccountFiltersAct: CategoryIncomeWithAccountFiltersAct | ||
) : FPAction<PieChartAct.Input, PieChartAct.Output>() { | ||
override suspend fun Input.compose(): suspend () -> Output = suspend { | ||
val allAccounts = accountsAct(Unit) | ||
val accountsUsed = if (accountIdFilterList.isEmpty()) | ||
allAccounts.let(::filterExcluded) | ||
else | ||
accountIdFilterList.mapNotNull { accID -> | ||
allAccounts.find { it.id == accID } | ||
} | ||
val accountIdFilterSet = accountsUsed.map { it.id }.toHashSet() | ||
|
||
Pair(accountsUsed, accountIdFilterSet) | ||
} then { | ||
val accountsUsed = it.first | ||
val accountIdFilterSet = it.second | ||
|
||
val transactions = trnsWithRangeAndAccFiltersAct( | ||
TrnsWithRangeAndAccFiltersAct.Input( | ||
range = range, | ||
accountIdFilterSet = accountIdFilterSet | ||
) | ||
) | ||
|
||
Pair(accountsUsed, transactions) | ||
} then { | ||
val accountsUsed = it.first | ||
val transactions = it.second | ||
|
||
val totalAmount = asyncIo { | ||
val incomeExpensePair = calcTrnsIncomeExpenseAct( | ||
CalcTrnsIncomeExpenseAct.Input( | ||
transactions = transactions, | ||
accounts = accountsUsed, | ||
baseCurrency = baseCurrency | ||
) | ||
) | ||
|
||
when (type) { | ||
TransactionType.INCOME -> incomeExpensePair.income.toDouble() | ||
TransactionType.EXPENSE -> incomeExpensePair.expense.toDouble() | ||
else -> error("not supported transactionType - $type") | ||
} | ||
} | ||
|
||
val categoryAmounts = asyncIo { | ||
val categories = categoriesAct(Unit) | ||
categories | ||
.plus(null) //for unspecified | ||
.map { category -> | ||
|
||
val catIncomeExpense = categoryIncomeWithAccountFiltersAct( | ||
CategoryIncomeWithAccountFiltersAct.Input( | ||
transactions = transactions, | ||
accountFilterList = accountsUsed, | ||
category = category, | ||
baseCurrency = baseCurrency | ||
) | ||
) | ||
|
||
CategoryAmount( | ||
category = category, | ||
amount = when (type) { | ||
TransactionType.INCOME -> catIncomeExpense.income.toDouble() | ||
TransactionType.EXPENSE -> catIncomeExpense.expense.toDouble() | ||
else -> error("not supported transactionType - $type") | ||
} | ||
) | ||
} | ||
.filter { catAmt -> | ||
catAmt.amount != 0.0 | ||
} | ||
.sortedByDescending { it.amount } | ||
} | ||
|
||
Output(totalAmount = totalAmount.await(), categoryAmounts = categoryAmounts.await()) | ||
} | ||
|
||
data class Input( | ||
val baseCurrency: String, | ||
val range: FromToTimeRange, | ||
val type: TransactionType, | ||
val accountIdFilterList: List<UUID> | ||
) | ||
|
||
data class Output(val totalAmount: Double, val categoryAmounts: List<CategoryAmount>) | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/ivy/wallet/domain/action/transaction/TrnsWithRangeAndAccFiltersAct.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ivy.wallet.domain.action.transaction | ||
|
||
import com.ivy.fp.action.FPAction | ||
import com.ivy.fp.action.thenFilter | ||
import com.ivy.wallet.domain.data.core.Transaction | ||
import com.ivy.wallet.io.persistence.dao.TransactionDao | ||
import com.ivy.wallet.ui.onboarding.model.FromToTimeRange | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
class TrnsWithRangeAndAccFiltersAct @Inject constructor( | ||
private val transactionDao: TransactionDao | ||
) : FPAction<TrnsWithRangeAndAccFiltersAct.Input, List<Transaction>>() { | ||
|
||
override suspend fun Input.compose(): suspend () -> List<Transaction> = suspend { | ||
transactionDao.findAllBetween(range.from(), range.to()) | ||
.map { it.toDomain() } | ||
} thenFilter { | ||
accountIdFilterSet.contains(it.accountId) | ||
} | ||
|
||
data class Input( | ||
val range: FromToTimeRange, | ||
val accountIdFilterSet: Set<UUID> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters