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

Commit

Permalink
WIP: ivy-fp module
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 24, 2022
1 parent 1782f7a commit d805d14
Show file tree
Hide file tree
Showing 40 changed files with 280 additions and 76 deletions.
16 changes: 8 additions & 8 deletions app/src/main/java/com/ivy/wallet/domain/action/ExchangeAct.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ivy.wallet.domain.action

import arrow.core.Option
import com.ivy.wallet.domain.action.framework.FPAction
import com.ivy.fp.action.FPAction
import com.ivy.fp.then
import com.ivy.wallet.domain.pure.ExchangeData
import com.ivy.wallet.domain.pure.exchange
import com.ivy.wallet.io.persistence.dao.ExchangeRateDao
import java.math.BigDecimal
Expand All @@ -14,20 +16,18 @@ class ExchangeAct @Inject constructor(
override suspend fun Input.compose(): suspend () -> Option<BigDecimal> = suspend {
io {
exchange(
baseCurrencyCode = baseCurrency,
data = data,
amount = amount,
fromCurrencyCode = fromCurrency,
toCurrencyCode = toCurrency,
getExchangeRate = exchangeRateDao::findByBaseCurrencyAndCurrency
getExchangeRate = exchangeRateDao::findByBaseCurrencyAndCurrency then {
it?.toDomain()
}
)
}
}


data class Input(
val baseCurrency: String,
val fromCurrency: Option<String>,
val toCurrency: String,
val data: ExchangeData,
val amount: BigDecimal
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ivy.wallet.domain.action.account

import com.ivy.wallet.domain.action.framework.FPAction
import com.ivy.fp.action.FPAction
import com.ivy.fp.action.thenMap
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.io.persistence.dao.TransactionDao
Expand All @@ -22,6 +23,8 @@ class AccTrnsAct @Inject constructor(
endDate = range.to
)
}
} thenMap {
it.toDomain()
}

class Input(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ivy.wallet.domain.action.account

import com.ivy.wallet.domain.action.framework.FPAction
import com.ivy.fp.action.FPAction
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.io.persistence.dao.AccountDao
import javax.inject.Inject
Expand All @@ -9,6 +9,6 @@ class AccountsAct @Inject constructor(
private val accountDao: AccountDao
) : FPAction<Unit, List<Account>>() {
override suspend fun Unit.compose(): suspend () -> List<Account> = suspend {
io { accountDao.findAll() }
io { accountDao.findAll().map { it.toDomain() } }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.ivy.wallet.domain.action.account

import arrow.core.nonEmptyListOf
import com.ivy.wallet.domain.action.framework.FPAction
import com.ivy.wallet.domain.action.framework.then
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ivy.wallet.domain.action.charts

import com.ivy.wallet.domain.action.framework.FPAction
import com.ivy.fp.action.FPAction
import com.ivy.wallet.domain.action.wallet.CalcWalletBalanceAct
import com.ivy.wallet.domain.pure.charts.ChartPeriod
import com.ivy.wallet.domain.pure.charts.SingleChartPoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ivy.wallet.domain.action.settings

import com.ivy.wallet.domain.action.framework.FPAction
import com.ivy.fp.action.FPAction
import com.ivy.wallet.io.persistence.dao.SettingsDao
import javax.inject.Inject

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.wallet.withDateDividers
import com.ivy.wallet.io.persistence.dao.AccountDao
import javax.inject.Inject

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

override suspend fun Input.compose(): suspend () -> List<TransactionHistoryItem> = suspend {
transactions.withDateDividers(
baseCurrencyCode = baseCurrency,
getAccount = accountDao::findById then { it?.toDomain() },
exchange = { data, amount ->
exchangeAct(
ExchangeAct.Input(
data = data,
amount = amount
)
)
}
)
}

data class Input(
val baseCurrency: String,
val transactions: List<Transaction>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ivy.wallet.domain.action.transaction

import com.ivy.fp.action.FPAction
import com.ivy.fp.action.thenMap
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.io.persistence.dao.TransactionDao
import javax.inject.Inject

class HistoryTrnsAct @Inject constructor(
private val transactionDao: TransactionDao
) : FPAction<HistoryTrnsAct.Input, List<Transaction>>() {

override suspend fun Input.compose(): suspend () -> List<Transaction> = suspend {
io {
transactionDao.findAllBetween(
startDate = range.from,
endDate = range.to
)
}
} thenMap { it.toDomain() }

data class Input(
val range: ClosedTimeRange
)
}

This file was deleted.

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

import com.ivy.wallet.domain.action.framework.Action
import com.ivy.fp.action.Action
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.logic.WalletLogic
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ivy.wallet.domain.action.wallet

import com.ivy.wallet.domain.action.framework.Action
import com.ivy.fp.action.Action
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.logic.WalletLogic
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ivy.wallet.domain.action.wallet

import arrow.core.toOption
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.action.framework.*
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import java.math.BigDecimal
import javax.inject.Inject
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/ivy/wallet/domain/pure/Exchange.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import arrow.core.Option
import arrow.core.Some
import arrow.core.computations.option
import arrow.core.toOption
import com.ivy.fp.Pure
import com.ivy.fp.SideEffect
import com.ivy.wallet.domain.data.core.ExchangeRate
import com.ivy.wallet.domain.pure.core.Pure
import com.ivy.wallet.domain.pure.core.SideEffect
import com.ivy.wallet.utils.isNotNullOrBlank
import java.math.BigDecimal

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/ivy/wallet/domain/pure/IvyCore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.ivy.wallet.domain.pure

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.ivy.wallet.domain.pure.account

import arrow.core.NonEmptyList
import com.ivy.fp.Pure
import com.ivy.fp.Total
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.core.Pure
import com.ivy.wallet.domain.pure.core.Total
import com.ivy.wallet.domain.pure.core.calculateValueFunctionsSum
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.toFPTransaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ivy.wallet.domain.pure.charts

import com.ivy.wallet.domain.pure.core.Pure
import com.ivy.wallet.domain.pure.core.SideEffect
import com.ivy.fp.Pure
import com.ivy.fp.SideEffect
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.data.WalletDAOs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package com.ivy.wallet.domain.pure.wallet
import arrow.core.NonEmptyList
import arrow.core.Some
import arrow.core.toOption
import com.ivy.fp.SideEffect
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.data.core.ExchangeRate
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.ExchangeData
import com.ivy.wallet.domain.pure.account.AccountValueFunction
import com.ivy.wallet.domain.pure.account.calcAccValues
import com.ivy.wallet.domain.pure.core.SideEffect
import com.ivy.wallet.domain.pure.core.Uncertain
import com.ivy.wallet.domain.pure.core.mapIndexedNel
import com.ivy.wallet.domain.pure.core.nonEmptyListOfZeros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package com.ivy.wallet.domain.pure.wallet

import arrow.core.Option
import arrow.core.toOption
import com.ivy.fp.Pure
import com.ivy.fp.SideEffect
import com.ivy.wallet.domain.data.TransactionHistoryDateDivider
import com.ivy.wallet.domain.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.ExchangeData
import com.ivy.wallet.domain.pure.core.*
import com.ivy.wallet.domain.pure.core.expenses
import com.ivy.wallet.domain.pure.core.incomes
import com.ivy.wallet.domain.pure.core.sum
import com.ivy.wallet.domain.pure.core.toFPTransactions
import com.ivy.wallet.domain.pure.data.FPTransaction
import com.ivy.wallet.utils.convertUTCtoLocal
import com.ivy.wallet.utils.toEpochSeconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.ivy.wallet.io.persistence.data

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.ivy.wallet.domain.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.TransactionType
import com.ivy.wallet.domain.data.core.Transaction
import java.time.LocalDateTime
Expand Down Expand Up @@ -36,7 +35,7 @@ data class TransactionEntity(

@PrimaryKey
val id: UUID = UUID.randomUUID()
) : TransactionHistoryItem {
) {
fun toDomain(): Transaction = Transaction(
accountId = accountId,
type = type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.ivy.wallet.ui.charts

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ivy.fp.action.then
import com.ivy.wallet.domain.action.charts.BalanceChartAct
import com.ivy.wallet.domain.action.framework.then
import com.ivy.wallet.domain.action.settings.BaseCurrencyAct
import com.ivy.wallet.domain.data.TransactionType
import com.ivy.wallet.domain.data.core.Category
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/ivy/wallet/ui/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.ivy.wallet.ui.home
import androidx.lifecycle.viewModelScope
import com.ivy.design.l0_system.Theme
import com.ivy.design.navigation.Navigation
import com.ivy.design.viewmodel.IvyViewModel
import com.ivy.wallet.domain.action.transaction.HistoryWithDateDivAct
import com.ivy.fp.viewmodel.IvyViewModel
import com.ivy.wallet.domain.action.transaction.AddDateDividersAct
import com.ivy.wallet.domain.action.wallet.CalcOverdueAct
import com.ivy.wallet.domain.action.wallet.CalcUpcomingAct
import com.ivy.wallet.domain.action.wallet.CalcWalletBalanceAct
Expand Down Expand Up @@ -49,7 +49,7 @@ class HomeViewModel @Inject constructor(
private val calcAccountBalanceAct: CalcWalletBalanceAct,
private val calcUpcomingAct: CalcUpcomingAct,
private val calcOverdueAct: CalcOverdueAct,
private val historyWithDateDivAct: HistoryWithDateDivAct,
private val historyWithDateDivAct: AddDateDividersAct,
) : IvyViewModel<HomeState>() {
override val mutableState: MutableStateFlow<HomeState> = MutableStateFlow(
HomeState.initial(ivyWalletCtx = ivyContext)
Expand Down Expand Up @@ -154,7 +154,7 @@ class HomeViewModel @Inject constructor(
updateState {
it.copy(
history = historyWithDateDivAct(
HistoryWithDateDivAct.Input(
AddDateDividersAct.Input(
timeRange = timeRange.toCloseTimeRange(),
baseCurrencyCode = stateVal().baseCurrencyCode
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.ivy.design.navigation.Navigation
import com.ivy.design.viewmodel.IvyViewModel
import com.ivy.fp.viewmodel.IvyViewModel
import com.ivy.wallet.domain.data.TransactionType
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.data.core.Category
Expand Down
16 changes: 16 additions & 0 deletions buildSrc/src/main/java/com/ivy/wallet/buildsrc/dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun DependencyHandler.appModuleDependencies(
kotlinVersion: String = GlobalVersions.kotlinVersion
) {
implementation(project(":ivy-design"))
implementation(project(":ivy-fp"))

Kotlin(version = kotlinVersion)
Coroutines(version = "1.5.0")
Expand Down Expand Up @@ -91,6 +92,21 @@ fun DependencyHandler.ivyDesignModuleDependencies(
Lifecycle(version = "2.3.1")
}

fun DependencyHandler.ivyFPModuleDependencies(
kotlinVersion: String = GlobalVersions.kotlinVersion
) {
Kotlin(version = kotlinVersion)
Coroutines(version = "1.5.0")
FunctionalProgramming(
arrowVersion = "1.0.1",
kotestVersion = "5.1.0",
kotlinVersion = kotlinVersion
)

AndroidX()
Lifecycle(version = "2.3.1")
}


//---------------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions ivy-fp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading

0 comments on commit d805d14

Please sign in to comment.