Added findIconForBankAsync(() to IBankIconFinder so that iOS can choose how to handle asynchronous retrieval of bank icon

This commit is contained in:
dankito 2020-07-30 14:42:09 +02:00
parent c476483f87
commit e8a27b1a83
4 changed files with 32 additions and 13 deletions

View File

@ -183,25 +183,31 @@ open class BankingPresenter(
} }
protected open fun findIconForBankAsync(customer: Customer) { protected open fun findIconForBankAsync(customer: Customer) {
asyncRunner.runAsync { bankIconFinder.findIconForBankAsync(customer.bankName) { bankIconUrl ->
findIconForBank(customer) 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) { protected open fun handleFindIconForBankResult(customer: Customer, bankIconUrl: String) {
try { val bankIconFile = saveBankIconToDisk(customer, bankIconUrl)
bankIconFinder.findIconForBank(customer.bankName)?.let { bankIconUrl ->
val bankIconFile = saveBankIconToDisk(customer, bankIconUrl)
customer.iconUrl = "file://" + bankIconFile.getAbsolutePath() // without 'file://' Android will not find it var iconFilePath = bankIconFile.getAbsolutePath()
persistAccount(customer) if (iconFilePath.startsWith("file://", true) == false) {
iconFilePath = "file://" + iconFilePath // without 'file://' Android will not find it
callAccountsChangedListeners()
}
} catch (e: Exception) {
log.error(e) { "Could not get icon for bank ${customer.bankName}" }
} }
customer.iconUrl = iconFilePath
persistAccount(customer)
callAccountsChangedListeners()
} }
protected open fun saveBankIconToDisk(customer: Customer, bankIconUrl: String): File { protected open fun saveBankIconToDisk(customer: Customer, bankIconUrl: String): File {

View File

@ -3,6 +3,8 @@ package net.dankito.banking.util
interface IBankIconFinder { interface IBankIconFinder {
fun findIconForBankAsync(bankName: String, prefSize: Int = 72, result: (String?) -> Unit)
fun findIconForBank(bankName: String, prefSize: Int = 72): String? fun findIconForBank(bankName: String, prefSize: Int = 72): String?
} }

View File

@ -3,6 +3,10 @@ package net.dankito.banking.util
open class NoOpBankIconFinder : IBankIconFinder { 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? { override fun findIconForBank(bankName: String, prefSize: Int): String? {
return null return null
} }

View File

@ -8,6 +8,7 @@ import org.jsoup.nodes.Document
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.net.URI import java.net.URI
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.concurrent.thread
open class BankIconFinder : IBankIconFinder { open class BankIconFinder : IBankIconFinder {
@ -36,6 +37,12 @@ open class BankIconFinder : IBankIconFinder {
protected val faviconComparator = FaviconComparator(webClient) 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? { override fun findIconForBank(bankName: String, prefSize: Int): String? {
findBankWebsite(bankName)?.let { bankUrl -> findBankWebsite(bankName)?.let { bankUrl ->
webClient.get(bankUrl).body?.let { bankHomepageResponse -> webClient.get(bankUrl).body?.let { bankHomepageResponse ->