Skip to content

Commit 07f9480

Browse files
authored
Merge pull request #387 from synonymdev/fix/time-out-calc-on_new-wallet
fix: High Balance warning time out calculation for fresh wallets
2 parents a46450c + de45b44 commit 07f9480

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

app/src/main/java/to/bitkit/ui/screens/wallets/HomeViewModel.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class HomeViewModel @Inject constructor(
4949
setupStateObservation()
5050
setupArticleRotation()
5151
setupFactRotation()
52-
checkHighBalance()
5352
}
5453

5554
private fun setupStateObservation() {
@@ -91,6 +90,7 @@ class HomeViewModel @Inject constructor(
9190
showEmptyState = settings.showEmptyBalanceView && balanceState.totalSats == 0uL
9291
)
9392
}.collect { newState ->
93+
checkHighBalance()
9494
_uiState.update { newState }
9595
}
9696
}
@@ -147,32 +147,36 @@ class HomeViewModel @Inject constructor(
147147
}
148148

149149
private fun checkHighBalance() {
150+
if (_uiState.value.highBalanceSheetVisible) return
151+
150152
viewModelScope.launch {
151153
delay(CHECK_DELAY_MILLISECONDS)
152154

153155
val settings = settingsStore.data.first()
156+
val currentTime = Clock.System.now().toEpochMilliseconds()
154157

155158
val totalOnChainSats = walletRepo.balanceState.value.totalSats
156159
val balanceUsd = satsToUsd(totalOnChainSats) ?: return@launch
157160
val thresholdReached = balanceUsd > BigDecimal(BALANCE_THRESHOLD_USD)
158-
val isTimeOutOver = settings.lastTimeAskedBalanceWarningMillis - ASK_INTERVAL_MILLIS > ASK_INTERVAL_MILLIS
161+
162+
val isTimeOutOver = settings.lastTimeAskedBalanceWarningMillis == 0L ||
163+
(currentTime - settings.lastTimeAskedBalanceWarningMillis > ASK_INTERVAL_MILLIS)
164+
159165
val belowMaxWarnings = settings.balanceWarningTimes < MAX_WARNINGS
160166

161-
if (thresholdReached && isTimeOutOver && belowMaxWarnings && !_uiState.value.highBalanceSheetVisible) {
167+
if (thresholdReached && isTimeOutOver && belowMaxWarnings) {
162168
settingsStore.update {
163169
it.copy(
164170
balanceWarningTimes = it.balanceWarningTimes + 1,
165-
lastTimeAskedBalanceWarningMillis = Clock.System.now().toEpochMilliseconds()
171+
lastTimeAskedBalanceWarningMillis = currentTime
166172
)
167173
}
168174
_uiState.update { it.copy(highBalanceSheetVisible = true) }
169175
}
170176

171177
if (!thresholdReached) {
172178
settingsStore.update {
173-
it.copy(
174-
balanceWarningTimes = 0,
175-
)
179+
it.copy(balanceWarningTimes = 0)
176180
}
177181
}
178182
}

0 commit comments

Comments
 (0)