Implemented retrievedAccountTransactionsResponseListeners so that listeners get informed if account transactions (or balances) changes and can update its view
This commit is contained in:
parent
8892368782
commit
b45ae38c50
|
@ -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<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) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue