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

Commit

Permalink
WIP: Clean-up domain.pure
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 24, 2022
1 parent 63837c4 commit ea91845
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 873 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import arrow.core.nonEmptyListOf
import com.ivy.fp.action.FPAction
import com.ivy.fp.action.then
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.pure.account.AccountValueFunctions
import com.ivy.wallet.domain.pure.account.calcAccValues
import com.ivy.wallet.domain.pure.AccountValueFunctions
import com.ivy.wallet.domain.pure.calcValues
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import java.math.BigDecimal
import javax.inject.Inject
Expand All @@ -20,9 +20,9 @@ class CalcAccBalanceAct @Inject constructor(
range = range
)
} then accTrnsAct then { accTrns ->
calcAccValues(
accountId = account.id,
accountsTrns = accTrns,
calcValues(
transactions = accTrns,
arg = account.id,
valueFunctions = nonEmptyListOf(AccountValueFunctions::balance)
).head
} then { balance ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import com.ivy.wallet.domain.action.ExchangeAct
import com.ivy.wallet.domain.action.actInput
import com.ivy.wallet.domain.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.wallet.withDateDividers
import com.ivy.wallet.domain.pure.transaction.transactionsWithDateDividers
import com.ivy.wallet.io.persistence.dao.AccountDao
import javax.inject.Inject

class AddDateDividersAct @Inject constructor(
class TrnsWithDateDividersAct @Inject constructor(
private val accountDao: AccountDao,
private val exchangeAct: ExchangeAct
) : FPAction<AddDateDividersAct.Input, List<TransactionHistoryItem>>() {
) : FPAction<TrnsWithDateDividersAct.Input, List<TransactionHistoryItem>>() {

override suspend fun Input.compose(): suspend () -> List<TransactionHistoryItem> = suspend {
transactions.withDateDividers(
transactionsWithDateDividers(
transactions = transactions,
baseCurrencyCode = baseCurrency,

getAccount = accountDao::findById then { it?.toDomain() },
exchange = ::actInput then exchangeAct
)
Expand Down
26 changes: 5 additions & 21 deletions app/src/main/java/com/ivy/wallet/domain/data/core/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.ivy.wallet.domain.data.core

import com.ivy.wallet.domain.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.TransactionType
import java.math.BigDecimal
import java.time.LocalDateTime
import java.util.*

data class Transaction(
val accountId: UUID,
val type: TransactionType,
val amount: Double,
val amount: BigDecimal,
val toAccountId: UUID? = null,
val toAmount: Double? = null,
val toAmount: BigDecimal,
val title: String? = null,
val description: String? = null,
val dateTime: LocalDateTime? = null,
Expand All @@ -25,24 +26,7 @@ data class Transaction(
val loanId: UUID? = null,

//This refers to the loan record id that is linked with a transaction
val loanRecordId:UUID? = null,

val isSynced: Boolean = false,
val isDeleted: Boolean = false,
val loanRecordId: UUID? = null,

val id: UUID = UUID.randomUUID()
) : TransactionHistoryItem {

fun isIdenticalWith(transaction: Transaction?): Boolean {
if (transaction == null) return false

//Set isSynced && isDeleted to false so they aren't accounted in the equals check
return this.copy(
isSynced = false,
isDeleted = false
) == transaction.copy(
isSynced = false,
isDeleted = false
)
}
}
) : TransactionHistoryItem
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.ivy.wallet.domain.pure.account
package com.ivy.wallet.domain.pure

import com.ivy.wallet.domain.data.TransactionType
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.core.ValueFunction
import com.ivy.wallet.domain.pure.data.FPTransaction
import java.math.BigDecimal
import java.util.*

typealias AccountValueFunction = ValueFunction<UUID>

object AccountValueFunctions {
fun balance(
fpTransaction: FPTransaction,
transaction: Transaction,
accountId: UUID
): BigDecimal = with(fpTransaction) {
): BigDecimal = with(transaction) {
if (this.accountId == accountId) {
//Account's transactions
when (type) {
TransactionType.INCOME -> amount
TransactionType.EXPENSE -> amount.negate()
TransactionType.TRANSFER -> {
if (toAccountId.orNull() != accountId) {
if (toAccountId != accountId) {
//transfer to another account
amount.negate()
} else {
Expand All @@ -30,39 +30,39 @@ object AccountValueFunctions {
}
} else {
//potential transfer to account?
toAccountId.orNull()?.takeIf { it == accountId } ?: return BigDecimal.ZERO
toAccountId?.takeIf { it == accountId } ?: return BigDecimal.ZERO
toAmount
}
}

fun income(
fpTransaction: FPTransaction,
transaction: Transaction,
accountId: UUID
): BigDecimal = with(fpTransaction) {
): BigDecimal = with(transaction) {
if (this.accountId == accountId && type == TransactionType.INCOME)
amount else BigDecimal.ZERO
}

fun expense(
fpTransaction: FPTransaction,
transaction: Transaction,
accountId: UUID
): BigDecimal = with(fpTransaction) {
): BigDecimal = with(transaction) {
if (this.accountId == accountId && type == TransactionType.EXPENSE)
amount else BigDecimal.ZERO
}

fun incomeCount(
fpTransaction: FPTransaction,
transaction: Transaction,
accountId: UUID
): BigDecimal = with(fpTransaction) {
): BigDecimal = with(transaction) {
if (this.accountId == accountId && type == TransactionType.INCOME)
BigDecimal.ONE else BigDecimal.ZERO
}

fun expenseCount(
fpTransaction: FPTransaction,
transaction: Transaction,
accountId: UUID
): BigDecimal = with(fpTransaction) {
): BigDecimal = with(transaction) {
if (this.accountId == accountId && type == TransactionType.EXPENSE)
BigDecimal.ONE else BigDecimal.ZERO
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.ivy.wallet.domain.pure.category
package com.ivy.wallet.domain.pure

import arrow.core.Option
import arrow.core.toOption
import com.ivy.fp.SideEffect
import com.ivy.wallet.domain.data.TransactionType
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.core.SuspendValueFunction
import com.ivy.wallet.domain.pure.data.FPTransaction
import com.ivy.wallet.domain.pure.exchangeToBaseCurrency
import com.ivy.wallet.io.persistence.dao.ExchangeRateDao
import java.math.BigDecimal
import java.util.*

Expand All @@ -16,17 +15,17 @@ typealias CategoryValueFunction = SuspendValueFunction<CategoryValueFunctions.Ar
object CategoryValueFunctions {
data class Argument(
val categoryId: UUID?,

val accounts: List<Account>,
val exchangeRateDao: ExchangeRateDao,
val baseCurrencyCode: String,

@SideEffect
val exchangeToBaseCurrency: suspend (Option<String>, BigDecimal) -> Option<BigDecimal>
)

suspend fun balance(
fpTransaction: FPTransaction,
transaction: Transaction,
arg: Argument,
): BigDecimal = with(fpTransaction) {
if (this.categoryId.orNull() == arg.categoryId) {
): BigDecimal = with(transaction) {
if (this.categoryId == arg.categoryId) {
when (type) {
TransactionType.INCOME -> amount.toBaseCurrencyOrZero(arg, accountId)
TransactionType.EXPENSE -> amount.toBaseCurrencyOrZero(arg, accountId).negate()
Expand All @@ -36,10 +35,10 @@ object CategoryValueFunctions {
}

suspend fun income(
fpTransaction: FPTransaction,
transaction: Transaction,
arg: Argument,
): BigDecimal = with(fpTransaction) {
if (this.categoryId.orNull() == arg.categoryId) {
): BigDecimal = with(transaction) {
if (this.categoryId == arg.categoryId) {
when (type) {
TransactionType.INCOME -> amount.toBaseCurrencyOrZero(arg, accountId)
else -> BigDecimal.ZERO
Expand All @@ -48,10 +47,10 @@ object CategoryValueFunctions {
}

suspend fun expense(
fpTransaction: FPTransaction,
transaction: Transaction,
arg: Argument,
): BigDecimal = with(fpTransaction) {
if (this.categoryId.orNull() == arg.categoryId) {
): BigDecimal = with(transaction) {
if (this.categoryId == arg.categoryId) {
when (type) {
TransactionType.EXPENSE -> amount.toBaseCurrencyOrZero(arg, accountId)
else -> BigDecimal.ZERO
Expand All @@ -60,10 +59,10 @@ object CategoryValueFunctions {
}

suspend fun incomeCount(
fpTransaction: FPTransaction,
transaction: Transaction,
arg: Argument,
): BigDecimal = with(fpTransaction) {
if (this.categoryId.orNull() == arg.categoryId) {
): BigDecimal = with(transaction) {
if (this.categoryId == arg.categoryId) {
when (type) {
TransactionType.INCOME -> BigDecimal.ONE
else -> BigDecimal.ZERO
Expand All @@ -72,10 +71,10 @@ object CategoryValueFunctions {
}

suspend fun expenseCount(
fpTransaction: FPTransaction,
transaction: Transaction,
arg: Argument,
): BigDecimal = with(fpTransaction) {
if (this.categoryId.orNull() == arg.categoryId) {
): BigDecimal = with(transaction) {
if (this.categoryId == arg.categoryId) {
when (type) {
TransactionType.EXPENSE -> BigDecimal.ONE
else -> BigDecimal.ZERO
Expand All @@ -84,24 +83,20 @@ object CategoryValueFunctions {
}

private suspend fun BigDecimal.toBaseCurrencyOrZero(
argument: Argument,
arg: Argument,
accountId: UUID
): BigDecimal {
return this.convertToBaseCurrency(
argument = argument,
arg = arg,
accountId = accountId
).orNull() ?: BigDecimal.ZERO
}

private suspend fun BigDecimal.convertToBaseCurrency(
accountId: UUID,
argument: Argument
arg: Argument
): Option<BigDecimal> {
return exchangeToBaseCurrency(
exchangeRateDao = argument.exchangeRateDao,
baseCurrencyCode = argument.baseCurrencyCode,
fromAmount = this,
fromCurrencyCode = argument.accounts.find { it.id == accountId }?.currency.toOption()
)
val trnCurrency = arg.accounts.find { it.id == accountId }?.currency.toOption()
return arg.exchangeToBaseCurrency(trnCurrency, this)
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/ivy/wallet/domain/pure/IvyCore.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
package com.ivy.wallet.domain.pure

import arrow.core.NonEmptyList
import com.ivy.fp.Pure
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.core.ValueFunction
import com.ivy.wallet.domain.pure.core.calculateValueFunctionsSum
import java.math.BigDecimal

@Pure
fun <Arg> calcValues(
transactions: List<Transaction>,
valueFunctions: NonEmptyList<ValueFunction<Arg>>,
arg: Arg
): NonEmptyList<BigDecimal> {
return calculateValueFunctionsSum(
valueFunctionArgument = arg,
transactions = transactions,
valueFunctions = valueFunctions
)
}
Loading

0 comments on commit ea91845

Please sign in to comment.