From b45ae38c50b9b2b86766224dd91d5731e653f90d Mon Sep 17 00:00:00 2001 From: dankl Date: Thu, 31 Oct 2019 00:57:34 +0100 Subject: [PATCH] Implemented retrievedAccountTransactionsResponseListeners so that listeners get informed if account transactions (or balances) changes and can update its view --- .../android/ui/MainWindowPresenter.kt | 58 ++++++++++++++----- .../android/ui/home/HomeFragment.kt | 26 ++++----- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt index cb5e6db3..c45bf294 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt @@ -37,6 +37,8 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { protected val accountAddedListeners = mutableListOf<(BankData, CustomerData) -> Unit>() + protected val retrievedAccountTransactionsResponseListeners = mutableListOf<(CustomerData, GetTransactionsResponse) -> Unit>() + open fun addAccountAsync(bankInfo: BankInfo, customerId: String, pin: String, callback: (AddAccountResponse) -> Unit) { @@ -72,7 +74,7 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { finTsClient.getTransactionsAsync(GetTransactionsParameter(true, fromDate), bank, customer) { response -> retrievedAccountTransactions(customer, response) - callback(response) // TODO: does not return all booked transactions, only the newly retrieved ones! + callback(response) } } @@ -89,23 +91,30 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { protected open fun retrievedAccountTransactions(customer: CustomerData, response: GetTransactionsResponse) { if (response.isSuccessful) { - if (bookedTransactions.containsKey(customer) == false) { - bookedTransactions.put(customer, response.bookedTransactions.toMutableSet()) - } - else { - bookedTransactions[customer]?.addAll(response.bookedTransactions) // TODO: does currently not work, overwrite equals() - } + updateAccountTransactionsAndBalances(customer, response) + } - if (unbookedTransactions.containsKey(customer) == false) { - unbookedTransactions.put(customer, response.unbookedTransactions.toMutableSet()) - } - else { - unbookedTransactions[customer]?.addAll(response.unbookedTransactions) - } + callRetrievedAccountTransactionsResponseListener(customer, response) + } - response.balance?.let { - balances[customer] = it - } + protected open fun updateAccountTransactionsAndBalances(customer: CustomerData, response: GetTransactionsResponse) { + + if (bookedTransactions.containsKey(customer) == false) { + bookedTransactions.put(customer, response.bookedTransactions.toMutableSet()) + } + else { + bookedTransactions[customer]?.addAll(response.bookedTransactions) // TODO: does currently not work, overwrite equals() + } + + if (unbookedTransactions.containsKey(customer) == false) { + unbookedTransactions.put(customer, response.unbookedTransactions.toMutableSet()) + } + else { + unbookedTransactions[customer]?.addAll(response.unbookedTransactions) + } + + response.balance?.let { + balances[customer] = it } } @@ -143,6 +152,13 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { } + open val allTransactions: List + get() = bookedTransactions.values.flatten().toList() // TODO: someday add unbooked transactions + + open val balanceOfAllAccounts: BigDecimal + get() = balances.values.fold(BigDecimal.ZERO) { acc, e -> acc + e } + + open fun addAccountAddedListener(listener: (BankData, CustomerData) -> Unit) { accountAddedListeners.add(listener) } @@ -153,4 +169,14 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { } } + open fun addRetrievedAccountTransactionsResponseListener(listener: (CustomerData, GetTransactionsResponse) -> Unit) { + retrievedAccountTransactionsResponseListeners.add(listener) + } + + protected open fun callRetrievedAccountTransactionsResponseListener(customer: CustomerData, response: GetTransactionsResponse) { + ArrayList(retrievedAccountTransactionsResponseListeners).forEach { + it(customer, response) // TODO: use RxJava for this + } + } + } \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt index 508a1e4e..08a97f1d 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt @@ -22,8 +22,6 @@ import net.dankito.fints.model.BankTransferData import net.dankito.fints.response.client.GetTransactionsResponse import net.dankito.utils.android.extensions.asActivity import java.math.BigDecimal -import java.util.* -import kotlin.concurrent.schedule class HomeFragment : Fragment() { @@ -135,30 +133,32 @@ class HomeFragment : Fragment() { // TODO: this is such a bad code style (context as? MainActivity)?.presenter?.let { presenter -> this.presenter = presenter + + presenter.addRetrievedAccountTransactionsResponseListener { _, response -> + handleGetTransactionsResponse(response) + } } } + private fun updateAccountsTransactions() { - presenter.updateAccountsTransactionsAsync { response -> - handleGetTransactionsResponse(response) - } + presenter.updateAccountsTransactionsAsync { } } private fun handleGetTransactionsResponse(response: GetTransactionsResponse) { context?.asActivity()?.let { activity -> activity.runOnUiThread { if (response.isSuccessful) { - transactionAdapter.items = response.bookedTransactions + val allTransactions = presenter.allTransactions + transactionAdapter.items = allTransactions - mnitmSearchTransactions.isVisible = response.bookedTransactions.isNotEmpty() - mnitmUpdateTransactions.isVisible = response.bookedTransactions.isNotEmpty() + mnitmSearchTransactions.isVisible = allTransactions.isNotEmpty() + mnitmUpdateTransactions.isVisible = allTransactions.isNotEmpty() - response.balance?.let { - mnitmBalance.title = it.toString() - mnitmBalance.setVisible(true) - } + mnitmBalance.title = presenter.balanceOfAllAccounts.toString() + mnitmBalance.isVisible = true } else { - AlertDialog.Builder(activity) + AlertDialog.Builder(activity) // TODO: may show account name in message .setMessage(activity.getString(R.string.fragment_home_could_not_retrieve_account_transactions, response.exception ?: response.errorsToShowToUser.joinToString("\n"))) .setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() } .show()