Storing transactions and balances now on value objects instead of in MainPresenter
This commit is contained in:
parent
f4194f832e
commit
ff456ccf32
|
@ -24,7 +24,7 @@ open class Account(
|
||||||
get() = bankAccounts.map { it.balance }.fold(BigDecimal.ZERO) { acc, e -> acc + e }
|
get() = bankAccounts.map { it.balance }.fold(BigDecimal.ZERO) { acc, e -> acc + e }
|
||||||
|
|
||||||
val transactions: List<AccountTransaction>
|
val transactions: List<AccountTransaction>
|
||||||
get() = bankAccounts.flatMap { it.transactions }
|
get() = bankAccounts.flatMap { it.bookedTransactions }
|
||||||
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
|
|
|
@ -12,16 +12,36 @@ open class BankAccount @JvmOverloads constructor(
|
||||||
var balance: BigDecimal = BigDecimal.ZERO,
|
var balance: BigDecimal = BigDecimal.ZERO,
|
||||||
var currency: String = "EUR",
|
var currency: String = "EUR",
|
||||||
var type: BankAccountType = BankAccountType.Giro,
|
var type: BankAccountType = BankAccountType.Giro,
|
||||||
accountTransactions: List<AccountTransaction> = listOf()
|
bookedAccountTransactions: List<AccountTransaction> = listOf()
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|
||||||
internal constructor() : this(Account(), "", "", null, null) // for object deserializers
|
internal constructor() : this(Account(), "", "", null, null) // for object deserializers
|
||||||
|
|
||||||
|
|
||||||
var transactions: List<AccountTransaction> = accountTransactions
|
var bookedTransactions: List<AccountTransaction> = bookedAccountTransactions
|
||||||
protected set
|
protected set
|
||||||
|
|
||||||
|
var unbookedTransactions: List<Any> = listOf()
|
||||||
|
protected set
|
||||||
|
|
||||||
|
|
||||||
|
open fun addBookedTransactions(retrievedBookedTransactions: List<AccountTransaction>) {
|
||||||
|
val uniqueTransactions = this.bookedTransactions.toMutableSet()
|
||||||
|
|
||||||
|
uniqueTransactions.addAll(retrievedBookedTransactions)
|
||||||
|
|
||||||
|
this.bookedTransactions = uniqueTransactions.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun addUnbookedTransactions(retrievedUnbookedTransactions: List<Any>) {
|
||||||
|
val uniqueUnbookedTransactions = this.unbookedTransactions.toMutableSet()
|
||||||
|
|
||||||
|
uniqueUnbookedTransactions.addAll(retrievedUnbookedTransactions)
|
||||||
|
|
||||||
|
this.unbookedTransactions = uniqueUnbookedTransactions.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "$name ($identifier)"
|
return "$name ($identifier)"
|
||||||
|
|
|
@ -34,12 +34,6 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
|
||||||
|
|
||||||
protected val accounts = mutableMapOf<Account, Any>()
|
protected val accounts = mutableMapOf<Account, Any>()
|
||||||
|
|
||||||
protected val bookedTransactions = mutableMapOf<BankAccount, MutableSet<AccountTransaction>>()
|
|
||||||
|
|
||||||
protected val unbookedTransactions = mutableMapOf<BankAccount, MutableSet<Any>>()
|
|
||||||
|
|
||||||
protected val balances = mutableMapOf<BankAccount, BigDecimal>()
|
|
||||||
|
|
||||||
protected val accountAddedListeners = mutableListOf<(Account) -> Unit>()
|
protected val accountAddedListeners = mutableListOf<(Account) -> Unit>()
|
||||||
|
|
||||||
protected val retrievedAccountTransactionsResponseListeners = mutableListOf<(BankAccount, GetTransactionsResponse) -> Unit>()
|
protected val retrievedAccountTransactionsResponseListeners = mutableListOf<(BankAccount, GetTransactionsResponse) -> Unit>()
|
||||||
|
@ -106,7 +100,7 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
|
||||||
accounts.keys.forEach { account ->
|
accounts.keys.forEach { account ->
|
||||||
account.bankAccounts.forEach { bankAccount ->
|
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 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
|
val fromDate = lastRetrievedTransactionDate?.let { Date(it.time - 24 * 60 * 60 * 1000) } // on day before last received transaction
|
||||||
|
|
||||||
getAccountTransactionsAsync(bankAccount, fromDate, callback)
|
getAccountTransactionsAsync(bankAccount, fromDate, callback)
|
||||||
|
@ -125,25 +119,15 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
|
||||||
protected open fun updateAccountTransactionsAndBalances(bankAccount: BankAccount, response: GetTransactionsResponse) {
|
protected open fun updateAccountTransactionsAndBalances(bankAccount: BankAccount, response: GetTransactionsResponse) {
|
||||||
|
|
||||||
response.bookedTransactions.forEach { entry ->
|
response.bookedTransactions.forEach { entry ->
|
||||||
if (bookedTransactions.containsKey(entry.key) == false) {
|
entry.key.addBookedTransactions(entry.value)
|
||||||
bookedTransactions.put(bankAccount, entry.value.toMutableSet())
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bookedTransactions[bankAccount]?.addAll(entry.value) // TODO: does currently not work, overwrite equals()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response.unbookedTransactions.forEach { entry ->
|
response.unbookedTransactions.forEach { entry ->
|
||||||
if (unbookedTransactions.containsKey(entry.key) == false) {
|
entry.key.addUnbookedTransactions(entry.value)
|
||||||
unbookedTransactions.put(bankAccount, entry.value.toMutableSet())
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
unbookedTransactions[bankAccount]?.addAll(entry.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response.balances.forEach { entry ->
|
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()
|
val queryLowercase = query.trim().toLowerCase()
|
||||||
|
|
||||||
if (queryLowercase.isEmpty()) {
|
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.otherPartyName?.toLowerCase()?.contains(queryLowercase) == true
|
||||||
|| it.usage.toLowerCase().contains(queryLowercase)
|
|| it.usage.toLowerCase().contains(queryLowercase)
|
||||||
|| it.bookingText?.toLowerCase()?.contains(queryLowercase) == true
|
|| it.bookingText?.toLowerCase()?.contains(queryLowercase) == true
|
||||||
}.sortedByDescending { it.bookingDate }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -196,10 +180,10 @@ open class MainWindowPresenter(callback: FinTsClientCallback) {
|
||||||
|
|
||||||
|
|
||||||
open val allTransactions: List<AccountTransaction>
|
open val allTransactions: List<AccountTransaction>
|
||||||
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
|
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) {
|
open fun addAccountAddedListener(listener: (Account) -> Unit) {
|
||||||
|
|
Loading…
Reference in New Issue