Updated on 2026-08-14

This commit is contained in:
Tangem 2025-05-16 16:07:29 +05:00
parent 349d8fc7dd
commit 240dc3f3b8

View file

@ -46,10 +46,8 @@ import com.tangem.features.pushnotifications.api.utils.PUSH_PERMISSION
import com.tangem.features.pushnotifications.api.utils.getPushPermissionOrNull
import com.tangem.utils.Provider
import com.tangem.utils.coroutines.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject
@ -107,6 +105,7 @@ internal class WalletModel @Inject constructor(
subscribeToUserWalletsUpdates()
subscribeOnBalanceHiding()
subscribeOnSelectedWalletFlow()
subscribeToScreenBackgroundState()
subscribeOnPushNotificationsPermission()
clickIntents.initialize(innerWalletRouter, modelScope)
@ -225,7 +224,6 @@ internal class WalletModel @Inject constructor(
addReferralDeepLink(selectedWallet)
walletDeepLinksHandler.registerForWallet(scope = modelScope, userWallet = selectedWallet)
subscribeOnExpressTransactionsUpdates(selectedWallet)
subscribeToScreenBackgroundState(selectedWallet)
}
.flowOn(dispatchers.main)
.launchIn(modelScope)
@ -248,39 +246,38 @@ internal class WalletModel @Inject constructor(
// We need to update the current wallet quotes if the application was in the background for more than 10 seconds
// and then returned to the foreground
private fun subscribeToScreenBackgroundState(userWallet: UserWallet) {
private fun subscribeToScreenBackgroundState() {
screenLifecycleProvider.isBackgroundState
.onEach { isBackground ->
expressTxStatusTaskScheduler.cancelTask()
refreshWalletJobHolder.cancel()
when {
isBackground -> needToRefreshTimer()
needToRefreshWallet && !isBackground -> {
triggerRefreshWalletQuotes()
subscribeOnExpressTransactionsUpdates(userWallet)
}
!isBackground -> subscribeOnExpressTransactionsUpdates(userWallet)
}
}
.launchIn(modelScope)
}
private fun subscribeOnExpressTransactionsUpdates(userWallet: UserWallet) {
expressTxStatusTaskScheduler.cancelTask()
expressTxStatusTaskScheduler.scheduleTask(
modelScope,
PeriodicTask(
isDelayFirst = false,
delay = EXPRESS_STATUS_UPDATE_DELAY,
task = {
runCatching {
onrampStatusFactory.updateOnrmapTransactionStatuses(userWallet)
}
},
onSuccess = { /* no-op */ },
onError = { /* no-op */ },
),
)
if (!userWallet.isMultiCurrency) {
expressTxStatusTaskScheduler.cancelTask()
expressTxStatusTaskScheduler.scheduleTask(
modelScope,
PeriodicTask(
isDelayFirst = false,
delay = EXPRESS_STATUS_UPDATE_DELAY,
task = {
runCatching {
onrampStatusFactory.updateOnrmapTransactionStatuses(userWallet)
}
},
onSuccess = { /* no-op */ },
onError = { /* no-op */ },
),
)
}
}
private fun needToRefreshTimer() {
@ -295,9 +292,18 @@ internal class WalletModel @Inject constructor(
val state = stateHolder.uiState.value
val wallet = state.wallets.getOrNull(state.selectedWalletIndex) ?: return
modelScope.launch {
refreshMultiCurrencyWalletQuotesUseCase(wallet.walletCardState.id).getOrElse {
Timber.e("Failed to refreshMultiCurrencyWalletQuotesUseCase $it")
}
awaitAll(
async {
refreshMultiCurrencyWalletQuotesUseCase(wallet.walletCardState.id).getOrElse {
Timber.e("Failed to refreshMultiCurrencyWalletQuotesUseCase $it")
}
},
async {
getWalletsUseCase.invokeSync()
.firstOrNull { it.walletId == wallet.walletCardState.id }
?.let(::subscribeOnExpressTransactionsUpdates)
},
)
}.saveIn(refreshWalletJobHolder)
}