Skip to content

Commit

Permalink
Reduce CountDownTimer interval when account is finalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
zaiatsartem committed Dec 12, 2024
1 parent a894670 commit ee1ceca
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
UpdateNotificationsSubscriptionUseCase(application)
}

private var countdownInterval: Long = BuildConfig.ACCOUNT_UPDATE_FREQUENCY_SEC
private var updater: CountDownTimer? = null

enum class DialogToShow {
UNSHIELDING,
NOTIFICATIONS_PERMISSION,
Expand All @@ -107,6 +110,7 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
}
viewModelScope.launch {
postState(OnboardingState.DONE)
restartUpdater(BuildConfig.ACCOUNT_UPDATE_FREQUENCY_SEC)
}
}

Expand Down Expand Up @@ -211,7 +215,11 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
postState(OnboardingState.DONE, notifyWaitingLiveData = notifyWaitingLiveData)
showSingleDialogIfNeeded()
} else {
postState(OnboardingState.FINALIZING_ACCOUNT, notifyWaitingLiveData = notifyWaitingLiveData)
postState(
state = OnboardingState.FINALIZING_ACCOUNT,
notifyWaitingLiveData = notifyWaitingLiveData
)
viewModelScope.launch { restartUpdater(5L) }
}
updateSubmissionStatesAndBalances()
}
Expand All @@ -224,12 +232,12 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
}

fun initiateFrequentUpdater() {
updater.cancel()
updater.start()
startUpdater()
}

fun stopFrequentUpdater() {
updater.cancel()
updater?.cancel()
updater = null
}

fun hasShowedInitialAnimation(): Boolean {
Expand All @@ -240,9 +248,11 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
App.appCore.session.setHasShowedInitialAnimation()
}

private var updater =
object : CountDownTimer(Long.MAX_VALUE, BuildConfig.ACCOUNT_UPDATE_FREQUENCY_SEC * 1000) {
private fun startUpdater() {
stopFrequentUpdater()
updater = object : CountDownTimer(Long.MAX_VALUE, countdownInterval * 1000) {
private var first = true

override fun onTick(millisUntilFinished: Long) {
if (first) { // ignore first tick
first = false
Expand All @@ -256,7 +266,13 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app

override fun onFinish() {
}
}
}.also { it.start() }
}

private fun restartUpdater(newInterval: Long) {
countdownInterval = newInterval
startUpdater()
}

private fun isRegularUpdateNeeded(): Boolean {
this.accountRepository.allAccountsWithIdentity.value?.forEach { accountWithIdentity ->
Expand Down

0 comments on commit ee1ceca

Please sign in to comment.