diff --git a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt new file mode 100644 index 00000000..17fbd50b --- /dev/null +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Extensions.kt @@ -0,0 +1,28 @@ +package net.dankito.utils.multiplatform + +import platform.Foundation.NSArray +import platform.Foundation.NSDictionary + + +fun NSArray.toList(): List { + val result = mutableListOf() + + for (i in 0L until this.count.toLong()) { + result.add(this.objectAtIndex(i.toULong()) as T) + } + + return result +} + + +fun NSDictionary.getString(key: String): String? { + return this.objectForKey(key) as? String +} + +fun NSDictionary.getString(key: String, defaultValue: String): String { + return this.getString(key) ?: defaultValue +} + +fun NSDictionary.getStringOrEmpty(key: String): String { + return this.getString(key, "") +} \ No newline at end of file diff --git a/tools/BankFinder/build.gradle b/tools/BankFinder/build.gradle index a41178b9..25b95659 100644 --- a/tools/BankFinder/build.gradle +++ b/tools/BankFinder/build.gradle @@ -24,7 +24,7 @@ kotlin { fromPreset(iOSTarget, 'ios') { binaries { framework { - baseName = "fints4k" + baseName = "BankFinder" } } } @@ -35,6 +35,8 @@ kotlin { commonMain { dependencies { implementation kotlin("stdlib-common") + + implementation project(":common") } } diff --git a/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankFinderBase.kt b/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankFinderBase.kt index fdc9241a..f6dc7a89 100644 --- a/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankFinderBase.kt +++ b/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankFinderBase.kt @@ -1,18 +1,8 @@ package net.dankito.banking.bankfinder -import net.dankito.utils.serialization.JacksonJsonSerializer -import org.slf4j.LoggerFactory - abstract class BankFinderBase : IBankFinder { - companion object { - const val BankListFileName = "BankList.json" - - private val log = LoggerFactory.getLogger(InMemoryBankFinder::class.java) - } - - protected abstract fun findBankByNameOrCityForNonEmptyQuery(query: String): List abstract fun searchBankByBic(bic: String): BankInfo? @@ -47,24 +37,8 @@ abstract class BankFinderBase : IBankFinder { } - protected open fun loadBankListFile(): List { - try { - val bankListString = readBankListFile() - - JacksonJsonSerializer().deserializeList(bankListString, BankInfo::class.java)?.let { - return it - } - } catch (e: Exception) { - log.error("Could not load bank list", e) - } - - return listOf() - } - - protected open fun readBankListFile(): String { - val inputStream = BankFinderBase::class.java.classLoader.getResourceAsStream(BankListFileName) - - return inputStream.bufferedReader().readText() + protected open fun loadBankList(): List { + return BankListDeserializer().loadBankList() } } \ No newline at end of file diff --git a/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt b/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt new file mode 100644 index 00000000..9c773bd3 --- /dev/null +++ b/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt @@ -0,0 +1,8 @@ +package net.dankito.banking.bankfinder + + +expect class BankListDeserializer() { + + fun loadBankList(): List + +} \ No newline at end of file diff --git a/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/InMemoryBankFinder.kt b/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/InMemoryBankFinder.kt index bc913096..abb64f66 100644 --- a/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/InMemoryBankFinder.kt +++ b/tools/BankFinder/src/commonMain/kotlin/net/dankito/banking/bankfinder/InMemoryBankFinder.kt @@ -58,7 +58,7 @@ open class InMemoryBankFinder() : BankFinderBase(), IBankFinder { return it } - val bankList = loadBankListFile() + val bankList = loadBankList() this.bankListField = bankList diff --git a/tools/BankFinder/src/iosMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt b/tools/BankFinder/src/iosMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt new file mode 100644 index 00000000..8a1f1daf --- /dev/null +++ b/tools/BankFinder/src/iosMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt @@ -0,0 +1,44 @@ +package net.dankito.banking.bankfinder + +import net.dankito.utils.multiplatform.getString +import net.dankito.utils.multiplatform.getStringOrEmpty +import net.dankito.utils.multiplatform.toList +import platform.Foundation.* + + +actual class BankListDeserializer { + + actual fun loadBankList(): List { + val bundle = NSBundle.mainBundle + + bundle.pathForResource("BankList", "json")?.let { bankListJsonPath -> + NSData.dataWithContentsOfFile(bankListJsonPath, NSDataReadingMappedIfSafe, null)?.let { bankListData -> + NSJSONSerialization.JSONObjectWithData(bankListData, NSJSONReadingMutableContainers, null)?.let { bankListJson -> + (bankListJson as? NSArray)?.let { + return bankListJson.toList().map { + mapToBankInfo(it) + } + } + } + } + } + + + return listOf() + } + + private fun mapToBankInfo(bankInfoDict: NSDictionary): BankInfo { + return BankInfo( + bankInfoDict.getStringOrEmpty("name"), + bankInfoDict.getStringOrEmpty("bankCode"), + bankInfoDict.getStringOrEmpty("bic"), + bankInfoDict.getStringOrEmpty("postalCode"), + bankInfoDict.getStringOrEmpty("city"), + bankInfoDict.getStringOrEmpty("checksumMethod"), + bankInfoDict.getString("pinTanAddress"), + bankInfoDict.getString("pinTanVersion"), + bankInfoDict.getString("oldBankCode") + ) + } + +} \ No newline at end of file diff --git a/tools/BankFinder/src/jvmMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt b/tools/BankFinder/src/jvmMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt new file mode 100644 index 00000000..25d095a1 --- /dev/null +++ b/tools/BankFinder/src/jvmMain/kotlin/net/dankito/banking/bankfinder/BankListDeserializer.kt @@ -0,0 +1,36 @@ +package net.dankito.banking.bankfinder + +import net.dankito.utils.multiplatform.log.LoggerFactory +import net.dankito.utils.serialization.JacksonJsonSerializer + + +actual class BankListDeserializer { + + companion object { + const val BankListFileName = "BankList.json" + + private val log = LoggerFactory.getLogger(InMemoryBankFinder::class) + } + + + actual fun loadBankList(): List { + try { + val bankListString = readBankListFile() + + JacksonJsonSerializer().deserializeList(bankListString, BankInfo::class.java)?.let { + return it + } + } catch (e: Exception) { + log.error(e) { "Could not load bank list" } + } + + return listOf() + } + + fun readBankListFile(): String { + val inputStream = BankFinderBase::class.java.classLoader.getResourceAsStream(BankListFileName) + + return inputStream.bufferedReader().readText() + } + +} \ No newline at end of file