Implemented retrievedAccountTransactionsResponseListeners so that listeners get informed if account transactions (or balances) changes and can update its view

This commit is contained in:
dankl 2019-10-31 00:57:34 +01:00 committed by dankito
parent 8892368782
commit b45ae38c50
2 changed files with 55 additions and 29 deletions

View File

@ -37,6 +37,8 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
protected val accountAddedListeners = mutableListOf<(BankData, CustomerData) -> Unit>() protected val accountAddedListeners = mutableListOf<(BankData, CustomerData) -> Unit>()
protected val retrievedAccountTransactionsResponseListeners = mutableListOf<(CustomerData, GetTransactionsResponse) -> Unit>()
open fun addAccountAsync(bankInfo: BankInfo, customerId: String, pin: String, open fun addAccountAsync(bankInfo: BankInfo, customerId: String, pin: String,
callback: (AddAccountResponse) -> Unit) { callback: (AddAccountResponse) -> Unit) {
@ -72,7 +74,7 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
finTsClient.getTransactionsAsync(GetTransactionsParameter(true, fromDate), bank, customer) { response -> finTsClient.getTransactionsAsync(GetTransactionsParameter(true, fromDate), bank, customer) { response ->
retrievedAccountTransactions(customer, response) retrievedAccountTransactions(customer, response)
callback(response) // TODO: does not return all booked transactions, only the newly retrieved ones! callback(response)
} }
} }
@ -89,6 +91,14 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
protected open fun retrievedAccountTransactions(customer: CustomerData, response: GetTransactionsResponse) { protected open fun retrievedAccountTransactions(customer: CustomerData, response: GetTransactionsResponse) {
if (response.isSuccessful) { if (response.isSuccessful) {
updateAccountTransactionsAndBalances(customer, response)
}
callRetrievedAccountTransactionsResponseListener(customer, response)
}
protected open fun updateAccountTransactionsAndBalances(customer: CustomerData, response: GetTransactionsResponse) {
if (bookedTransactions.containsKey(customer) == false) { if (bookedTransactions.containsKey(customer) == false) {
bookedTransactions.put(customer, response.bookedTransactions.toMutableSet()) bookedTransactions.put(customer, response.bookedTransactions.toMutableSet())
} }
@ -107,7 +117,6 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
balances[customer] = it balances[customer] = it
} }
} }
}
open fun transferMoneyAsync(bankTransferData: BankTransferData, callback: (FinTsClientResponse) -> Unit) { open fun transferMoneyAsync(bankTransferData: BankTransferData, callback: (FinTsClientResponse) -> Unit) {
@ -143,6 +152,13 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
} }
open val allTransactions: List<AccountTransaction>
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) { open fun addAccountAddedListener(listener: (BankData, CustomerData) -> Unit) {
accountAddedListeners.add(listener) 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
}
}
} }

View File

@ -22,8 +22,6 @@ import net.dankito.fints.model.BankTransferData
import net.dankito.fints.response.client.GetTransactionsResponse import net.dankito.fints.response.client.GetTransactionsResponse
import net.dankito.utils.android.extensions.asActivity import net.dankito.utils.android.extensions.asActivity
import java.math.BigDecimal import java.math.BigDecimal
import java.util.*
import kotlin.concurrent.schedule
class HomeFragment : Fragment() { class HomeFragment : Fragment() {
@ -135,30 +133,32 @@ class HomeFragment : Fragment() {
// TODO: this is such a bad code style // TODO: this is such a bad code style
(context as? MainActivity)?.presenter?.let { presenter -> (context as? MainActivity)?.presenter?.let { presenter ->
this.presenter = presenter this.presenter = presenter
presenter.addRetrievedAccountTransactionsResponseListener { _, response ->
handleGetTransactionsResponse(response)
}
} }
} }
private fun updateAccountsTransactions() { private fun updateAccountsTransactions() {
presenter.updateAccountsTransactionsAsync { response -> presenter.updateAccountsTransactionsAsync { }
handleGetTransactionsResponse(response)
}
} }
private fun handleGetTransactionsResponse(response: GetTransactionsResponse) { private fun handleGetTransactionsResponse(response: GetTransactionsResponse) {
context?.asActivity()?.let { activity -> context?.asActivity()?.let { activity ->
activity.runOnUiThread { activity.runOnUiThread {
if (response.isSuccessful) { if (response.isSuccessful) {
transactionAdapter.items = response.bookedTransactions val allTransactions = presenter.allTransactions
transactionAdapter.items = allTransactions
mnitmSearchTransactions.isVisible = response.bookedTransactions.isNotEmpty() mnitmSearchTransactions.isVisible = allTransactions.isNotEmpty()
mnitmUpdateTransactions.isVisible = response.bookedTransactions.isNotEmpty() mnitmUpdateTransactions.isVisible = allTransactions.isNotEmpty()
response.balance?.let { mnitmBalance.title = presenter.balanceOfAllAccounts.toString()
mnitmBalance.title = it.toString() mnitmBalance.isVisible = true
mnitmBalance.setVisible(true)
}
} else { } 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"))) .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() } .setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() }
.show() .show()