Implemented filtering holdings

This commit is contained in:
dankito 2024-09-12 15:49:43 +02:00
parent 5029a2c3cb
commit 5253219565
2 changed files with 36 additions and 2 deletions

View File

@ -41,8 +41,7 @@ fun TransactionsList(uiState: UiState, uiSettings: UiSettings, isMobile: Boolean
val holdings by uiState.holdings.collectAsState() val holdings by uiState.holdings.collectAsState()
val holdingsToDisplay by remember(transactionsFilter, holdings) { val holdingsToDisplay by remember(transactionsFilter, holdings) {
// derivedStateOf { filterService.filterAccounts(transactions, transactionsFilter) } // TODO: filter which holdings to display derivedStateOf { filterService.filterHoldings(holdings, transactionsFilter) }
derivedStateOf { holdings }
} }
val showBalance by uiSettings.showBalance.collectAsState() val showBalance by uiSettings.showBalance.collectAsState()

View File

@ -1,6 +1,7 @@
package net.codinux.banking.ui.service package net.codinux.banking.ui.service
import net.codinux.banking.dataaccess.entities.BankAccountEntity import net.codinux.banking.dataaccess.entities.BankAccountEntity
import net.codinux.banking.dataaccess.entities.HoldingEntity
import net.codinux.banking.dataaccess.entities.UserEntity import net.codinux.banking.dataaccess.entities.UserEntity
import net.codinux.banking.ui.model.AccountTransactionViewModel import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.AccountTransactionsFilter import net.codinux.banking.ui.model.AccountTransactionsFilter
@ -43,6 +44,40 @@ class AccountTransactionsFilterService {
|| (transaction.otherPartyName != null && transaction.otherPartyName.contains(searchTerm, true)) || (transaction.otherPartyName != null && transaction.otherPartyName.contains(searchTerm, true))
fun filterHoldings(holdings: List<HoldingEntity>, filter: AccountTransactionsFilter): List<HoldingEntity> {
if (filter.year != null) { // TODO: check if it's current year (and month)
return emptyList()
}
var appliedHoldingFilter = if (filter.showAllAccounts) {
holdings
} else {
holdings.filter { matchesFilter(it, filter.selectedAccounts.value) }
}
val searchTerm = filter.searchTerm
return if (searchTerm.isBlank()) {
appliedHoldingFilter
} else {
appliedHoldingFilter.filter { matchesSearchTerm(it, searchTerm) }
}
}
private fun matchesFilter(holding: HoldingEntity, filter: List<BankAccountFilter>): Boolean =
filter.any { (user, bankAccount) ->
if (bankAccount != null) {
holding.bankAccountId == bankAccount.id
} else {
holding.userId == user.id
}
}
private fun matchesSearchTerm(holding: HoldingEntity, searchTerm: String): Boolean =
holding.name.contains(searchTerm, true)
|| holding.isin?.contains(searchTerm, true) == true
|| holding.wkn?.contains(searchTerm, true) == true
fun isSelected(user: UserEntity, transactionsFilter: AccountTransactionsFilter): Boolean { fun isSelected(user: UserEntity, transactionsFilter: AccountTransactionsFilter): Boolean {
if (transactionsFilter.showAllAccounts) { if (transactionsFilter.showAllAccounts) {
return false return false