From 6309f73bf5a44318e83ab402cf49f2402a83bc78 Mon Sep 17 00:00:00 2001 From: Artem Zaiats Date: Fri, 13 Dec 2024 12:11:27 +0200 Subject: [PATCH] Update CountDownTimer --- app/build.gradle | 1 + .../AccountsOverviewFragment.kt | 4 ++-- .../AccountsOverviewViewModel.kt | 20 ++++++++----------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ee694a88..f9089cb7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,6 +39,7 @@ android { buildConfigField("boolean", "INCL_DEV_OPTIONS", "false") buildConfigField("boolean", "SHOW_GTU_DROP", "false") buildConfigField("Long", "ACCOUNT_UPDATE_FREQUENCY_SEC", "60l") + buildConfigField("Long", "FAST_ACCOUNT_UPDATE_FREQUENCY_SEC", "5l") buildConfigField("boolean", "FAIL_IDENTITY_CREATION", "false") buildConfigField("boolean", "FORCE_NO_EMAIL_CLIENTS", "false") buildConfigField("boolean", "SHOW_NEWSFEED", "true") diff --git a/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewFragment.kt b/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewFragment.kt index 48286928..b908e263 100644 --- a/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewFragment.kt +++ b/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewFragment.kt @@ -112,7 +112,7 @@ class AccountsOverviewFragment : BaseFragment() { override fun onResume() { super.onResume() viewModel.updateState() - viewModel.initiateFrequentUpdater() + viewModel.initiateUpdater() } override fun onDestroy() { @@ -124,7 +124,7 @@ class AccountsOverviewFragment : BaseFragment() { override fun onPause() { super.onPause() - viewModel.stopFrequentUpdater() + viewModel.stopUpdater() } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { diff --git a/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewViewModel.kt b/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewViewModel.kt index 96156d9a..1d9c1e99 100644 --- a/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewViewModel.kt +++ b/app/src/main/java/com/concordium/wallet/ui/account/accountsoverview/AccountsOverviewViewModel.kt @@ -77,14 +77,12 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app private val ccdOnrampSiteRepository: CcdOnrampSiteRepository private val accountsObserver: Observer> private val notificationsPreferences: NotificationsPreferences + private var updater: CountDownTimer? = null private val updateNotificationsSubscriptionUseCase by lazy { UpdateNotificationsSubscriptionUseCase(application) } - private var countdownInterval: Long = BuildConfig.ACCOUNT_UPDATE_FREQUENCY_SEC - private var updater: CountDownTimer? = null - enum class DialogToShow { UNSHIELDING, NOTIFICATIONS_PERMISSION, @@ -210,7 +208,7 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app private suspend fun handleAccountCreation(notifyWaitingLiveData: Boolean) { val allAccounts = accountRepository.getAll() - if (allAccounts.isNotEmpty() && allAccounts.any { it.transactionStatus == TransactionStatus.FINALIZED }) { + if (allAccounts.any { it.transactionStatus == TransactionStatus.FINALIZED }) { App.appCore.session.hasCompletedOnboarding() postState(OnboardingState.DONE, notifyWaitingLiveData = notifyWaitingLiveData) showSingleDialogIfNeeded() @@ -219,7 +217,7 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app state = OnboardingState.FINALIZING_ACCOUNT, notifyWaitingLiveData = notifyWaitingLiveData ) - viewModelScope.launch { restartUpdater(5L) } + viewModelScope.launch { restartUpdater(BuildConfig.FAST_ACCOUNT_UPDATE_FREQUENCY_SEC) } } updateSubmissionStatesAndBalances() } @@ -231,11 +229,11 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app accountUpdater.updateForAllAccounts() } - fun initiateFrequentUpdater() { + fun initiateUpdater() { startUpdater() } - fun stopFrequentUpdater() { + fun stopUpdater() { updater?.cancel() updater = null } @@ -248,11 +246,10 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app App.appCore.session.setHasShowedInitialAnimation() } - private fun startUpdater() { - stopFrequentUpdater() + private fun startUpdater(countdownInterval: Long = BuildConfig.ACCOUNT_UPDATE_FREQUENCY_SEC) { + stopUpdater() updater = object : CountDownTimer(Long.MAX_VALUE, countdownInterval * 1000) { private var first = true - override fun onTick(millisUntilFinished: Long) { if (first) { // ignore first tick first = false @@ -270,8 +267,7 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app } private fun restartUpdater(newInterval: Long) { - countdownInterval = newInterval - startUpdater() + startUpdater(newInterval) } private fun isRegularUpdateNeeded(): Boolean {