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

Commit

Permalink
Re-work HomeViewModel.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 24, 2022
1 parent 490eef0 commit 985a49f
Show file tree
Hide file tree
Showing 26 changed files with 327 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ivy.wallet.domain.action.account

import com.ivy.fp.action.FPAction
import com.ivy.fp.action.then
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.io.persistence.dao.AccountDao
import java.util.*
import javax.inject.Inject

class AccountByIdAct @Inject constructor(
private val accountDao: AccountDao
) : FPAction<UUID, Account?>() {
override suspend fun UUID.compose(): suspend () -> Account? = suspend {
this //accountId
} then accountDao::findById then {
it?.toDomain()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ivy.wallet.domain.action
package com.ivy.wallet.domain.action.exchange

import arrow.core.Option
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.domain.pure.exchange.ExchangeData
import com.ivy.wallet.domain.pure.exchange.exchange
import com.ivy.wallet.io.persistence.dao.ExchangeRateDao
import java.math.BigDecimal
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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 DueTrnsAct @Inject constructor(
private val transactionDao: TransactionDao
) : FPAction<ClosedTimeRange, List<Transaction>>() {

override suspend fun ClosedTimeRange.compose(): suspend () -> List<Transaction> = suspend {
io {
transactionDao.findAllDueToBetween(
startDate = from,
endDate = to
)
}
} thenMap { it.toDomain() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ import javax.inject.Inject

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

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

data class Input(
val range: ClosedTimeRange
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HistoryWithDateDivsAct @Inject constructor(
) : FPAction<HistoryWithDateDivsAct.Input, List<TransactionHistoryItem>>() {

override suspend fun Input.compose(): suspend () -> List<TransactionHistoryItem> = suspend {
HistoryTrnsAct.Input(range)
range
} then historyTrnsAct then { trns ->
TrnsWithDateDivsAct.Input(
baseCurrency = baseCurrency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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.actInput
import com.ivy.wallet.domain.action.exchange.ExchangeAct
import com.ivy.wallet.domain.action.exchange.actInput
import com.ivy.wallet.domain.data.TransactionHistoryItem
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.transaction.transactionsWithDateDividers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.ivy.wallet.domain.action.viewmodel.home
import arrow.core.nonEmptyListOf
import arrow.core.toOption
import com.ivy.fp.action.FPAction
import com.ivy.fp.action.then
import com.ivy.fp.action.thenMap
import com.ivy.wallet.domain.action.ExchangeAct
import com.ivy.wallet.domain.action.account.AccTrnsAct
Expand All @@ -14,14 +13,13 @@ import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.transaction.AccountValueFunctions
import com.ivy.wallet.domain.pure.transaction.foldTransactions
import com.ivy.wallet.domain.pure.util.orZero
import java.math.BigDecimal
import javax.inject.Inject

class CalcBalanceIncsExpsAct @Inject constructor(
class CalcBalanceIncomeExpenseAct @Inject constructor(
private val accTrnsAct: AccTrnsAct,
private val exchangeAct: ExchangeAct
) : FPAction<CalcBalanceIncsExpsAct.Input, CalcBalanceIncsExpsAct.Output>() {
) : FPAction<CalcBalanceIncomeExpenseAct.Input, CalcBalanceIncomeExpenseAct.Output>() {

override suspend fun Input.compose(): suspend () -> Output = suspend {
filterExcluded(accounts)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.ivy.wallet.domain.action.viewmodel.home

import com.ivy.fp.action.FPAction
import com.ivy.fp.action.lambda
import com.ivy.fp.action.then
import com.ivy.fp.then
import com.ivy.wallet.domain.action.account.AccountByIdAct
import com.ivy.wallet.domain.action.exchange.ExchangeAct
import com.ivy.wallet.domain.action.exchange.actInput
import com.ivy.wallet.domain.action.transaction.DueTrnsAct
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.exchange.ExchangeTrnArgument
import com.ivy.wallet.domain.pure.exchange.exchangeInCurrency
import com.ivy.wallet.domain.pure.transaction.expenses
import com.ivy.wallet.domain.pure.transaction.incomes
import com.ivy.wallet.domain.pure.transaction.sumTrns
import com.ivy.wallet.utils.timeNowUTC
import java.time.LocalDateTime
import javax.inject.Inject

class DueTrnsInfoAct @Inject constructor(
private val dueTrnsAct: DueTrnsAct,
private val accountByIdAct: AccountByIdAct,
private val exchangeAct: ExchangeAct
) : FPAction<DueTrnsInfoAct.Input, DueTrnsInfoAct.Output>() {

override suspend fun Input.compose(): suspend () -> Output = suspend {
range
} then dueTrnsAct then { trns ->
val timeNow = timeNowUTC()
trns.filter {
this.dueFilter(it, timeNow)
}
} then { upcomingTrns ->
//We have due transactions in different currencies
val exchangeArg = ExchangeTrnArgument(
baseCurrency = baseCurrency,
exchange = ::actInput then exchangeAct,
getAccount = accountByIdAct.lambda()
)

io {
Output(
dueIncomeExpense = IncomeExpensePair(
income = sumTrns(
incomes(upcomingTrns),
::exchangeInCurrency,
exchangeArg
),
expense = sumTrns(
expenses(upcomingTrns),
::exchangeInCurrency,
exchangeArg
)
),
dueTrns = upcomingTrns
)
}
}

data class Input(
val range: ClosedTimeRange,
val baseCurrency: String,
val dueFilter: (Transaction, LocalDateTime) -> Boolean
)

data class Output(
val dueIncomeExpense: IncomeExpensePair,
val dueTrns: List<Transaction>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ivy.wallet.domain.action.viewmodel.home

import com.ivy.fp.action.FPAction
import com.ivy.wallet.io.persistence.dao.TransactionDao
import javax.inject.Inject

class HasTrnsAct @Inject constructor(
private val transactionDao: TransactionDao
) : FPAction<Unit, Boolean>() {
override suspend fun Unit.compose(): suspend () -> Boolean = suspend {
io {
transactionDao.findAll_LIMIT_1().isNotEmpty()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ivy.wallet.domain.action.viewmodel.home

import com.ivy.fp.action.FPAction
import com.ivy.fp.action.then
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.transaction.isOverdue
import javax.inject.Inject

class OverdueAct @Inject constructor(
private val dueTrnsInfoAct: DueTrnsInfoAct
) : FPAction<OverdueAct.Input, OverdueAct.Output>() {

override suspend fun Input.compose(): suspend () -> Output = suspend {
DueTrnsInfoAct.Input(
range = range,
baseCurrency = baseCurrency,
dueFilter = ::isOverdue
)
} then dueTrnsInfoAct then {
Output(
overdue = it.dueIncomeExpense,
overdueTrns = it.dueTrns
)
}

data class Input(
val range: ClosedTimeRange,
val baseCurrency: String
)

data class Output(
val overdue: IncomeExpensePair,
val overdueTrns: List<Transaction>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ivy.wallet.domain.action.viewmodel.home

import com.ivy.fp.action.FPAction
import com.ivy.fp.action.then
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.transaction.isUpcoming
import javax.inject.Inject

class UpcomingAct @Inject constructor(
private val dueTrnsInfoAct: DueTrnsInfoAct
) : FPAction<UpcomingAct.Input, UpcomingAct.Output>() {

override suspend fun Input.compose(): suspend () -> Output = suspend {
DueTrnsInfoAct.Input(
range = range,
baseCurrency = baseCurrency,
dueFilter = ::isUpcoming
)
} then dueTrnsInfoAct then {
Output(
upcoming = it.dueIncomeExpense,
upcomingTrns = it.dueTrns
)
}

data class Input(
val range: ClosedTimeRange,
val baseCurrency: String
)

data class Output(
val upcoming: IncomeExpensePair,
val upcomingTrns: List<Transaction>
)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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.pure.ExchangeData
import com.ivy.wallet.domain.action.exchange.ExchangeAct
import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.exchange.ExchangeData
import java.math.BigDecimal
import javax.inject.Inject

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ivy.wallet.domain.pure
package com.ivy.wallet.domain.pure.exchange

import arrow.core.None
import arrow.core.Option
Expand Down
Loading

0 comments on commit 985a49f

Please sign in to comment.