Implemented selecting Account / BankAccount / All accounts and that only selected BankAccounts' transaction and balance gets displayed

This commit is contained in:
dankl 2020-01-12 15:46:17 +01:00 committed by dankito
parent 3a8033750f
commit a720430e71
5 changed files with 111 additions and 21 deletions

View File

@ -7,8 +7,8 @@ import javafx.scene.control.ContextMenu
import javafx.scene.input.ContextMenuEvent
import javafx.scene.input.MouseButton
import javafx.scene.input.MouseEvent
import net.dankito.banking.ui.model.Account
import net.dankito.banking.ui.model.AccountTransaction
import net.dankito.banking.ui.model.BankAccount
import net.dankito.banking.ui.model.parameters.TransferMoneyData
import net.dankito.banking.ui.model.responses.GetTransactionsResponse
import net.dankito.banking.ui.presenter.MainWindowPresenter
@ -30,15 +30,13 @@ open class AccountTransactionsView(private val presenter: MainWindowPresenter) :
init {
presenter.addAccountsChangedListener { handleAccountsChanged(it) }
presenter.addSelectedBankAccountsChangedListener { handleSelectedBankAccountsChanged(it) }
presenter.addRetrievedAccountTransactionsResponseListener { _, response ->
handleGetTransactionsResponseOffUiThread(response)
}
transactionsFilter.addListener { _, _, newValue ->
transactionsToDisplay.setAll(presenter.searchAccountTransactions(newValue))
}
transactionsFilter.addListener { _, _, newValue -> updateTransactionsToDisplay(newValue) }
}
@ -110,8 +108,23 @@ open class AccountTransactionsView(private val presenter: MainWindowPresenter) :
}
protected open fun handleAccountsChanged(accounts: List<Account>) {
isAccountSelected.value = accounts.isNotEmpty() // TODO: not correct, check if an account has been selected
protected open fun handleSelectedBankAccountsChanged(selectedBankAccounts: List<BankAccount>) {
runLater {
isAccountSelected.value = selectedBankAccounts.isNotEmpty()
updateTransactionsToDisplay()
}
}
protected open fun updateTransactionsToDisplay() {
updateTransactionsToDisplay(transactionsFilter.value)
}
protected open fun updateTransactionsToDisplay(filter: String) {
transactionsToDisplay.setAll(presenter.searchSelectedAccountTransactions(filter))
// TODO: if transactions are filtered calculate and show balance of displayed transactions?
balance.value = presenter.balanceOfSelectedBankAccounts.toString()
}
protected open fun handleGetTransactionsResponseOffUiThread(response: GetTransactionsResponse) {
@ -120,10 +133,7 @@ open class AccountTransactionsView(private val presenter: MainWindowPresenter) :
protected open fun handleGetTransactionsResponseOnUiThread(response: GetTransactionsResponse) {
if (response.isSuccessful) {
transactionsToDisplay.setAll(presenter.allTransactions) // TODO: get that one for currently displayed account and apply current filter on it
// TODO: if transactions are filtered calculate and show balance of displayed transactions?
balance.value = presenter.balanceOfAllAccounts.toString() // TODO: get that one for currently displayed account and add currency
updateTransactionsToDisplay()
}
}

View File

@ -3,7 +3,11 @@ package net.dankito.banking.ui.javafx.controls
import javafx.collections.FXCollections
import javafx.geometry.Pos
import javafx.scene.control.TreeItem
import javafx.scene.control.TreeView
import javafx.scene.layout.Priority
import net.dankito.banking.ui.javafx.model.AccountsAccountTreeItem
import net.dankito.banking.ui.javafx.model.AccountsBankAccountTreeItem
import net.dankito.banking.ui.javafx.model.AccountsRootTreeItem
import net.dankito.banking.ui.presenter.MainWindowPresenter
import net.dankito.utils.javafx.ui.controls.addButton
import net.dankito.utils.javafx.ui.extensions.fixedHeight
@ -48,7 +52,7 @@ open class AccountsView(protected val presenter: MainWindowPresenter) : View() {
}
add(AccountsTreeView(accounts).apply {
selectionModel.selectedItemProperty().addListener { _, _, newValue -> selectedAccountChanged(newValue) }
selectionModel.selectedItemProperty().addListener { _, _, newValue -> selectedBankAccountChanged(newValue) }
vboxConstraints {
vGrow = Priority.ALWAYS
@ -62,4 +66,14 @@ open class AccountsView(protected val presenter: MainWindowPresenter) : View() {
presenter.showAddAccountDialog()
}
protected open fun selectedBankAccountChanged(accountTreeItem: TreeItem<String>?) {
accountTreeItem?.let {
when (accountTreeItem) {
is AccountsBankAccountTreeItem -> presenter.selectedBankAccount(accountTreeItem.bankAccount)
is AccountsAccountTreeItem -> presenter.selectedAccount(accountTreeItem.account)
else -> presenter.selectedAllBankAccounts()
}
}
}
}

View File

@ -3,7 +3,7 @@ package net.dankito.banking.ui.javafx.model
import net.dankito.banking.ui.model.Account
open class AccountsAccountTreeItem(account: Account) : AccountsTreeItemBase(account.displayName) {
open class AccountsAccountTreeItem(val account: Account) : AccountsTreeItemBase(account.displayName) {
init {
isExpanded = true

View File

@ -3,4 +3,4 @@ package net.dankito.banking.ui.javafx.model
import net.dankito.banking.ui.model.BankAccount
open class AccountsBankAccountTreeItem(bankAccount: BankAccount) : AccountsTreeItemBase(bankAccount.displayName)
open class AccountsBankAccountTreeItem(val bankAccount: BankAccount) : AccountsTreeItemBase(bankAccount.displayName)

View File

@ -45,10 +45,15 @@ open class MainWindowPresenter(
protected val clientsForAccounts = mutableMapOf<Account, IBankingClient>()
protected var selectedBankAccountsField = mutableListOf<BankAccount>()
protected val accountsChangedListeners = mutableListOf<(List<Account>) -> Unit>()
protected val retrievedAccountTransactionsResponseListeners = mutableListOf<(BankAccount, GetTransactionsResponse) -> Unit>()
protected val selectedBankAccountsChangedListeners = mutableListOf<(List<BankAccount>) -> Unit>()
protected val callback: BankingClientCallback = object : BankingClientCallback {
@ -81,6 +86,8 @@ open class MainWindowPresenter(
retrievedAccountTransactions(bankAccount, response)
}
}
selectedAccount(account)
}
callback(response)
@ -203,14 +210,14 @@ open class MainWindowPresenter(
}
open fun searchAccountTransactions(query: String): List<AccountTransaction> {
open fun searchSelectedAccountTransactions(query: String): List<AccountTransaction> {
val queryLowercase = query.trim().toLowerCase()
if (queryLowercase.isEmpty()) {
return allTransactions
return selectedBankAccountsAccountTransactions
}
return allTransactions.filter {
return selectedBankAccountsAccountTransactions.filter {
it.otherPartyName?.toLowerCase()?.contains(queryLowercase) == true
|| it.usage.toLowerCase().contains(queryLowercase)
|| it.bookingText?.toLowerCase()?.contains(queryLowercase) == true
@ -241,6 +248,34 @@ open class MainWindowPresenter(
}
open val selectedBankAccounts: List<BankAccount>
get() = ArrayList(selectedBankAccountsField)
open val selectedBankAccountsAccountTransactions: List<AccountTransaction>
get() = getAccountTransactionsForAccounts(selectedBankAccounts.map { it.account }.toSet())
open val balanceOfSelectedBankAccounts: BigDecimal
get() = getBalanceForAccounts(selectedBankAccounts.map { it.account }.toSet())
open fun selectedAllBankAccounts() {
setSelectedBankAccounts(bankAccounts)
}
open fun selectedAccount(account: Account) {
setSelectedBankAccounts(account.bankAccounts)
}
open fun selectedBankAccount(bankAccount: BankAccount) {
setSelectedBankAccounts(listOf(bankAccount))
}
protected open fun setSelectedBankAccounts(bankAccounts: List<BankAccount>) {
this.selectedBankAccountsField = ArrayList(bankAccounts) // make a copy
callSelectedBankAccountsChangedListeners(selectedBankAccountsField)
}
open val accounts: List<Account>
get() = clientsForAccounts.keys.toList()
@ -248,10 +283,19 @@ open class MainWindowPresenter(
get() = accounts.flatMap { it.bankAccounts }
open val allTransactions: List<AccountTransaction>
get() = clientsForAccounts.keys.flatMap { it.transactions }.sortedByDescending { it.bookingDate } // TODO: someday add unbooked transactions
get() = getAccountTransactionsForAccounts(accounts)
open val balanceOfAllAccounts: BigDecimal
get() = clientsForAccounts.keys.map { it.balance }.fold(BigDecimal.ZERO) { acc, e -> acc + e }
get() = getBalanceForAccounts(accounts)
protected open fun getAccountTransactionsForAccounts(accounts: Collection<Account>): List<AccountTransaction> {
return accounts.flatMap { it.transactions }.sortedByDescending { it.bookingDate } // TODO: someday add unbooked transactions
}
protected open fun getBalanceForAccounts(accounts: Collection<Account>): BigDecimal {
return accounts.map { it.balance }.fold(BigDecimal.ZERO) { acc, e -> acc + e }
}
open fun addAccountsChangedListener(listener: (List<Account>) -> Unit): Boolean {
@ -270,8 +314,13 @@ open class MainWindowPresenter(
}
}
open fun addRetrievedAccountTransactionsResponseListener(listener: (BankAccount, GetTransactionsResponse) -> Unit) {
retrievedAccountTransactionsResponseListeners.add(listener)
open fun addRetrievedAccountTransactionsResponseListener(listener: (BankAccount, GetTransactionsResponse) -> Unit): Boolean {
return retrievedAccountTransactionsResponseListeners.add(listener)
}
open fun removeRetrievedAccountTransactionsResponseListener(listener: (BankAccount, GetTransactionsResponse) -> Unit): Boolean {
return retrievedAccountTransactionsResponseListeners.add(listener)
}
protected open fun callRetrievedAccountTransactionsResponseListener(bankAccount: BankAccount, response: GetTransactionsResponse) {
@ -280,4 +329,21 @@ open class MainWindowPresenter(
}
}
open fun addSelectedBankAccountsChangedListener(listener: (List<BankAccount>) -> Unit): Boolean {
return selectedBankAccountsChangedListeners.add(listener)
}
open fun removeSelectedBankAccountsChangedListener(listener: (List<BankAccount>) -> Unit): Boolean {
return selectedBankAccountsChangedListeners.add(listener)
}
protected open fun callSelectedBankAccountsChangedListeners(selectedBankAccounts: List<BankAccount>) {
val selectedBankAccounts = this.selectedBankAccounts
ArrayList(selectedBankAccountsChangedListeners).forEach {
it(selectedBankAccounts) // TODO: use RxJava for this
}
}
}