Updated on 2026-08-14

This commit is contained in:
Tangem 2023-10-04 20:53:22 +03:00
parent d51d499943
commit 8c3eb4b604
3 changed files with 28 additions and 23 deletions

View file

@ -5,6 +5,8 @@ import com.tangem.domain.balancehiding.IsBalanceHiddenUseCase
import com.tangem.domain.balancehiding.ListenToFlipsUseCase
import com.tangem.domain.tokens.UpdateDelayedNetworkStatusUseCase
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.usecase.GetSelectedWalletUseCase
import com.tangem.features.send.navigation.SendRouter
import com.tangem.tap.di.DelayedWork
@ -60,7 +62,10 @@ internal class SendViewModel @Inject constructor(
.fold(
ifLeft = { Timber.e(it.toString()) },
ifRight = { wallet ->
updateDelayedCurrencyStatusUseCase(wallet.walletId, cryptoCurrency.network, true)
// we should update network to find pending tx after 1 sec
updateForPendingTx(wallet, cryptoCurrency.network)
// we should update network for new balance
updateForBalance(wallet, cryptoCurrency.network)
},
)
}
@ -69,7 +74,27 @@ internal class SendViewModel @Inject constructor(
}
}
private suspend fun updateForPendingTx(userWallet: UserWallet, network: Network) {
updateDelayedCurrencyStatusUseCase(
userWalletId = userWallet.walletId,
network = network,
delayMillis = UPDATE_PENDING_TX_DELAY_MILLIS,
refresh = true,
)
}
private suspend fun updateForBalance(userWallet: UserWallet, network: Network) {
updateDelayedCurrencyStatusUseCase(
userWalletId = userWallet.walletId,
network = network,
delayMillis = UPDATE_BALANCE_DELAY_MILLIS,
refresh = true,
)
}
companion object {
private const val UPDATE_BALANCE_DELAY_MILLIS = 11000L
private const val UPDATE_PENDING_TX_DELAY_MILLIS = 1000L
private const val TAG = "SendViewModel"
}
}

View file

@ -33,9 +33,10 @@ class UpdateDelayedNetworkStatusUseCase(
suspend operator fun invoke(
userWalletId: UserWalletId,
network: Network,
delayMillis: Long,
refresh: Boolean = false,
): Either<CurrencyStatusError, Unit> {
delay(DELAY_MILLIS)
delay(delayMillis)
return either {
fetchNetworkStatus(userWalletId, network, refresh)
}
@ -52,8 +53,4 @@ class UpdateDelayedNetworkStatusUseCase(
raise(CurrencyStatusError.DataError(it))
}
}
companion object {
private const val DELAY_MILLIS = 11000L
}
}

View file

@ -35,7 +35,6 @@ import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import com.tangem.utils.coroutines.JobHolder
import com.tangem.utils.coroutines.saveIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import timber.log.Timber
@ -72,7 +71,6 @@ internal class TokenDetailsViewModel @Inject constructor(
private val marketPriceJobHolder = JobHolder()
private val refreshStateJobHolder = JobHolder()
private val networkStatusAutoUpdateStateJobHolder = JobHolder()
private var cryptoCurrencyStatus: CryptoCurrencyStatus? = null
private var wallet by Delegates.notNull<UserWallet>()
@ -162,17 +160,6 @@ internal class TokenDetailsViewModel @Inject constructor(
.flowOn(dispatchers.io)
.launchIn(viewModelScope)
.saveIn(marketPriceJobHolder)
viewModelScope.launch(dispatchers.io) {
// Wait for blockchain updates pending transactions.
// Immediate update doesn't receive any changes.
delay(NETWORK_STATUS_AUTO_UPDATE_DELAY)
fetchCurrencyStatusUseCase.invoke(
userWalletId = wallet.walletId,
id = cryptoCurrency.id,
refresh = true,
)
}.saveIn(networkStatusAutoUpdateStateJobHolder)
}
private fun updateTxHistory(refresh: Boolean = false) {
@ -389,8 +376,4 @@ internal class TokenDetailsViewModel @Inject constructor(
override fun onCloseRentInfoNotification() {
uiState = stateFactory.getStateWithRemovedRentNotification()
}
companion object {
private const val NETWORK_STATUS_AUTO_UPDATE_DELAY = 1000L
}
}