Added initializedListeners to IBankingPersistence
This commit is contained in:
parent
cf7a52f19e
commit
2cc5408857
|
@ -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)
|
||||
}
|
||||
} }
|
||||
}
|
||||
|
||||
}
|
|
@ -93,4 +93,9 @@ open class BankingPersistenceJson(
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
override fun addInitializedListener(listener: () -> Unit) {
|
||||
listener()
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -28,4 +28,7 @@ interface IBankingPersistence {
|
|||
|
||||
fun saveBankIcon(bank: TypedBankData, iconUrl: String, fileExtension: String?)
|
||||
|
||||
|
||||
fun addInitializedListener(listener: () -> Unit)
|
||||
|
||||
}
|
|
@ -46,4 +46,9 @@ open class NoOpBankingPersistence : IBankingPersistence {
|
|||
|
||||
}
|
||||
|
||||
|
||||
override fun addInitializedListener(listener: () -> Unit) {
|
||||
listener()
|
||||
}
|
||||
|
||||
}
|
|
@ -154,12 +154,14 @@ open class BankingPresenter(
|
|||
|
||||
|
||||
init {
|
||||
persister.addInitializedListener {
|
||||
asyncRunner.runAsync {
|
||||
readAppSettings()
|
||||
readPersistedBanks()
|
||||
|
||||
if (appSettings.automaticallyUpdateAccountsAfterMinutes != null) { // TODO: check if time has elapsed
|
||||
doAutomaticAccountsUpdate()
|
||||
//doAutomaticAccountsUpdate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import BankingUiSwift
|
|||
import EncryptedCoreData
|
||||
|
||||
|
||||
// TODO: implement addInitializedListener()
|
||||
class CoreDataBankingPersistence: IBankingPersistence, ITransactionPartySearcher {
|
||||
|
||||
private let mapper = Mapper()
|
||||
|
@ -289,4 +290,8 @@ class CoreDataBankingPersistence: IBankingPersistence, ITransactionPartySearcher
|
|||
}
|
||||
}
|
||||
|
||||
func addInitializedListener(listener: () -> Unit) {
|
||||
listener()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue