Added initializedListeners to IBankingPersistence

This commit is contained in:
dankito 2021-11-05 23:33:05 +01:00
parent cf7a52f19e
commit 2cc5408857
7 changed files with 67 additions and 7 deletions

View File

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

View File

@ -93,4 +93,9 @@ open class BankingPersistenceJson(
}
}
override fun addInitializedListener(listener: () -> Unit) {
listener()
}
}

View File

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

View File

@ -28,4 +28,7 @@ interface IBankingPersistence {
fun saveBankIcon(bank: TypedBankData, iconUrl: String, fileExtension: String?)
fun addInitializedListener(listener: () -> Unit)
}

View File

@ -46,4 +46,9 @@ open class NoOpBankingPersistence : IBankingPersistence {
}
override fun addInitializedListener(listener: () -> Unit) {
listener()
}
}

View File

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

View File

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