From ff456ccf321f9b72b7716e954d60cdbb1b862727 Mon Sep 17 00:00:00 2001 From: dankl Date: Sun, 3 Nov 2019 23:26:47 +0100 Subject: [PATCH] Storing transactions and balances now on value objects instead of in MainPresenter --- .../net/dankito/banking/ui/model/Account.kt | 2 +- .../dankito/banking/ui/model/BankAccount.kt | 24 +++++++++++-- .../android/ui/MainWindowPresenter.kt | 34 +++++-------------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/Account.kt b/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/Account.kt index cb362daf..8c0e5fb0 100644 --- a/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/Account.kt +++ b/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/Account.kt @@ -24,7 +24,7 @@ open class Account( get() = bankAccounts.map { it.balance }.fold(BigDecimal.ZERO) { acc, e -> acc + e } val transactions: List - get() = bankAccounts.flatMap { it.transactions } + get() = bankAccounts.flatMap { it.bookedTransactions } override fun toString(): String { diff --git a/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt b/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt index b3788357..1d21985d 100644 --- a/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt +++ b/BankingUiCommon/src/main/java/net/dankito/banking/ui/model/BankAccount.kt @@ -12,16 +12,36 @@ open class BankAccount @JvmOverloads constructor( var balance: BigDecimal = BigDecimal.ZERO, var currency: String = "EUR", var type: BankAccountType = BankAccountType.Giro, - accountTransactions: List = listOf() + bookedAccountTransactions: List = listOf() ) { internal constructor() : this(Account(), "", "", null, null) // for object deserializers - var transactions: List = accountTransactions + var bookedTransactions: List = bookedAccountTransactions protected set + var unbookedTransactions: List = listOf() + protected set + + + open fun addBookedTransactions(retrievedBookedTransactions: List) { + val uniqueTransactions = this.bookedTransactions.toMutableSet() + + uniqueTransactions.addAll(retrievedBookedTransactions) + + this.bookedTransactions = uniqueTransactions.toList() + } + + open fun addUnbookedTransactions(retrievedUnbookedTransactions: List) { + val uniqueUnbookedTransactions = this.unbookedTransactions.toMutableSet() + + uniqueUnbookedTransactions.addAll(retrievedUnbookedTransactions) + + this.unbookedTransactions = uniqueUnbookedTransactions.toList() + } + override fun toString(): String { return "$name ($identifier)" 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 ffe7fdcd..98d80158 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 @@ -34,12 +34,6 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { protected val accounts = mutableMapOf() - protected val bookedTransactions = mutableMapOf>() - - protected val unbookedTransactions = mutableMapOf>() - - protected val balances = mutableMapOf() - protected val accountAddedListeners = mutableListOf<(Account) -> Unit>() protected val retrievedAccountTransactionsResponseListeners = mutableListOf<(BankAccount, GetTransactionsResponse) -> Unit>() @@ -106,7 +100,7 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { accounts.keys.forEach { account -> account.bankAccounts.forEach { bankAccount -> 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 = bookedTransactions[bankAccount]?.firstOrNull { it.bookingDate <= today }?.bookingDate + val lastRetrievedTransactionDate = bankAccount.bookedTransactions.firstOrNull { it.bookingDate <= today }?.bookingDate val fromDate = lastRetrievedTransactionDate?.let { Date(it.time - 24 * 60 * 60 * 1000) } // on day before last received transaction getAccountTransactionsAsync(bankAccount, fromDate, callback) @@ -125,25 +119,15 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { protected open fun updateAccountTransactionsAndBalances(bankAccount: BankAccount, response: GetTransactionsResponse) { response.bookedTransactions.forEach { entry -> - if (bookedTransactions.containsKey(entry.key) == false) { - bookedTransactions.put(bankAccount, entry.value.toMutableSet()) - } - else { - bookedTransactions[bankAccount]?.addAll(entry.value) // TODO: does currently not work, overwrite equals() - } + entry.key.addBookedTransactions(entry.value) } response.unbookedTransactions.forEach { entry -> - if (unbookedTransactions.containsKey(entry.key) == false) { - unbookedTransactions.put(bankAccount, entry.value.toMutableSet()) - } - else { - unbookedTransactions[bankAccount]?.addAll(entry.value) - } + entry.key.addUnbookedTransactions(entry.value) } response.balances.forEach { entry -> - balances[entry.key] = entry.value + entry.key.balance = entry.value } } @@ -171,14 +155,14 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { val queryLowercase = query.trim().toLowerCase() if (queryLowercase.isEmpty()) { - return bookedTransactions.values.flatten().toList() + return allTransactions } - return bookedTransactions.values.flatten().filter { + return allTransactions.filter { it.otherPartyName?.toLowerCase()?.contains(queryLowercase) == true || it.usage.toLowerCase().contains(queryLowercase) || it.bookingText?.toLowerCase()?.contains(queryLowercase) == true - }.sortedByDescending { it.bookingDate } + } } @@ -196,10 +180,10 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { open val allTransactions: List - get() = bookedTransactions.values.flatten().toList() // TODO: someday add unbooked transactions + get() = accounts.keys.flatMap { it.transactions }.sortedByDescending { it.bookingDate } // TODO: someday add unbooked transactions open val balanceOfAllAccounts: BigDecimal - get() = balances.values.fold(BigDecimal.ZERO) { acc, e -> acc + e } + get() = accounts.keys.map { it.balance }.fold(BigDecimal.ZERO) { acc, e -> acc + e } open fun addAccountAddedListener(listener: (Account) -> Unit) {