From 1857590834336ea70ceceaba8ea8927f5a117182 Mon Sep 17 00:00:00 2001 From: dankl Date: Sat, 11 Jan 2020 23:16:44 +0100 Subject: [PATCH] Extracted findUniqueBankForIbanAsync() --- .../ui/presenter/MainWindowPresenter.kt | 33 ++++++++++++++++--- .../android/ui/dialogs/TransferMoneyDialog.kt | 22 ++++++------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/MainWindowPresenter.kt b/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/MainWindowPresenter.kt index 2639a55b..1085631d 100644 --- a/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/MainWindowPresenter.kt +++ b/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/MainWindowPresenter.kt @@ -20,6 +20,7 @@ import net.dankito.fints.banks.BankFinder import net.dankito.fints.model.BankInfo import net.dankito.utils.IThreadPool import net.dankito.utils.ThreadPool +import net.dankito.utils.extensions.ofMaxLength import net.dankito.utils.web.client.OkHttpWebClient import java.math.BigDecimal import java.util.* @@ -158,17 +159,39 @@ open class MainWindowPresenter( open fun preloadBanksAsync() { - searchBanksByBankCodeAsync("1") { } + findUniqueBankForBankCodeAsync("1") { } } - open fun searchBanksByBankCodeAsync(enteredBankCode: String, callback: (List) -> Unit) { + open fun findUniqueBankForIbanAsync(iban: String, callback: (BankInfo?) -> Unit) { threadPool.runAsync { - callback(searchBanksByBankCode(enteredBankCode)) + callback(findUniqueBankForIban(iban)) } } - open fun searchBanksByBankCode(enteredBankCode: String): List { - return bankFinder.findBankByBankCode(enteredBankCode) + open fun findUniqueBankForIban(iban: String): BankInfo? { + // first two characters are country code, 3rd and 4th character are checksum, bank code has 8 digits in Germany and user + // should enter at least five characters before we start searching (before there shouldn't be a chance of a unique result) + if (iban.length >= 9) { + if (iban.startsWith("DE", true)) { + return findUniqueBankForBankCode(iban.substring(4).ofMaxLength(8)) + } + } + + return null + } + + open fun findUniqueBankForBankCodeAsync(bankCode: String, callback: (BankInfo?) -> Unit) { + threadPool.runAsync { + callback(findUniqueBankForBankCode(bankCode)) + } + } + + open fun findUniqueBankForBankCode(bankCode: String): BankInfo? { + val searchResult = bankFinder.findBankByBankCode(bankCode) + + val groupedByBic = searchResult.groupBy { it.bic } + + return if (groupedByBic.size == 1) searchResult.first() else null } open fun searchBanksByNameBankCodeOrCity(query: String?): List { diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt index 0b9144fa..2b31e20d 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt @@ -192,21 +192,21 @@ open class TransferMoneyDialog : DialogFragment() { } protected open fun tryToGetBicFromIban(enteredText: CharSequence) { - if (enteredText.length >= 12) { // first two characters are country code, 3rd and 4th character are checksum - if (enteredText.startsWith("DE", true)) { - presenter.searchBanksByBankCodeAsync(enteredText.substring(4)) { foundBanks -> - if (foundBanks.isNotEmpty()) { - context?.asActivity()?.runOnUiThread { - edtxtRemitteeBic.setText(foundBanks.first().bic) - - checkIfRequiredDataEnteredOnUiThread() - } - } - } + presenter.findUniqueBankForIbanAsync(enteredText.toString()) { foundBank -> + context?.asActivity()?.runOnUiThread { + showValuesForFoundBankOnUiThread(foundBank) } } } + private fun showValuesForFoundBankOnUiThread(foundBank: BankInfo?) { + edtxtRemitteeBank.setText(if (foundBank != null) (foundBank.name + " " + foundBank.city) else "") + + edtxtRemitteeBic.setText(foundBank?.bic ?: "") + + checkIfRequiredDataEnteredOnUiThread() + } + protected open fun checkIfRequiredDataEnteredOnUiThread() { val requiredDataEntered = edtxtRemitteeName.text.toString().isNotEmpty()