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,26 +183,32 @@ 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 ->
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()
if (iconFilePath.startsWith("file://", true) == false) {
iconFilePath = "file://" + iconFilePath // without 'file://' Android will not find it
}
customer.iconUrl = iconFilePath
persistAccount(customer)
callAccountsChangedListeners()
}
} catch (e: Exception) {
log.error(e) { "Could not get icon for bank ${customer.bankName}" }
}
}
protected open fun saveBankIconToDisk(customer: Customer, bankIconUrl: String): File {
val bankIconsDir = File(dataFolder, "bank_icons")

View File

@ -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?
}

View File

@ -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
}

View File

@ -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 ->