Skip to content

Commit

Permalink
Update CountDownTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaiatsartem committed Dec 13, 2024
1 parent ee1ceca commit 6309f73
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class AccountsOverviewFragment : BaseFragment() {
override fun onResume() {
super.onResume()
viewModel.updateState()
viewModel.initiateFrequentUpdater()
viewModel.initiateUpdater()
}

override fun onDestroy() {
Expand All @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
private val ccdOnrampSiteRepository: CcdOnrampSiteRepository
private val accountsObserver: Observer<List<AccountWithIdentity>>
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,
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -270,8 +267,7 @@ class AccountsOverviewViewModel(application: Application) : AndroidViewModel(app
}

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

private fun isRegularUpdateNeeded(): Boolean {
Expand Down

0 comments on commit 6309f73

Please sign in to comment.