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

Commit

Permalink
Implement declarative wallet balance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 24, 2022
1 parent 9ad7b53 commit 60e95fb
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ivy.wallet.domain.action

suspend infix fun <A, B> (suspend (A) -> List<B>).thenFilter(
predicate: (B) -> Boolean
): suspend (A) -> List<B> =
{ a ->
val list = this(a)
list.filter(predicate)
}

suspend infix fun <A, B> (Action<A, List<B>>).thenFilter(
predicate: (B) -> Boolean
): suspend (A) -> List<B> =
{ a ->
val list = this(a)
list.filter(predicate)
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/ivy/wallet/domain/action/CompositionMap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ivy.wallet.domain.action

suspend infix fun <A, B, C> (suspend (A) -> List<B>).thenMap(
transform: suspend (B) -> C
): suspend (A) -> List<C> =
{ a ->
val list = this(a)
list.map {
transform(it)
}
}

suspend infix fun <A, B, C> (Action<A, List<B>>).thenMap(
transform: suspend (B) -> C
): suspend (A) -> List<C> =
{ a ->
val list = this(a)
list.map {
transform(it)
}
}
29 changes: 29 additions & 0 deletions app/src/main/java/com/ivy/wallet/domain/action/CompositionSum.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ivy.wallet.domain.action

import java.math.BigDecimal

suspend infix fun <A, B> (suspend (A) -> List<B>).thenSum(
value: (B) -> BigDecimal
): suspend (A) -> BigDecimal =
{ a ->
val list = this(a)
list.fold(
initial = BigDecimal.ZERO,
operation = { acc, b ->
acc + value(b)
}
)
}

suspend infix fun <A, B> (Action<A, List<B>>).thenSum(
value: (B) -> BigDecimal
): suspend (A) -> BigDecimal =
{ a ->
val list = this(a)
list.fold(
initial = BigDecimal.ZERO,
operation = { acc, b ->
acc + value(b)
}
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.ivy.wallet.domain.action.wallet

import arrow.core.toOption
import com.ivy.wallet.domain.action.FPAction
import com.ivy.wallet.domain.action.*
import com.ivy.wallet.domain.action.account.AccountsAct
import com.ivy.wallet.domain.action.fixUnit
import com.ivy.wallet.domain.action.then
import java.math.BigDecimal
import javax.inject.Inject

Expand All @@ -17,23 +15,21 @@ class CalcWalletBalanceAct @Inject constructor(
override suspend fun Input.compose(): suspend () -> BigDecimal = recipe().fixUnit()

private suspend fun Input.recipe(): suspend (Unit) -> BigDecimal =
accountsAct then {
it.filter { acc -> acc.includeInBalance }
} then {
it.map { acc -> calcAccBalanceAct(CalcAccBalanceAct.Input(acc)) }
} then {
it.map { balanceOutput ->
exchangeAct(
ExchangeAct.Input(
baseCurrency = baseCurrency,
fromCurrency = balanceOutput.account.currency.toOption(),
toCurrency = balanceCurrency,
amount = balanceOutput.balance
)
accountsAct thenFilter {
it.includeInBalance
} thenMap {
calcAccBalanceAct(CalcAccBalanceAct.Input(it))
} thenMap {
exchangeAct(
ExchangeAct.Input(
baseCurrency = baseCurrency,
fromCurrency = it.account.currency.toOption(),
toCurrency = balanceCurrency,
amount = it.balance
)
}
} then { balances ->
balances.sumOf { it.orNull() ?: BigDecimal.ZERO }
)
} thenSum {
it.orNull() ?: BigDecimal.ZERO
}

data class Input(
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/com/ivy/wallet/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ class HomeViewModel @Inject constructor(

val timeRange = period.toRange(ivyContext.startDayOfMonth)

//TODO: Fix that!
// updateState {
// it.copy(
// balance = calcAccountBalanceAct(settings.currency)
// )
// }
updateState {
it.copy(
balance = calcAccountBalanceAct(
CalcWalletBalanceAct.Input(
baseCurrency = settings.currency,
balanceCurrency = settings.currency
)
)
)
}

updateState {
it.copy(
Expand Down

0 comments on commit 60e95fb

Please sign in to comment.