From 5253219565539d713595e81ba02f6c00596f77bc Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 12 Sep 2024 15:49:43 +0200 Subject: [PATCH] Implemented filtering holdings --- .../transactions/TransactionsList.kt | 3 +- .../AccountTransactionsFilterService.kt | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/transactions/TransactionsList.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/transactions/TransactionsList.kt index 8dec6b0..c94cb91 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/transactions/TransactionsList.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/transactions/TransactionsList.kt @@ -41,8 +41,7 @@ fun TransactionsList(uiState: UiState, uiSettings: UiSettings, isMobile: Boolean val holdings by uiState.holdings.collectAsState() val holdingsToDisplay by remember(transactionsFilter, holdings) { -// derivedStateOf { filterService.filterAccounts(transactions, transactionsFilter) } // TODO: filter which holdings to display - derivedStateOf { holdings } + derivedStateOf { filterService.filterHoldings(holdings, transactionsFilter) } } val showBalance by uiSettings.showBalance.collectAsState() diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/AccountTransactionsFilterService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/AccountTransactionsFilterService.kt index ac02b80..a377438 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/AccountTransactionsFilterService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/AccountTransactionsFilterService.kt @@ -1,6 +1,7 @@ package net.codinux.banking.ui.service 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.ui.model.AccountTransactionViewModel import net.codinux.banking.ui.model.AccountTransactionsFilter @@ -43,6 +44,40 @@ class AccountTransactionsFilterService { || (transaction.otherPartyName != null && transaction.otherPartyName.contains(searchTerm, true)) + fun filterHoldings(holdings: List, filter: AccountTransactionsFilter): List { + 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): 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 { if (transactionsFilter.showAllAccounts) { return false