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

Commit

Permalink
WIP: Composition
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 24, 2022
1 parent d805d14 commit 5b4343f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/ivy/wallet/domain/action/ExchangeAct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import javax.inject.Inject
class ExchangeAct @Inject constructor(
private val exchangeRateDao: ExchangeRateDao,
) : FPAction<ExchangeAct.Input, Option<BigDecimal>>() {

override suspend fun Input.compose(): suspend () -> Option<BigDecimal> = suspend {
io {
exchange(
Expand All @@ -25,9 +24,16 @@ class ExchangeAct @Inject constructor(
}
}


data class Input(
val data: ExchangeData,
val amount: BigDecimal
)
}

fun exchangeActInput(
data: ExchangeData,
amount: BigDecimal
): ExchangeAct.Input = ExchangeAct.Input(
data = data,
amount = amount
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ivy.wallet.domain.action.transaction
import com.ivy.fp.action.FPAction
import com.ivy.fp.then
import com.ivy.wallet.domain.action.ExchangeAct
import com.ivy.wallet.domain.action.exchangeActInput
import com.ivy.wallet.domain.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.wallet.withDateDividers
Expand All @@ -18,14 +19,7 @@ class AddDateDividersAct @Inject constructor(
transactions.withDateDividers(
baseCurrencyCode = baseCurrency,
getAccount = accountDao::findById then { it?.toDomain() },
exchange = { data, amount ->
exchangeAct(
ExchangeAct.Input(
data = data,
amount = amount
)
)
}
exchange = ::exchangeActInput then exchangeAct
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.ivy.fp.action.*
import com.ivy.wallet.domain.action.ExchangeAct
import com.ivy.wallet.domain.action.account.AccountsAct
import com.ivy.wallet.domain.action.account.CalcAccBalanceAct
import com.ivy.wallet.domain.pure.ExchangeData
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import java.math.BigDecimal
import javax.inject.Inject
Expand All @@ -30,9 +31,11 @@ class CalcWalletBalanceAct @Inject constructor(
} thenMap {
exchangeAct(
ExchangeAct.Input(
baseCurrency = baseCurrency,
fromCurrency = it.account.currency.toOption(),
toCurrency = balanceCurrency,
data = ExchangeData(
baseCurrency = baseCurrency,
fromCurrency = it.account.currency.toOption(),
toCurrency = balanceCurrency
),
amount = it.balance
)
)
Expand Down
13 changes: 13 additions & 0 deletions ivy-fp/src/main/java/com/ivy/fp/Composition.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ivy.fp

import com.ivy.fp.action.Action

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
Expand Down Expand Up @@ -36,6 +38,17 @@ infix fun <A, B, C, D> ((A, B) -> C).then(f: (C) -> D): (A, B) -> D = { a, b ->
f(c)
}

infix fun <A, B, C, D> ((A, B) -> C).then(act: Action<C, D>): suspend (A, B) -> D = { a, b ->
val c = this(a, b)
act(c)
}

suspend infix fun <A, B, C, D> (suspend (A, B) -> C).then(f: suspend (C) -> D): suspend (A, B) -> D =
{ a, b ->
val c = this(a, b)
f(c)
}

infix fun <A, B, C, D, E> ((A, B, C) -> D).then(f: (D) -> E): (A, B, C) -> E = { a, b, c ->
val d = this(a, b, c)
f(d)
Expand Down

0 comments on commit 5b4343f

Please sign in to comment.