From e8a27b1a83e517f9375f3c0a62cd43a19f360915 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 30 Jul 2020 14:42:09 +0200 Subject: [PATCH] Added findIconForBankAsync(() to IBankIconFinder so that iOS can choose how to handle asynchronous retrieval of bank icon --- .../banking/ui/presenter/BankingPresenter.kt | 32 +++++++++++-------- .../dankito/banking/util/IBankIconFinder.kt | 2 ++ .../banking/util/NoOpBankIconFinder.kt | 4 +++ .../dankito/banking/util/BankIconFinder.kt | 7 ++++ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt index c940375d..e86a5e44 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -183,25 +183,31 @@ open class BankingPresenter( } protected open fun findIconForBankAsync(customer: Customer) { - asyncRunner.runAsync { - findIconForBank(customer) + bankIconFinder.findIconForBankAsync(customer.bankName) { bankIconUrl -> + bankIconUrl?.let { + try { + handleFindIconForBankResult(customer, bankIconUrl) + } catch (e: Exception) { + log.error(e) { "Could not get icon for bank ${customer.bankName}" } + } + } } } - protected open fun findIconForBank(customer: Customer) { - try { - bankIconFinder.findIconForBank(customer.bankName)?.let { bankIconUrl -> - val bankIconFile = saveBankIconToDisk(customer, bankIconUrl) + protected open fun handleFindIconForBankResult(customer: Customer, bankIconUrl: String) { + val bankIconFile = saveBankIconToDisk(customer, bankIconUrl) - customer.iconUrl = "file://" + bankIconFile.getAbsolutePath() // without 'file://' Android will not find it + var iconFilePath = bankIconFile.getAbsolutePath() - persistAccount(customer) - - callAccountsChangedListeners() - } - } catch (e: Exception) { - log.error(e) { "Could not get icon for bank ${customer.bankName}" } + if (iconFilePath.startsWith("file://", true) == false) { + iconFilePath = "file://" + iconFilePath // without 'file://' Android will not find it } + + customer.iconUrl = iconFilePath + + persistAccount(customer) + + callAccountsChangedListeners() } protected open fun saveBankIconToDisk(customer: Customer, bankIconUrl: String): File { diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/IBankIconFinder.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/IBankIconFinder.kt index fa26b7b1..14e41d1b 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/IBankIconFinder.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/IBankIconFinder.kt @@ -3,6 +3,8 @@ package net.dankito.banking.util interface IBankIconFinder { + fun findIconForBankAsync(bankName: String, prefSize: Int = 72, result: (String?) -> Unit) + fun findIconForBank(bankName: String, prefSize: Int = 72): String? } \ No newline at end of file diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/NoOpBankIconFinder.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/NoOpBankIconFinder.kt index 21c2a5f1..8dc0bd42 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/NoOpBankIconFinder.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/util/NoOpBankIconFinder.kt @@ -3,6 +3,10 @@ package net.dankito.banking.util open class NoOpBankIconFinder : IBankIconFinder { + override fun findIconForBankAsync(bankName: String, prefSize: Int, result: (String?) -> Unit) { + result(findIconForBank(bankName, prefSize)) + } + override fun findIconForBank(bankName: String, prefSize: Int): String? { return null } diff --git a/ui/BankingUiCommon/src/jvmMain/kotlin/net/dankito/banking/util/BankIconFinder.kt b/ui/BankingUiCommon/src/jvmMain/kotlin/net/dankito/banking/util/BankIconFinder.kt index 82e530fc..4e59f529 100644 --- a/ui/BankingUiCommon/src/jvmMain/kotlin/net/dankito/banking/util/BankIconFinder.kt +++ b/ui/BankingUiCommon/src/jvmMain/kotlin/net/dankito/banking/util/BankIconFinder.kt @@ -8,6 +8,7 @@ import org.jsoup.nodes.Document import org.slf4j.LoggerFactory import java.net.URI import java.util.regex.Pattern +import kotlin.concurrent.thread open class BankIconFinder : IBankIconFinder { @@ -36,6 +37,12 @@ open class BankIconFinder : IBankIconFinder { protected val faviconComparator = FaviconComparator(webClient) + override fun findIconForBankAsync(bankName: String, prefSize: Int, result: (String?) -> Unit) { + thread { + result(findIconForBank(bankName, prefSize)) + } + } + override fun findIconForBank(bankName: String, prefSize: Int): String? { findBankWebsite(bankName)?.let { bankUrl -> webClient.get(bankUrl).body?.let { bankHomepageResponse ->