diff --git a/app/src/main/java/com/ivy/wallet/domain/action/loan/LoanByIdAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/loan/LoanByIdAct.kt new file mode 100644 index 0000000000..9e771cc9cd --- /dev/null +++ b/app/src/main/java/com/ivy/wallet/domain/action/loan/LoanByIdAct.kt @@ -0,0 +1,15 @@ +package com.ivy.wallet.domain.action.loan + +import com.ivy.fp.action.FPAction +import com.ivy.wallet.domain.data.core.Loan +import com.ivy.wallet.io.persistence.dao.LoanDao +import java.util.* +import javax.inject.Inject + +class LoanByIdAct @Inject constructor( + private val loanDao: LoanDao +) : FPAction() { + override suspend fun UUID.compose(): suspend () -> Loan? = suspend { + loanDao.findById(this)?.toDomain() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/domain/action/loan/LoansAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/loan/LoansAct.kt new file mode 100644 index 0000000000..86c2c8fc23 --- /dev/null +++ b/app/src/main/java/com/ivy/wallet/domain/action/loan/LoansAct.kt @@ -0,0 +1,15 @@ +package com.ivy.wallet.domain.action.loan + +import com.ivy.fp.action.FPAction +import com.ivy.fp.action.thenMap +import com.ivy.wallet.domain.data.core.Loan +import com.ivy.wallet.io.persistence.dao.LoanDao +import javax.inject.Inject + +class LoansAct @Inject constructor( + private val loanDao: LoanDao +) : FPAction>() { + override suspend fun Unit.compose(): suspend () -> List = suspend { + loanDao.findAll() + } thenMap { it.toDomain() } +} \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/domain/action/viewmodel/home/CalcBalanceIncomeExpenseAct.kt b/app/src/main/java/com/ivy/wallet/domain/action/viewmodel/home/CalcBalanceIncomeExpenseAct.kt index 22a530d4f5..250a940fb4 100644 --- a/app/src/main/java/com/ivy/wallet/domain/action/viewmodel/home/CalcBalanceIncomeExpenseAct.kt +++ b/app/src/main/java/com/ivy/wallet/domain/action/viewmodel/home/CalcBalanceIncomeExpenseAct.kt @@ -3,16 +3,18 @@ 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 +import com.ivy.wallet.domain.action.exchange.ExchangeAct import com.ivy.wallet.domain.data.core.Account -import com.ivy.wallet.domain.pure.ExchangeData import com.ivy.wallet.domain.pure.account.filterExcluded import com.ivy.wallet.domain.pure.data.ClosedTimeRange import com.ivy.wallet.domain.pure.data.IncomeExpensePair +import com.ivy.wallet.domain.pure.exchange.ExchangeData 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 diff --git a/app/src/main/java/com/ivy/wallet/ui/charts/ChartsViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/charts/ChartsViewModel.kt index 439a2f4933..b18cc06f0f 100644 --- a/app/src/main/java/com/ivy/wallet/ui/charts/ChartsViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/charts/ChartsViewModel.kt @@ -7,13 +7,12 @@ import com.ivy.wallet.domain.action.charts.BalanceChartAct import com.ivy.wallet.domain.action.settings.BaseCurrencyAct import com.ivy.wallet.domain.data.TransactionType import com.ivy.wallet.domain.data.core.Category -import com.ivy.wallet.domain.logic.WalletCategoryLogic +import com.ivy.wallet.domain.deprecated.logic.WalletCategoryLogic import com.ivy.wallet.domain.pure.charts.ChartPeriod import com.ivy.wallet.domain.pure.charts.IncomeExpenseChartPoint import com.ivy.wallet.domain.pure.charts.SingleChartPoint import com.ivy.wallet.domain.pure.charts.incomeExpenseChart import com.ivy.wallet.domain.pure.data.WalletDAOs -import com.ivy.wallet.domain.pure.wallet.baseCurrencyCode import com.ivy.wallet.io.persistence.dao.CategoryDao import com.ivy.wallet.io.persistence.dao.SettingsDao import com.ivy.wallet.ui.onboarding.model.FromToTimeRange @@ -75,7 +74,7 @@ class ChartsViewModel @Inject constructor( fun start() { viewModelScope.launch { - _baseCurrencyCode.value = ioThread { baseCurrencyCode(settingsDao) } +// _baseCurrencyCode.value = ioThread { baseCurrencyCode(settingsDao) } walletCharts(period = period.value) } @@ -137,16 +136,16 @@ class ChartsViewModel @Inject constructor( period: ChartPeriod, category: Category ) { - _categoryExpenseValues.value = categoryExpenseValues.loadCategoryValue( - period = period, - category = category, - calculateValue = { range -> - walletCategoryLogic.calculateCategoryExpenses( - category = category, - range = range - ).absoluteValue - } - ) +// _categoryExpenseValues.value = categoryExpenseValues.loadCategoryValue( +// period = period, +// category = category, +// calculateValue = { range -> +// walletCategoryLogic.calculateCategoryExpenses( +// category = category, +// range = range +// ).absoluteValue +// } +// ) } private suspend fun loadCategoryExpenseCount( diff --git a/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt index 1087eb0fc5..5cec1d883a 100644 --- a/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt @@ -6,13 +6,13 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.ivy.design.navigation.Navigation -import com.ivy.wallet.domain.logic.csv.CSVImporter -import com.ivy.wallet.domain.logic.csv.CSVMapper -import com.ivy.wallet.domain.logic.csv.CSVNormalizer -import com.ivy.wallet.domain.logic.csv.IvyFileReader -import com.ivy.wallet.domain.logic.csv.model.ImportResult -import com.ivy.wallet.domain.logic.csv.model.ImportType -import com.ivy.wallet.domain.logic.zip.ExportZipLogic +import com.ivy.wallet.domain.deprecated.logic.csv.CSVImporter +import com.ivy.wallet.domain.deprecated.logic.csv.CSVMapper +import com.ivy.wallet.domain.deprecated.logic.csv.CSVNormalizer +import com.ivy.wallet.domain.deprecated.logic.csv.IvyFileReader +import com.ivy.wallet.domain.deprecated.logic.csv.model.ImportResult +import com.ivy.wallet.domain.deprecated.logic.csv.model.ImportType +import com.ivy.wallet.domain.deprecated.logic.zip.ExportZipLogic import com.ivy.wallet.ui.Import import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.onboarding.viewmodel.OnboardingViewModel diff --git a/app/src/main/java/com/ivy/wallet/ui/loan/LoanViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/loan/LoanViewModel.kt index 9ac38c9761..4bb3f115d7 100644 --- a/app/src/main/java/com/ivy/wallet/ui/loan/LoanViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/loan/LoanViewModel.kt @@ -2,15 +2,18 @@ package com.ivy.wallet.ui.loan import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.ivy.wallet.domain.action.account.AccountsAct +import com.ivy.wallet.domain.action.category.CategoriesAct +import com.ivy.wallet.domain.action.loan.LoansAct import com.ivy.wallet.domain.data.core.Account import com.ivy.wallet.domain.data.core.Loan +import com.ivy.wallet.domain.deprecated.logic.AccountCreator +import com.ivy.wallet.domain.deprecated.logic.LoanCreator +import com.ivy.wallet.domain.deprecated.logic.loantrasactions.LoanTransactionsLogic +import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData +import com.ivy.wallet.domain.deprecated.logic.model.CreateLoanData +import com.ivy.wallet.domain.deprecated.sync.item.LoanSync import com.ivy.wallet.domain.event.AccountsUpdatedEvent -import com.ivy.wallet.domain.logic.AccountCreator -import com.ivy.wallet.domain.logic.LoanCreator -import com.ivy.wallet.domain.logic.loantrasactions.LoanTransactionsLogic -import com.ivy.wallet.domain.logic.model.CreateAccountData -import com.ivy.wallet.domain.logic.model.CreateLoanData -import com.ivy.wallet.domain.sync.item.LoanSync import com.ivy.wallet.io.persistence.SharedPrefs import com.ivy.wallet.io.persistence.dao.AccountDao import com.ivy.wallet.io.persistence.dao.LoanDao @@ -42,7 +45,10 @@ class LoanViewModel @Inject constructor( private val sharedPrefs: SharedPrefs, private val accountDao: AccountDao, private val accountCreator: AccountCreator, - private val loanTransactionsLogic: LoanTransactionsLogic + private val loanTransactionsLogic: LoanTransactionsLogic, + private val loansAct: LoansAct, + private val accountsAct: AccountsAct, + private val categoriesAct: CategoriesAct ) : ViewModel() { private val _baseCurrencyCode = MutableStateFlow(getDefaultFIATCurrency().currencyCode) @@ -75,7 +81,7 @@ class LoanViewModel @Inject constructor( initialiseAccounts() _loans.value = ioThread { - loanDao.findAll() + loansAct(Unit) .map { loan -> val amountPaid = calculateAmountPaid(loan) val loanAmount = loan.amount @@ -111,7 +117,7 @@ class LoanViewModel @Inject constructor( } private suspend fun initialiseAccounts() { - val accounts = ioThread { accountDao.findAll() } + val accounts = accountsAct(Unit) _accounts.value = accounts _selectedAccount.value = defaultAccountId(accounts) _selectedAccount.value?.let { @@ -142,7 +148,7 @@ class LoanViewModel @Inject constructor( ioThread { newOrder.forEachIndexed { index, item -> loanDao.save( - item.loan.copy( + item.loan.toEntity().copy( orderNum = index.toDouble(), isSynced = false ) @@ -165,7 +171,7 @@ class LoanViewModel @Inject constructor( accountCreator.createAccount(data) { EventBus.getDefault().post(AccountsUpdatedEvent()) - _accounts.value = ioThread { accountDao.findAll() }!! + _accounts.value = accountsAct(Unit) _state.value = state.value.copy(accounts = _accounts.value) } diff --git a/app/src/main/java/com/ivy/wallet/ui/loandetails/LoanDetailsViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/loandetails/LoanDetailsViewModel.kt index f857af2349..b8f3b7eb0f 100644 --- a/app/src/main/java/com/ivy/wallet/ui/loandetails/LoanDetailsViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/loandetails/LoanDetailsViewModel.kt @@ -3,18 +3,20 @@ package com.ivy.wallet.ui.loandetails import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.ivy.design.navigation.Navigation +import com.ivy.wallet.domain.action.account.AccountsAct +import com.ivy.wallet.domain.action.loan.LoanByIdAct import com.ivy.wallet.domain.data.core.Account import com.ivy.wallet.domain.data.core.Loan import com.ivy.wallet.domain.data.core.LoanRecord import com.ivy.wallet.domain.data.core.Transaction +import com.ivy.wallet.domain.deprecated.logic.AccountCreator +import com.ivy.wallet.domain.deprecated.logic.LoanCreator +import com.ivy.wallet.domain.deprecated.logic.LoanRecordCreator +import com.ivy.wallet.domain.deprecated.logic.loantrasactions.LoanTransactionsLogic +import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData +import com.ivy.wallet.domain.deprecated.logic.model.CreateLoanRecordData +import com.ivy.wallet.domain.deprecated.logic.model.EditLoanRecordData import com.ivy.wallet.domain.event.AccountsUpdatedEvent -import com.ivy.wallet.domain.logic.AccountCreator -import com.ivy.wallet.domain.logic.LoanCreator -import com.ivy.wallet.domain.logic.LoanRecordCreator -import com.ivy.wallet.domain.logic.loantrasactions.LoanTransactionsLogic -import com.ivy.wallet.domain.logic.model.CreateAccountData -import com.ivy.wallet.domain.logic.model.CreateLoanRecordData -import com.ivy.wallet.domain.logic.model.EditLoanRecordData import com.ivy.wallet.io.persistence.dao.* import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.LoanDetails @@ -42,7 +44,9 @@ class LoanDetailsViewModel @Inject constructor( private val accountDao: AccountDao, private val accountCreator: AccountCreator, private val loanTransactionsLogic: LoanTransactionsLogic, - private val nav: Navigation + private val nav: Navigation, + private val accountsAct: AccountsAct, + private val loanByIdAct: LoanByIdAct ) : ViewModel() { private val _baseCurrency = MutableStateFlow("") @@ -87,13 +91,9 @@ class LoanDetailsViewModel @Inject constructor( _baseCurrency.value = it } - _accounts.value = ioThread { - accountDao.findAll() - } + _accounts.value = accountsAct(Unit) - _loan.value = ioThread { - loanDao.findById(id = loanId) - } + _loan.value = loanByIdAct(loanId) loan.value?.let { loan -> _selectedLoanAccount.value = accounts.value.find { @@ -120,7 +120,7 @@ class LoanDetailsViewModel @Inject constructor( ) DisplayLoanRecord( - it, + it.toDomain(), account = account, loanRecordTransaction = trans != null, loanRecordCurrencyCode = account?.currency ?: defaultCurrencyCode, @@ -148,7 +148,7 @@ class LoanDetailsViewModel @Inject constructor( } associatedTransaction = ioThread { - transactionDao.findLoanTransaction(loanId = loan.value!!.id) + transactionDao.findLoanTransaction(loanId = loan.value!!.id)?.toDomain() } associatedTransaction?.let { @@ -296,7 +296,7 @@ class LoanDetailsViewModel @Inject constructor( accountCreator.createAccount(data) { EventBus.getDefault().post(AccountsUpdatedEvent()) - _accounts.value = ioThread { accountDao.findAll() } + _accounts.value = accountsAct(Unit) } TestIdlingResource.decrement() diff --git a/app/src/main/java/com/ivy/wallet/ui/main/MainViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/main/MainViewModel.kt index 4068c1da28..e4e7906d7c 100644 --- a/app/src/main/java/com/ivy/wallet/ui/main/MainViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/main/MainViewModel.kt @@ -4,11 +4,11 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.ivy.design.navigation.Navigation +import com.ivy.wallet.domain.deprecated.logic.AccountCreator +import com.ivy.wallet.domain.deprecated.logic.currency.ExchangeRatesLogic +import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData +import com.ivy.wallet.domain.deprecated.sync.IvySync import com.ivy.wallet.domain.event.AccountsUpdatedEvent -import com.ivy.wallet.domain.logic.AccountCreator -import com.ivy.wallet.domain.logic.currency.ExchangeRatesLogic -import com.ivy.wallet.domain.logic.model.CreateAccountData -import com.ivy.wallet.domain.sync.IvySync import com.ivy.wallet.io.persistence.dao.SettingsDao import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.Main diff --git a/app/src/main/java/com/ivy/wallet/ui/onboarding/viewmodel/OnboardingViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/onboarding/viewmodel/OnboardingViewModel.kt index d7cfe4e211..4d8c66f9aa 100644 --- a/app/src/main/java/com/ivy/wallet/ui/onboarding/viewmodel/OnboardingViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/onboarding/viewmodel/OnboardingViewModel.kt @@ -5,16 +5,18 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.ivy.design.l0_system.Theme import com.ivy.design.navigation.Navigation +import com.ivy.wallet.domain.action.account.AccountsAct +import com.ivy.wallet.domain.action.category.CategoriesAct import com.ivy.wallet.domain.data.IvyCurrency import com.ivy.wallet.domain.data.core.Account import com.ivy.wallet.domain.data.core.Category import com.ivy.wallet.domain.data.core.Settings -import com.ivy.wallet.domain.logic.* -import com.ivy.wallet.domain.logic.currency.ExchangeRatesLogic -import com.ivy.wallet.domain.logic.model.CreateAccountData -import com.ivy.wallet.domain.logic.model.CreateCategoryData -import com.ivy.wallet.domain.logic.notification.TransactionReminderLogic -import com.ivy.wallet.domain.sync.IvySync +import com.ivy.wallet.domain.deprecated.logic.* +import com.ivy.wallet.domain.deprecated.logic.currency.ExchangeRatesLogic +import com.ivy.wallet.domain.deprecated.logic.model.CreateAccountData +import com.ivy.wallet.domain.deprecated.logic.model.CreateCategoryData +import com.ivy.wallet.domain.deprecated.logic.notification.TransactionReminderLogic +import com.ivy.wallet.domain.deprecated.sync.IvySync import com.ivy.wallet.io.network.FCMClient import com.ivy.wallet.io.network.IvyAnalytics import com.ivy.wallet.io.network.IvySession @@ -48,6 +50,9 @@ class OnboardingViewModel @Inject constructor( private val categoryDao: CategoryDao, private val accountCreator: AccountCreator, + private val accountsAct: AccountsAct, + private val categoriesAct: CategoriesAct, + //Only OnboardingRouter stuff sharedPrefs: SharedPrefs, ivySync: IvySync, @@ -133,8 +138,8 @@ class OnboardingViewModel @Inject constructor( theme = if (isSystemDarkMode) Theme.DARK else Theme.LIGHT, name = "", baseCurrency = defaultCurrency.code, - bufferAmount = 1000.0 - ) + bufferAmount = 1000.0.toBigDecimal() + ).toEntity() ) } @@ -283,15 +288,13 @@ class OnboardingViewModel @Inject constructor( } } - private suspend fun accountsWithBalance(): List = ioThread { - accountDao.findAll() - .map { - AccountBalance( - account = it, - balance = accountLogic.calculateAccountBalance(it) - ) - } - } + private suspend fun accountsWithBalance(): List = accountsAct(Unit) + .map { + AccountBalance( + account = it, + balance = accountLogic.calculateAccountBalance(it) + ) + } fun onAddAccountsDone() { viewModelScope.launch { @@ -320,7 +323,7 @@ class OnboardingViewModel @Inject constructor( TestIdlingResource.increment() categoryCreator.editCategory(updatedCategory) { - _categories.value = ioThread { categoryDao.findAll() }!! + _categories.value = categoriesAct(Unit)!! } TestIdlingResource.decrement() @@ -332,7 +335,7 @@ class OnboardingViewModel @Inject constructor( TestIdlingResource.increment() categoryCreator.createCategory(data) { - _categories.value = ioThread { categoryDao.findAll() }!! + _categories.value = categoriesAct(Unit)!! } TestIdlingResource.decrement() diff --git a/app/src/main/java/com/ivy/wallet/ui/reports/ReportViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/reports/ReportViewModel.kt index 3bc73a6bef..165d264fe8 100644 --- a/app/src/main/java/com/ivy/wallet/ui/reports/ReportViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/reports/ReportViewModel.kt @@ -6,15 +6,16 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.ivy.design.navigation.Navigation import com.ivy.fp.viewmodel.IvyViewModel +import com.ivy.wallet.domain.action.account.AccountsAct +import com.ivy.wallet.domain.action.category.CategoriesAct +import com.ivy.wallet.domain.action.exchange.ExchangeAct +import com.ivy.wallet.domain.action.transaction.TrnsWithDateDivsAct import com.ivy.wallet.domain.data.TransactionType import com.ivy.wallet.domain.data.core.Account import com.ivy.wallet.domain.data.core.Category import com.ivy.wallet.domain.data.core.Transaction -import com.ivy.wallet.domain.logic.PlannedPaymentsLogic -import com.ivy.wallet.domain.logic.WalletLogic -import com.ivy.wallet.domain.logic.csv.ExportCSVLogic -import com.ivy.wallet.domain.logic.currency.ExchangeRatesLogic -import com.ivy.wallet.domain.pure.wallet.withDateDividers +import com.ivy.wallet.domain.deprecated.logic.PlannedPaymentsLogic +import com.ivy.wallet.domain.deprecated.logic.csv.ExportCSVLogic import com.ivy.wallet.io.persistence.dao.* import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.RootActivity @@ -33,15 +34,17 @@ import javax.inject.Inject class ReportViewModel @Inject constructor( private val plannedPaymentsLogic: PlannedPaymentsLogic, private val settingsDao: SettingsDao, - private val walletLogic: WalletLogic, private val transactionDao: TransactionDao, private val ivyContext: IvyWalletCtx, private val nav: Navigation, private val accountDao: AccountDao, private val categoryDao: CategoryDao, - private val exchangeRatesLogic: ExchangeRatesLogic, private val exchangeRateDao: ExchangeRateDao, - private val exportCSVLogic: ExportCSVLogic + private val exportCSVLogic: ExportCSVLogic, + private val exchangeAct: ExchangeAct, + private val accountsAct: AccountsAct, + private val categoriesAct: CategoriesAct, + private val trnsWithDateDivsAct: TrnsWithDateDivsAct ) : IvyViewModel() { override val mutableState: MutableStateFlow = MutableStateFlow( ReportScreenState() @@ -66,8 +69,8 @@ class ReportViewModel @Inject constructor( fun start() { viewModelScope.launch(Dispatchers.IO) { _baseCurrency.value = settingsDao.findFirst().currency - _accounts.value = accountDao.findAll() - _categories.value = listOf(unSpecifiedCategory) + categoryDao.findAll() + _accounts.value = accountsAct(Unit) + _categories.value = listOf(unSpecifiedCategory) + categoriesAct(Unit) updateState { it.copy( @@ -107,10 +110,11 @@ class ReportViewModel @Inject constructor( .sortedByDescending { it.dateTime } val historyWithDateDividers = scope.async { - history.withDateDividers( - exchangeRateDao = exchangeRateDao, - accountDao = accountDao, - baseCurrencyCode = _baseCurrency.value + trnsWithDateDivsAct( + TrnsWithDateDivsAct.Input( + baseCurrency = stateVal().baseCurrency, + transactions = history + ) ) } @@ -187,6 +191,7 @@ class ReportViewModel @Inject constructor( return transactionDao .findAll() + .map { it.toDomain() } .asSequence() .filter { //Filter by Transaction Type