From a257223543c3bc3ea9079235f83c89be507c2109 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 9 Sep 2024 20:33:34 +0200 Subject: [PATCH] Implemented searching banks by bic --- .../banking/ui/dialogs/AddAccountDialog.kt | 2 +- .../codinux/banking/ui/service/BankFinder.kt | 23 ++++++++++--------- .../banking/ui/service/BankingService.kt | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt index 2cf486b..bb6a574 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/AddAccountDialog.kt @@ -97,7 +97,7 @@ fun AddAccountDialog( Column { AutocompleteTextField( - label = "Bank (Suche mit Name, Bankleitzahl oder Ort)", + label = "Bank (Suche mit Name, Ort, BIC oder Bankleitzahl)", value = enteredBankSearchQuery, onEnteredTextChanged = { enteredBankSearchQuery = it }, onSelectedItemChanged = { diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankFinder.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankFinder.kt index 8ab147a..46903c7 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankFinder.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankFinder.kt @@ -17,7 +17,7 @@ class BankFinder { return bankList.take(maxItems ?: Int.MAX_VALUE) } - suspend fun findBankByNameBankCodeOrCity(query: String?, maxItems: Int? = null): List { + suspend fun findBankByNameBicBankCodeOrCity(query: String?, maxItems: Int? = null): List { if (query.isNullOrBlank()) { return getBankList(maxItems) } @@ -26,7 +26,8 @@ class BankFinder { return findBankByBankCode(query, maxItems) } - return findBankByNameBankCodeOrCityForNonEmptyQuery(query, maxItems) + // we already checked for BankCode above, so there's no need to do this again in method below + return findBankByNameBicOrCityForNonEmptyQuery(query, maxItems) } @@ -77,18 +78,18 @@ class BankFinder { } } - suspend fun findBankByNameBankCodeOrCityForNonEmptyQuery(query: String, maxItems: Int?): List { + private suspend fun findBankByNameBicOrCityForNonEmptyQuery(query: String, maxItems: Int?): List { val queryPartsLowerCase = query.lowercase().split(" ", "-") return getBankList().asSequence().filter { bankInfo -> - checkIfAllQueryPartsMatchBankNameBankCodeOrCity(queryPartsLowerCase, bankInfo) + checkIfAllQueryPartsMatchBankNameBicOrCity(queryPartsLowerCase, bankInfo) } .max(maxItems) } - private fun checkIfAllQueryPartsMatchBankNameBankCodeOrCity(queryPartsLowerCase: List, bankInfo: BankInfo): Boolean { + private fun checkIfAllQueryPartsMatchBankNameBicOrCity(queryPartsLowerCase: List, bankInfo: BankInfo): Boolean { for (queryPartLowerCase in queryPartsLowerCase) { - if (checkIfQueryMatchesBankNameBankCodeOrCity(bankInfo, queryPartLowerCase) == false) { + if (checkIfQueryMatchesBankNameBicOrCity(bankInfo, queryPartLowerCase) == false) { return false } } @@ -96,11 +97,11 @@ class BankFinder { return true } - private fun checkIfQueryMatchesBankNameBankCodeOrCity(bankInfo: BankInfo, queryLowerCase: String): Boolean { - return bankInfo.name.lowercase().contains(queryLowerCase) - || bankInfo.bankCode.startsWith(queryLowerCase) - || bankInfo.city.lowercase().startsWith(queryLowerCase) - || bankInfo.branchesInOtherCities.any { it.lowercase().startsWith(queryLowerCase) } + private fun checkIfQueryMatchesBankNameBicOrCity(bankInfo: BankInfo, queryLowerCase: String): Boolean { + return bankInfo.name.contains(queryLowerCase, true) + || bankInfo.bic.startsWith(queryLowerCase, true) + || bankInfo.city.startsWith(queryLowerCase, true) + || bankInfo.branchesInOtherCities.any { it.startsWith(queryLowerCase, true) } } fun Sequence.max(maxItems: Int? = null): List = diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt index f31b2c2..65060be 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt @@ -69,7 +69,7 @@ class BankingService( suspend fun findBanks(query: String): List = - bankFinder.findBankByNameBankCodeOrCity(query, 25) + bankFinder.findBankByNameBicBankCodeOrCity(query, 25) suspend fun addAccount(bank: BankInfo, loginName: String, password: String, retrieveAllTransactions: Boolean = false): Boolean { try {