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.
WIP: Action composition (not building)
- Loading branch information
1 parent
2cf9c52
commit 8b22a51
Showing
12 changed files
with
232 additions
and
134 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ivy.wallet.domain.action | ||
|
||
abstract class FPAction<I, O> : Action<I, O>() { | ||
protected abstract suspend fun I.recipe(): (suspend () -> O) | ||
|
||
override suspend fun I.willDo(): O { | ||
return recipe().invoke() | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/ivy/wallet/domain/action/account/AccountsAct.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,14 @@ | ||
package com.ivy.wallet.domain.action.account | ||
|
||
import com.ivy.wallet.domain.action.FPAction | ||
import com.ivy.wallet.domain.data.entity.Account | ||
import com.ivy.wallet.io.persistence.dao.AccountDao | ||
import javax.inject.Inject | ||
|
||
class AccountsAct @Inject constructor( | ||
private val accountDao: AccountDao | ||
) : FPAction<Unit, List<Account>>() { | ||
override suspend fun Unit.recipe(): suspend () -> List<Account> = suspend { | ||
io { accountDao.findAll() } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/com/ivy/wallet/domain/action/settings/BaseCurrencyAct.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,16 @@ | ||
package com.ivy.wallet.domain.action.settings | ||
|
||
import com.ivy.wallet.domain.action.FPAction | ||
import com.ivy.wallet.domain.fp.wallet.baseCurrencyCode | ||
import com.ivy.wallet.io.persistence.dao.SettingsDao | ||
import javax.inject.Inject | ||
|
||
class BaseCurrencyAct @Inject constructor( | ||
private val settingsDao: SettingsDao | ||
) : FPAction<Unit, String>() { | ||
override suspend fun Unit.recipe(): suspend () -> String { | ||
return suspend { | ||
io { baseCurrencyCode(settingsDao) } | ||
} | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
app/src/main/java/com/ivy/wallet/domain/action/settings/GetBaseCurrencyAct.kt
This file was deleted.
Oops, something went wrong.
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
33 changes: 0 additions & 33 deletions
33
app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcAccountsBalanceAct.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/ivy/wallet/domain/action/wallet/CalcWalletBalanceAct.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,39 @@ | ||
package com.ivy.wallet.domain.action.wallet | ||
|
||
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.then | ||
import java.math.BigDecimal | ||
import javax.inject.Inject | ||
|
||
class CalcWalletBalanceAct @Inject constructor( | ||
private val accountsAct: AccountsAct, | ||
private val calcAccBalanceAct: CalcAccBalanceAct, | ||
private val exchangeAct: ExchangeAct, | ||
private val baseCurrencyAct: BaseCurrencyAct | ||
) : FPAction<CalcWalletBalanceAct.Input, BigDecimal>() { | ||
|
||
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 | ||
) | ||
) | ||
} | ||
} then { balances -> | ||
balances.sumOf { it.orNull() ?: BigDecimal.ZERO } | ||
}).fixUnit() | ||
|
||
data class Input( | ||
val currency: String | ||
) | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/ivy/wallet/domain/action/wallet/ExchangeAct.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,33 @@ | ||
package com.ivy.wallet.domain.action.wallet | ||
|
||
import arrow.core.Option | ||
import com.ivy.wallet.domain.action.FPAction | ||
import com.ivy.wallet.domain.fp.exchange | ||
import com.ivy.wallet.io.persistence.dao.ExchangeRateDao | ||
import java.math.BigDecimal | ||
import javax.inject.Inject | ||
|
||
class ExchangeAct @Inject constructor( | ||
private val exchangeRateDao: ExchangeRateDao, | ||
) : FPAction<ExchangeAct.Input, Option<BigDecimal>>() { | ||
|
||
override suspend fun Input.recipe(): suspend () -> Option<BigDecimal> = suspend { | ||
io { | ||
exchange( | ||
baseCurrencyCode = baseCurrency, | ||
exchangeRateDao = exchangeRateDao, | ||
fromAmount = amount, | ||
fromCurrencyCode = fromCurrency, | ||
toCurrencyCode = toCurrency | ||
) | ||
} | ||
} | ||
|
||
|
||
data class Input( | ||
val baseCurrency: String, | ||
val fromCurrency: Option<String>, | ||
val toCurrency: String, | ||
val amount: BigDecimal | ||
) | ||
} |
Oops, something went wrong.