From 29dbe70983f02070269f02a2ece3574218ccb15a Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 15 Sep 2020 20:53:08 +0200 Subject: [PATCH] Ensured that persisting methods get called off UI thread --- .../banking/ui/presenter/BankingPresenter.kt | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) 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 979603f5..b336e46a 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 @@ -79,7 +79,7 @@ open class BankingPresenter constructor( override fun enterTan(customer: TypedCustomer, tanChallenge: TanChallenge, callback: (EnterTanResult) -> Unit) { if (saveAccountOnNextEnterTanInvocation) { - persistAccount(customer) + persistAccountOffUiThread(customer) saveAccountOnNextEnterTanInvocation = false } @@ -158,7 +158,7 @@ open class BankingPresenter constructor( callAccountsChangedListeners() - persistAccount(account) + persistAccountOffUiThread(account) if (response.supportsRetrievingTransactionsOfLast90DaysWithoutTan) { response.bookedTransactionsOfLast90Days.keys.forEach { bankAccount -> @@ -206,7 +206,7 @@ open class BankingPresenter constructor( customer.iconUrl = iconFilePath - persistAccount(customer) + persistAccountOffUiThread(customer) callAccountsChangedListeners() } @@ -243,6 +243,12 @@ open class BankingPresenter constructor( open fun deleteAccount(customer: TypedCustomer) { + asyncRunner.runAsync { + deleteAccountOffUiThread(customer) + } + } + + protected open fun deleteAccountOffUiThread(customer: TypedCustomer) { val wasSelected = isSingleSelectedAccount(customer) or // either account or one of its bank accounts is currently selected (customer.accounts.firstOrNull { isSingleSelectedBankAccount(it) } != null) @@ -372,13 +378,13 @@ open class BankingPresenter constructor( updateBalance(bankAccount, it) } - persistAccountTransactions(bankAccount, response.bookedTransactions, response.unbookedTransactions) + persistAccountTransactionsOffUiThread(bankAccount, response.bookedTransactions, response.unbookedTransactions) } protected open fun updateBalance(bankAccount: TypedBankAccount, balance: BigDecimal) { bankAccount.balance = balance - persistAccount(bankAccount.customer) + persistAccountOffUiThread(bankAccount.customer) } @@ -394,13 +400,13 @@ open class BankingPresenter constructor( } open fun accountDisplayIndexUpdated(account: TypedCustomer) { - persistAccount(account) + persistAccountAsync(account) callAccountsChangedListeners() } open fun accountUpdated(bank: TypedCustomer) { - persistAccount(bank) + persistAccountAsync(bank) callAccountsChangedListeners() @@ -408,16 +414,28 @@ open class BankingPresenter constructor( } open fun accountUpdated(account: TypedBankAccount) { - persistAccount(account.customer) + persistAccountAsync(account.customer) callAccountsChangedListeners() } - protected open fun persistAccount(customer: ICustomer<*, *>) { + protected open fun persistAccountAsync(customer: ICustomer<*, *>) { + asyncRunner.runAsync { + persistAccountOffUiThread(customer) + } + } + + /** + * Ensure that this method only gets called off UI thread (at least for Android Room db) as otherwise it may blocks UI thread. + */ + protected open fun persistAccountOffUiThread(customer: ICustomer<*, *>) { persister.saveOrUpdateAccount(customer as TypedCustomer, customers) } - protected open fun persistAccountTransactions(bankAccount: TypedBankAccount, bookedTransactions: List, unbookedTransactions: List) { + /** + * Ensure that this method only gets called off UI thread (at least for Android Room db) as otherwise it may blocks UI thread. + */ + protected open fun persistAccountTransactionsOffUiThread(bankAccount: TypedBankAccount, bookedTransactions: List, unbookedTransactions: List) { persister.saveOrUpdateAccountTransactions(bankAccount, bookedTransactions) // TODO: someday also persist unbooked transactions