diff --git a/app/src/main/java/com/ivy/wallet/domain/action/Action.kt b/app/src/main/java/com/ivy/wallet/domain/action/Action.kt index b920f51ad6..5de7d11243 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/Action.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/Action.kt @@ -23,81 +23,4 @@ abstract class Action { withContext(Dispatchers.Main) { return@withContext action() } -} - -infix fun Action.after(act1: Action): Action = object : Action() { - override suspend fun A.willDo(): C { - val b = act1(this@willDo) //A -> B - return this@after(b) //B -> C - } -} - -infix fun Action.then(act2: Action): Action = object : Action() { - override suspend fun A.willDo(): C { - val b = this@then(this) - return act2(b) - } -} - -suspend infix fun Action.after(lambda: suspend (A) -> B): suspend (A) -> C = { a -> - val b = lambda(a) - this@after(b) -} - -suspend infix fun (suspend (B) -> C).after(lambda: suspend (A) -> B): suspend (A) -> C = - { a -> - val b = lambda(a) - this@after(b) - } - -suspend infix fun (suspend (A) -> B).then(lambda: suspend (B) -> C): suspend (A) -> C = - { a -> - val b = this@then(a) - lambda(b) - } - -suspend infix fun (suspend () -> B).then(lambda: suspend (B) -> C): suspend () -> C = - { - val b = this@then() - lambda(b) - } - -suspend infix fun (suspend () -> B).then(act: Action): suspend () -> C = - { - val b = this@then() - act(b) - } - -suspend infix fun (suspend (A) -> B).then(act: Action): suspend (A) -> C = - { a -> - val b = this@then(a) - act(b) - } - -suspend infix fun (Action).then(f: suspend (B) -> C): suspend (A) -> C = - { a -> - val b = this@then(a) - f(b) - } - - -suspend infix fun (() -> B).then(f: suspend (B) -> C): suspend () -> C = - { - val b = this@then() - f(b) - } - -fun (() -> C).fixUnit(): suspend (Unit) -> C = - { - this() - } - -fun (suspend () -> C).fixUnit(): suspend (Unit) -> C = - { - this() - } - -fun (suspend (Unit) -> C).fixUnit(): suspend () -> C = - { - this(Unit) - } \ No newline at end of file +} \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/domain/action/Composition.kt b/app/src/main/java/com/ivy/wallet/domain/action/Composition.kt new file mode 100644 index 0000000000..f9f39e51cf --- /dev/null +++ b/app/src/main/java/com/ivy/wallet/domain/action/Composition.kt @@ -0,0 +1,54 @@ +package com.ivy.wallet.domain.action + + +suspend infix fun (suspend (A) -> B).then(f: suspend (B) -> C): suspend (A) -> C = + { a -> + val b = this@then(a) + f(b) + } + +suspend infix fun (suspend () -> B).then(f: suspend (B) -> C): suspend () -> C = + { + val b = this@then() + f(b) + } + +suspend infix fun (suspend (A) -> B).then(act: Action): suspend (A) -> C = + { a -> + val b = this@then(a) + act(b) + } + +suspend infix fun (suspend () -> B).then(act: Action): suspend () -> C = + { + val b = this@then() + act(b) + } + +suspend infix fun (Action).then(f: suspend (B) -> C): suspend (A) -> C = + { a -> + val b = this@then(a) + f(b) + } + + +suspend infix fun (() -> B).then(f: suspend (B) -> C): suspend () -> C = + { + val b = this@then() + f(b) + } + +fun (() -> C).fixUnit(): suspend (Unit) -> C = + { + this() + } + +fun (suspend () -> C).fixUnit(): suspend (Unit) -> C = + { + this() + } + +fun (suspend (Unit) -> C).fixUnit(): suspend () -> C = + { + this(Unit) + } \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/domain/action/CompositionUtils.kt b/app/src/main/java/com/ivy/wallet/domain/action/CompositionUtils.kt new file mode 100644 index 0000000000..8f66a37365 --- /dev/null +++ b/app/src/main/java/com/ivy/wallet/domain/action/CompositionUtils.kt @@ -0,0 +1,9 @@ +package com.ivy.wallet.domain.action + +suspend infix fun (suspend (Any) -> List).thenFilter( + predicate: (A) -> Boolean +): suspend (Any) -> List = + { a -> + val list = this(a) + list.filter(predicate) + } diff --git a/app/src/main/java/com/ivy/wallet/domain/action/FPAction.kt b/app/src/main/java/com/ivy/wallet/domain/action/FPAction.kt index f0b370473b..716623ab68 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/FPAction.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/FPAction.kt @@ -1,9 +1,9 @@ package com.ivy.wallet.domain.action abstract class FPAction : Action() { - protected abstract suspend fun I.recipe(): (suspend () -> O) + protected abstract suspend fun I.compose(): (suspend () -> O) override suspend fun I.willDo(): O { - return recipe().invoke() + return compose().invoke() } } \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/domain/action/account/AccTrnsAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/account/AccTrnsAct.kt index 364fcb044e..3f18483b40 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/account/AccTrnsAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/account/AccTrnsAct.kt @@ -10,7 +10,7 @@ import javax.inject.Inject class AccTrnsAct @Inject constructor( private val transactionDao: TransactionDao ) : FPAction>() { - override suspend fun Input.recipe(): suspend () -> List = suspend { + override suspend fun Input.compose(): suspend () -> List = suspend { io { transactionDao.findAllByAccountAndBetween( accountId = accountId, diff --git a/app/src/main/java/com/ivy/wallet/domain/action/account/AccountsAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/account/AccountsAct.kt index c76212dd16..2921d3406f 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/account/AccountsAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/account/AccountsAct.kt @@ -8,7 +8,7 @@ import javax.inject.Inject class AccountsAct @Inject constructor( private val accountDao: AccountDao ) : FPAction>() { - override suspend fun Unit.recipe(): suspend () -> List = suspend { + override suspend fun Unit.compose(): suspend () -> List = suspend { io { accountDao.findAll() } } } \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/domain/action/settings/BaseCurrencyAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/settings/BaseCurrencyAct.kt index b3bc6f668a..fdc5294578 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/settings/BaseCurrencyAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/settings/BaseCurrencyAct.kt @@ -8,7 +8,7 @@ import javax.inject.Inject class BaseCurrencyAct @Inject constructor( private val settingsDao: SettingsDao ) : FPAction() { - override suspend fun Unit.recipe(): suspend () -> String { + override suspend fun Unit.compose(): suspend () -> String { return suspend { io { baseCurrencyCode(settingsDao) } } diff --git a/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcAccBalanceAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcAccBalanceAct.kt index eb6387a76c..01751a3105 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcAccBalanceAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcAccBalanceAct.kt @@ -15,7 +15,7 @@ class CalcAccBalanceAct @Inject constructor( private val accTrnsAct: AccTrnsAct ) : FPAction() { - override suspend fun Input.recipe(): suspend () -> Output = suspend { + override suspend fun Input.compose(): suspend () -> Output = suspend { AccTrnsAct.Input( accountId = account.id, range = ClosedTimeRange.allTimeIvy() diff --git a/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcWalletBalanceAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcWalletBalanceAct.kt index 818cc00b59..599a4b6079 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcWalletBalanceAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcWalletBalanceAct.kt @@ -1,8 +1,9 @@ package com.ivy.wallet.domain.action.wallet +import arrow.core.toOption import com.ivy.wallet.domain.action.FPAction import com.ivy.wallet.domain.action.account.AccountsAct -import com.ivy.wallet.domain.action.settings.BaseCurrencyAct +import com.ivy.wallet.domain.action.fixUnit import com.ivy.wallet.domain.action.then import java.math.BigDecimal import javax.inject.Inject @@ -11,29 +12,32 @@ class CalcWalletBalanceAct @Inject constructor( private val accountsAct: AccountsAct, private val calcAccBalanceAct: CalcAccBalanceAct, private val exchangeAct: ExchangeAct, - private val baseCurrencyAct: BaseCurrencyAct ) : FPAction() { - override suspend fun Input.recipe(): suspend () -> BigDecimal = (accountsAct then { - it.filter { acc -> acc.includeInBalance } - } then { - it.map { acc -> calcAccBalanceAct(CalcAccBalanceAct.Input(acc)) } - } then baseCurrencyAct then { - it.map { balanceOutput -> - exchangeAct( - ExchangeAct.Input( - baseCurrency =, - fromCurrency = balanceOutput.account.currency.toOption(), - toCurrency =, - amount = balanceOutput.balance + 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 + ) ) - ) + } + } then { balances -> + balances.sumOf { it.orNull() ?: BigDecimal.ZERO } } - } then { balances -> - balances.sumOf { it.orNull() ?: BigDecimal.ZERO } - }).fixUnit() data class Input( - val currency: String + val baseCurrency: String, + val balanceCurrency: String ) } diff --git a/app/src/main/java/com/ivy/wallet/domain/action/wallet/ExchangeAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/wallet/ExchangeAct.kt index d00ae3e75d..cf756d705d 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/wallet/ExchangeAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/wallet/ExchangeAct.kt @@ -11,7 +11,7 @@ class ExchangeAct @Inject constructor( private val exchangeRateDao: ExchangeRateDao, ) : FPAction>() { - override suspend fun Input.recipe(): suspend () -> Option = suspend { + override suspend fun Input.compose(): suspend () -> Option = suspend { io { exchange( baseCurrencyCode = baseCurrency,