From 2cc54088574c182ab1b3f22cdae43c2319b41390 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 5 Nov 2021 23:33:05 +0100 Subject: [PATCH] Added initializedListeners to IBankingPersistence --- .../persistence/RoomBankingPersistence.kt | 36 ++++++++++++++++++- .../persistence/BankingPersistenceJson.kt | 5 +++ .../authentication/AuthenticationService.kt | 8 ++++- .../persistence/IBankingPersistence.kt | 3 ++ .../persistence/NoOpBankingPersistence.kt | 5 +++ .../banking/ui/presenter/BankingPresenter.kt | 12 ++++--- .../CoreDataBankingPersistence.swift | 5 +++ 7 files changed, 67 insertions(+), 7 deletions(-) diff --git a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt index 250d7f05..e061c6e1 100644 --- a/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt +++ b/persistence/database/RoomBankingPersistence/src/main/java/net/dankito/banking/persistence/RoomBankingPersistence.kt @@ -18,6 +18,7 @@ import net.dankito.utils.multiplatform.asString import net.sqlcipher.database.SQLiteDatabase import net.sqlcipher.database.SupportFactory import org.slf4j.LoggerFactory +import java.util.concurrent.CopyOnWriteArraySet open class RoomBankingPersistence(protected open val applicationContext: Context) : IBankingPersistence, ITransactionPartySearcher { @@ -37,9 +38,19 @@ open class RoomBankingPersistence(protected open val applicationContext: Context protected lateinit var database: BankingDatabase + protected open var isInitialized = false + + protected open val initializedListeners = CopyOnWriteArraySet<() -> Unit>() + override fun decryptData(password: CharArray): Boolean { - return openDatabase(password) + val result = openDatabase(password) + + if (result) { + callInitializedListeners() + } + + return result } override fun changePassword(newPassword: CharArray): Boolean { @@ -249,4 +260,27 @@ open class RoomBankingPersistence(protected open val applicationContext: Context .map { TransactionParty(it.name, it.accountId, it.bankCode) } } + + override fun addInitializedListener(listener: () -> Unit) { + if (isInitialized) { + listener() + } else { + initializedListeners.add(listener) + } + } + + protected open fun callInitializedListeners() { + isInitialized = true + val copy = ArrayList(initializedListeners) + initializedListeners.clear() + + copy.forEach { listener -> { + try { + listener() + } catch (e: Exception) { + log.error("Could not call listener $listener", e) + } + } } + } + } \ No newline at end of file diff --git a/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt b/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt index e67b7ff0..eea7e825 100644 --- a/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt +++ b/persistence/json/BankingPersistenceJson/src/main/kotlin/net/dankito/banking/persistence/BankingPersistenceJson.kt @@ -93,4 +93,9 @@ open class BankingPersistenceJson( } } + + override fun addInitializedListener(listener: () -> Unit) { + listener() + } + } \ No newline at end of file diff --git a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt index 3d258fb4..e31882d5 100644 --- a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt +++ b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/ui/android/authentication/AuthenticationService.kt @@ -50,6 +50,8 @@ open class AuthenticationService( init { + log.info("Initializing AuthenticationService (at the end database must get opened) ...") + val settings = loadAuthenticationSettings() if (settings == null) { // first app run -> create a default password @@ -151,7 +153,11 @@ open class AuthenticationService( } protected open fun openDatabase(password: CharArray): Boolean { - return persistence.decryptData(password) + val result = persistence.decryptData(password) + + log.info("Did decrypting data / opening database succeed? $result") + + return result } diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt index a7ed4632..4fa8e779 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/IBankingPersistence.kt @@ -28,4 +28,7 @@ interface IBankingPersistence { fun saveBankIcon(bank: TypedBankData, iconUrl: String, fileExtension: String?) + + fun addInitializedListener(listener: () -> Unit) + } \ No newline at end of file diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt index d9b10e32..068e386b 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/persistence/NoOpBankingPersistence.kt @@ -46,4 +46,9 @@ open class NoOpBankingPersistence : IBankingPersistence { } + + override fun addInitializedListener(listener: () -> Unit) { + listener() + } + } \ No newline at end of file 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 22c923d8..2f3c6355 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 @@ -154,12 +154,14 @@ open class BankingPresenter( init { - asyncRunner.runAsync { - readAppSettings() - readPersistedBanks() + persister.addInitializedListener { + asyncRunner.runAsync { + readAppSettings() + readPersistedBanks() - if (appSettings.automaticallyUpdateAccountsAfterMinutes != null) { // TODO: check if time has elapsed - doAutomaticAccountsUpdate() + if (appSettings.automaticallyUpdateAccountsAfterMinutes != null) { // TODO: check if time has elapsed + //doAutomaticAccountsUpdate() + } } } diff --git a/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift b/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift index 48e6ab5e..d27381e4 100644 --- a/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift +++ b/ui/BankingiOSApp/BankingiOSApp/persistence/CoreDataBankingPersistence.swift @@ -5,6 +5,7 @@ import BankingUiSwift import EncryptedCoreData +// TODO: implement addInitializedListener() class CoreDataBankingPersistence: IBankingPersistence, ITransactionPartySearcher { private let mapper = Mapper() @@ -288,5 +289,9 @@ class CoreDataBankingPersistence: IBankingPersistence, ITransactionPartySearcher NSLog("Could not delete all banks: \(error)") } } + + func addInitializedListener(listener: () -> Unit) { + listener() + } }