From 28537668ff2dbf034ad929d86c5d2659bb969927 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 16 Sep 2020 15:12:17 +0200 Subject: [PATCH] Implemented passing only new transactions to persister.saveOrUpdateAccountTransactions(). Had to call saveOrUpdateAccountTransactions() therefore also in receivedAccountsTransactionChunk listener --- .../banking/ui/model/IAccountTransaction.kt | 3 ++ .../banking/ui/presenter/BankingPresenter.kt | 28 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/IAccountTransaction.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/IAccountTransaction.kt index daf92551..eed754c2 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/IAccountTransaction.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/model/IAccountTransaction.kt @@ -54,6 +54,9 @@ interface IAccountTransaction { var technicalId: String + open val transactionIdentifier: String + get() = buildTransactionIdentifier() + val showOtherPartyName: Boolean get() = otherPartyName.isNullOrBlank() == false /* && type != "ENTGELTABSCHLUSS" && type != "AUSZAHLUNG" */ // TODO diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt index 8546210f..4db84d99 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -360,25 +360,35 @@ open class BankingPresenter( protected open fun receivedAccountsTransactionChunk(bankAccount: TypedBankAccount, accountTransactionsChunk: List) { if (accountTransactionsChunk.isNotEmpty()) { - bankAccount.addBookedTransactions(accountTransactionsChunk) + asyncRunner.runAsync { // don't block retrieving next chunk by blocked saving to db / json + updateAccountTransactions(bankAccount, accountTransactionsChunk) - callRetrievedAccountTransactionsResponseListener(GetTransactionsResponse(bankAccount, true, null, accountTransactionsChunk)) + callRetrievedAccountTransactionsResponseListener(GetTransactionsResponse(bankAccount, true, null, accountTransactionsChunk)) + } } } protected open fun updateAccountTransactionsAndBalances(response: GetTransactionsResponse) { - val bankAccount = response.bankAccount - bankAccount.addBookedTransactions(response.bookedTransactions) - - bankAccount.addUnbookedTransactions(response.unbookedTransactions) + updateAccountTransactions(bankAccount, response.bookedTransactions, response.unbookedTransactions) response.balance?.let { updateBalance(bankAccount, it) } + } - persistAccountTransactionsOffUiThread(bankAccount, response.bookedTransactions, response.unbookedTransactions) + protected open fun updateAccountTransactions(bankAccount: TypedBankAccount, bookedTransactions: List, unbookedTransactions: List? = null) { + val knownAccountTransactions = bankAccount.bookedTransactions.map { it.transactionIdentifier } + + val newBookedTransactions = bookedTransactions.filterNot { knownAccountTransactions.contains(it.transactionIdentifier) } + bankAccount.addBookedTransactions(newBookedTransactions) + + unbookedTransactions?.let { + bankAccount.addUnbookedTransactions(unbookedTransactions) + } + + persistAccountTransactionsOffUiThread(bankAccount, newBookedTransactions) } protected open fun updateBalance(bankAccount: TypedBankAccount, balance: BigDecimal) { @@ -435,10 +445,8 @@ open class BankingPresenter( /** * Ensure that this method only gets called off UI thread (at least for Android Room db) as otherwise it may blocks UI thread. */ - protected open fun persistAccountTransactionsOffUiThread(bankAccount: TypedBankAccount, bookedTransactions: List, unbookedTransactions: List) { + protected open fun persistAccountTransactionsOffUiThread(bankAccount: TypedBankAccount, bookedTransactions: List) { persister.saveOrUpdateAccountTransactions(bankAccount, bookedTransactions) - - // TODO: someday also persist unbooked transactions }