Skip to content

Commit

Permalink
Merge pull request #338 from tangem/bugfix/AND-4848_Redesign_Request_…
Browse files Browse the repository at this point in the history
…for_the_balance_is_sent_at_every_pull-to-refresh_in_the_Token_Details

AND-4848 Redesign: Request for the balance is sent at every pull-to-refresh in the Token Details
  • Loading branch information
kozarezvlad authored Oct 11, 2023
2 parents e911b32 + 229987b commit 026a7d3
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BinanceWalletManager(
override val currentHost: String
get() = networkProvider.host

override suspend fun update() {
override suspend fun updateInternal() {
val result = networkProvider.getInfo(wallet.address)
when (result) {
is Result.Success -> updateWallet(result.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ open class BitcoinWalletManager(
override val currentHost: String
get() = networkProvider.baseUrl

override suspend fun update() {
override suspend fun updateInternal() {
coroutineScope {
val addressInfos = mutableListOf<BitcoinAddressInfo>()
val responsesDeferred =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BitcoinCashWalletManager(

override val minimalFee = 0.00001.toBigDecimal()

override suspend fun update() {
override suspend fun updateInternal() {
when (val response = networkProvider.getInfo(wallet.address)) {
is Result.Success -> updateWallet(response.data)
is Result.Failure -> updateError(response.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CardanoWalletManager(
override val currentHost: String
get() = networkProvider.baseUrl

override suspend fun update() {
override suspend fun updateInternal() {
when (val response = networkProvider.getInfo(wallet.addresses.map { it.value }.toSet())) {
is Result.Success -> updateWallet(response.data)
is Result.Failure -> updateError(response.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ChiaWalletManager(
private val blockchain = wallet.blockchain
private val puzzleHash = ChiaAddressService.getPuzzleHash(wallet.address).toHexString()

override suspend fun update() {
override suspend fun updateInternal() {
when (val response = networkProvider.getUnspents(puzzleHash)) {
is Result.Success -> updateWallet(response.data)
is Result.Failure -> updateError(response.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CosmosWalletManager(
get() = networkService.host

override val allowsFeeSelection: FeeSelectionState = cosmosChain.allowsFeeSelection
override suspend fun update() {
override suspend fun updateInternal() {
val unconfirmedTxHashes = wallet.recentTransactions
.filter { it.status == TransactionStatus.Unconfirmed }
.mapNotNull { it.hash }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class EthereumWalletManager(
override val currentHost: String
get() = networkProvider.baseUrl

override suspend fun update() {
override suspend fun updateInternal() {
when (val result = networkProvider.getInfo(wallet.address, cardTokens)) {
is Result.Success -> updateWallet(result.data)
is Result.Failure -> updateError(result.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class KaspaWalletManager(
private val blockchain = wallet.blockchain
override val dustValue: BigDecimal = FEE_PER_UNSPENT_OUTPUT.toBigDecimal()

override suspend fun update() {
override suspend fun updateInternal() {
when (val response = networkProvider.getInfo(wallet.address)) {
is Result.Success -> updateWallet(response.data)
is Result.Failure -> updateError(response.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NearWalletManager(
override val currentHost: String
get() = networkService.host

override suspend fun update() {
override suspend fun updateInternal() {
when (val walletInfoResult = networkService.getAccount(wallet.address)) {
is Result.Success -> {
when (val account = walletInfoResult.data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PolkadotWalletManager(
override val currentHost: String
get() = networkProvider.baseUrl

override suspend fun update() {
override suspend fun updateInternal() {
val amount = networkProvider.getBalance(wallet.address).successOr {
wallet.removeAllTokens()
throw (it.error as BlockchainSdkError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SolanaWalletManager(
private val feeRentHolder = mutableMapOf<Fee, BigDecimal>()
private val valueConverter = ValueConverter()

override suspend fun update() {
override suspend fun updateInternal() {
val accountInfo = multiNetworkProvider.performRequest {
getMainAccountInfo(accountPubK)
}.successOr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class StellarWalletManager(
private var baseReserve = BASE_RESERVE
private var sequence = 0L

override suspend fun update() {
override suspend fun updateInternal() {
when (val result = networkProvider.getInfo(wallet.address)) {
is Result.Failure -> updateError(result.error)
is Result.Success -> updateWallet(result.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TezosWalletManager(
private val blockchain = wallet.blockchain
private var publicKeyRevealed: Boolean? = null

override suspend fun update() {
override suspend fun updateInternal() {
when (val response = networkProvider.getInfo(wallet.address)) {
is Result.Success -> updateWallet(response.data)
is Result.Failure -> updateError(response.error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TonWalletManager(
override val currentHost: String
get() = networkService.host

override suspend fun update() {
override suspend fun updateInternal() {
when (val walletInfoResult = networkService.getWalletInformation(wallet.address)) {
is Result.Failure -> updateError(walletInfoResult.error)
is Result.Success -> updateWallet(walletInfoResult.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TronWalletManager(

private val dummySigner = DummySigner()

override suspend fun update() {
override suspend fun updateInternal() {
val transactionIds = wallet.recentTransactions
.filter { it.status == TransactionStatus.Unconfirmed }
.mapNotNull { it.hash }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class XrpWalletManager(

private val blockchain = wallet.blockchain

override suspend fun update() {
override suspend fun updateInternal() {
when (val result = networkProvider.getInfo(wallet.address)) {
is Result.Success -> updateWallet(result.data)
is Result.Failure -> updateError(result.error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tangem.blockchain.common

import com.tangem.blockchain.extensions.DebouncedInvoke
import com.tangem.blockchain.common.transaction.Fee
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.blockchain.common.txhistory.DefaultTransactionHistoryProvider
Expand Down Expand Up @@ -28,7 +29,18 @@ abstract class WalletManager(

open val dustValue: BigDecimal? = null

abstract suspend fun update()
private val updateDebounced = DebouncedInvoke()

/**
* Update wallet state. [forceUpdate] to skip debounce
*/
suspend fun update(forceUpdate: Boolean = false) {
updateDebounced.invokeOnExpire(forceUpdate) {
updateInternal()
}
}

internal abstract suspend fun updateInternal()

protected open fun updateRecentTransactionsBasic(transactions: List<BasicTransactionData>) {
val (confirmedTransactions, unconfirmedTransactions) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tangem.blockchain.extensions

/**
* Class wrapping method invocation to be debounced within specified interval [invokeIntervalMillis].
*
* @param invokeIntervalMillis interval [invokeIntervalMillis]
*/
internal class DebouncedInvoke(
private val invokeIntervalMillis: Long = 10000L,
) {
private var lastInvokeTime = 0L

/** Call executable [block] with specified interval or force execution with [forceUpdate] */
suspend fun invokeOnExpire(forceUpdate: Boolean = false, block: suspend () -> Unit) {
if (forceUpdate) {
block()
} else if (isExpired()) {
lastInvokeTime = System.currentTimeMillis()
block()
}
}

private fun isExpired(): Boolean {
return System.currentTimeMillis() - lastInvokeTime >= invokeIntervalMillis
}
}

0 comments on commit 026a7d3

Please sign in to comment.