From 96454a742d5013a9ac8821ced02db2e1d7c67885 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 20 May 2020 20:40:29 +0200 Subject: [PATCH] Implemented storing when account transactions have been retrieved for last time and retrieving next transactions from this date on --- .../net/dankito/banking/ui/model/BankAccount.kt | 1 + .../banking/ui/presenter/BankingPresenter.kt | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt b/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt index fdb19065..28b2d362 100644 --- a/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt +++ b/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt @@ -16,6 +16,7 @@ open class BankAccount @JvmOverloads constructor( var balance: BigDecimal = BigDecimal.ZERO, var currency: String = "EUR", var type: BankAccountType = BankAccountType.Girokonto, + var lastRetrievedTransactionsTimestamp: Date? = null, var supportsRetrievingAccountTransactions: Boolean = false, var supportsRetrievingBalance: Boolean = false, var supportsTransferringMoney: Boolean = false, diff --git a/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt index d44a76fa..942f3fe4 100644 --- a/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -26,7 +26,6 @@ import java.io.File import java.io.FileOutputStream import java.math.BigDecimal import java.net.URL -import java.net.URLEncoder import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList @@ -145,6 +144,8 @@ open class BankingPresenter( val newClient = bankingClientCreator.createClient(bankInfo, customerId, pin, databaseFolder, threadPool, this.callback) + val startDate = Date() + newClient.addAccountAsync { response -> val account = response.account @@ -159,7 +160,7 @@ open class BankingPresenter( if (response.supportsRetrievingTransactionsOfLast90DaysWithoutTan) { response.bookedTransactions.keys.forEach { bankAccount -> - retrievedAccountTransactions(bankAccount, response) + retrievedAccountTransactions(bankAccount, startDate, response) } } @@ -265,9 +266,11 @@ open class BankingPresenter( callback: (GetTransactionsResponse) -> Unit) { getClientForAccount(bankAccount.account)?.let { client -> + val startDate = Date() + client.getTransactionsAsync(bankAccount, net.dankito.banking.ui.model.parameters.GetTransactionsParameter(true, fromDate)) { response -> - retrievedAccountTransactions(bankAccount, response) + retrievedAccountTransactions(bankAccount, startDate, response) callback(response) } @@ -278,9 +281,7 @@ open class BankingPresenter( clientsForAccounts.keys.forEach { account -> account.bankAccounts.forEach { bankAccount -> if (bankAccount.supportsRetrievingAccountTransactions) { - val today = Date() // TODO: still don't know where this bug is coming from that bank returns a transaction dated at end of year - val lastRetrievedTransactionDate = bankAccount.bookedTransactions.firstOrNull { it.bookingDate <= today }?.bookingDate - val fromDate = lastRetrievedTransactionDate?.let { Date(it.time - OneDayMillis) } // one day before last received transaction + val fromDate = bankAccount.lastRetrievedTransactionsTimestamp?.let { Date(it.time - OneDayMillis) } // one day before last received transactions fetchAccountTransactionsAsync(bankAccount, fromDate, callback) } @@ -288,8 +289,10 @@ open class BankingPresenter( } } - protected open fun retrievedAccountTransactions(bankAccount: BankAccount, response: GetTransactionsResponse) { + protected open fun retrievedAccountTransactions(bankAccount: BankAccount, startDate: Date, response: GetTransactionsResponse) { if (response.isSuccessful) { + bankAccount.lastRetrievedTransactionsTimestamp = startDate + updateAccountTransactionsAndBalances(bankAccount, response) }