From 7e2b094f13cfc4592b4e961daaaf9dc3bd1dfcd6 Mon Sep 17 00:00:00 2001 From: Tangem Date: Sat, 18 Feb 2023 18:07:01 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tap/common/extensions/WalletManager.kt | 30 +++ .../tap/domain/model/WalletDataModel.kt | 2 + .../model/builders/WalletStoreBuilder.kt | 6 + .../DefaultWalletAmountsRepository.kt | 6 +- .../utils/WalletDataOperations.kt | 31 ++- .../utils/WalletStoreOperations.kt | 8 + .../tap/features/wallet/redux/WalletState.kt | 2 + .../wallet/redux/middlewares/Mapper.kt | 1 + .../redux/reducers/MultiWalletReducer.kt | 10 +- .../redux/reducers/TotalBalanceOperations.kt | 4 +- .../wallet/redux/reducers/WalletReducer.kt | 1 + .../tap/features/wallet/ui/WalletFragment.kt | 2 - .../features/wallet/ui/wallet/WalletView.kt | 6 +- .../ui/wallet/saltPay/SaltPayWalletView.kt | 201 ++++++++++-------- .../res/layout/layout_salt_pay_balance.xml | 3 +- buildSrc/src/main/java/Versions.kt | 2 +- 16 files changed, 204 insertions(+), 111 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/common/extensions/WalletManager.kt b/app/src/main/java/com/tangem/tap/common/extensions/WalletManager.kt index cda489f536..a07ef080da 100644 --- a/app/src/main/java/com/tangem/tap/common/extensions/WalletManager.kt +++ b/app/src/main/java/com/tangem/tap/common/extensions/WalletManager.kt @@ -2,6 +2,9 @@ package com.tangem.tap.common.extensions import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.BlockchainSdkError +import com.tangem.blockchain.common.Token +import com.tangem.blockchain.common.TransactionData +import com.tangem.blockchain.common.TransactionHistoryProvider import com.tangem.blockchain.common.Wallet import com.tangem.blockchain.common.WalletManager import com.tangem.common.services.Result @@ -10,6 +13,7 @@ import com.tangem.tap.domain.TapError import com.tangem.tap.domain.extensions.amountToCreateAccount import com.tangem.tap.domain.getFirstToken import com.tangem.tap.features.demo.isDemoCard +import com.tangem.tap.features.wallet.models.Currency import com.tangem.tap.features.wallet.redux.AddressData import com.tangem.tap.features.wallet.redux.reducers.createAddressesData import com.tangem.tap.network.NetworkConnectivity @@ -30,6 +34,9 @@ suspend fun WalletManager.safeUpdate(): Result = try { Result.Success(wallet) } else { update() + if (wallet.blockchain == Blockchain.SaltPay && this is TransactionHistoryProvider) { + this.getTransactionHistory(wallet.address, wallet.blockchain, wallet.getTokens()) + } Result.Success(wallet) } } catch (exception: Exception) { @@ -76,10 +83,33 @@ fun WalletManager?.getAddressData(): AddressData? { else addressDataList[0] } +fun WalletManager.getTxHistory(currency: Currency): List = wallet.getTxHistory(currency) + +fun WalletManager.getBlockchainTxHistory(): List = wallet.getBlockchainTxHistory() + +fun WalletManager.getTokenTxHistory(token: Token): List = wallet.getTokenTxHistory(token) + fun WalletManager.Companion.stub(): T { val wallet = Wallet(Blockchain.Unknown, setOf(), Wallet.PublicKey(byteArrayOf(), null, null), setOf()) return object : WalletManager(wallet) { override val currentHost: String = "" override suspend fun update() {} } as T +} + +fun Wallet.getTxHistory(currency: Currency): List { + return (currency as? Currency.Token)?.let { this.getTokenTxHistory(it.token) } + ?: getBlockchainTxHistory() +} + +fun Wallet.getBlockchainTxHistory(): List { + return historyTransactions.filter { + it.contractAddress.isNullOrEmpty() + } +} + +fun Wallet.getTokenTxHistory(token: Token): List { + return historyTransactions.filter { + it.contractAddress == token.contractAddress + } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/model/WalletDataModel.kt b/app/src/main/java/com/tangem/tap/domain/model/WalletDataModel.kt index 3429afa07c..b9e83390a0 100644 --- a/app/src/main/java/com/tangem/tap/domain/model/WalletDataModel.kt +++ b/app/src/main/java/com/tangem/tap/domain/model/WalletDataModel.kt @@ -1,5 +1,6 @@ package com.tangem.tap.domain.model +import com.tangem.blockchain.common.TransactionData import com.tangem.tap.domain.model.WalletDataModel.Status import com.tangem.tap.features.wallet.models.Currency import com.tangem.tap.features.wallet.models.PendingTransaction @@ -23,6 +24,7 @@ data class WalletDataModel( val existentialDeposit: BigDecimal?, val fiatRate: BigDecimal?, val isCardSingleToken: Boolean, + val historyTransactions: List?, ) { /** diff --git a/app/src/main/java/com/tangem/tap/domain/model/builders/WalletStoreBuilder.kt b/app/src/main/java/com/tangem/tap/domain/model/builders/WalletStoreBuilder.kt index 651ab25018..5b4f90aea3 100644 --- a/app/src/main/java/com/tangem/tap/domain/model/builders/WalletStoreBuilder.kt +++ b/app/src/main/java/com/tangem/tap/domain/model/builders/WalletStoreBuilder.kt @@ -6,6 +6,8 @@ import com.tangem.blockchain.common.Token import com.tangem.blockchain.common.WalletManager import com.tangem.common.hdWallet.DerivationPath import com.tangem.domain.common.util.UserWalletId +import com.tangem.tap.common.extensions.getBlockchainTxHistory +import com.tangem.tap.common.extensions.getTokenTxHistory import com.tangem.tap.domain.model.WalletDataModel import com.tangem.tap.domain.model.WalletStoreModel import com.tangem.tap.domain.tokens.models.BlockchainNetwork @@ -98,6 +100,7 @@ private fun BlockchainNetwork.getBlockchainWalletData(walletManager: WalletManag existentialDeposit = getExistentialDeposit(walletManager), fiatRate = null, isCardSingleToken = false, + historyTransactions = walletManager?.getBlockchainTxHistory(), ) } @@ -115,6 +118,7 @@ private fun BlockchainNetwork.getTokensWalletsData(walletManager: WalletManager? existentialDeposit = getExistentialDeposit(walletManager), fiatRate = null, isCardSingleToken = walletManager?.cardTokens?.contains(token) ?: false, + historyTransactions = walletManager?.getTokenTxHistory(token), ) } } @@ -131,6 +135,7 @@ private fun Blockchain.toBlockchainWalletData(walletManager: WalletManager): Wal existentialDeposit = getExistentialDeposit(walletManager), fiatRate = null, isCardSingleToken = false, + historyTransactions = walletManager.getBlockchainTxHistory(), ) } @@ -147,6 +152,7 @@ private fun Token.toTokenWalletData(walletManager: WalletManager): WalletDataMod existentialDeposit = getExistentialDeposit(walletManager), fiatRate = null, isCardSingleToken = walletManager.cardTokens.contains(this), + historyTransactions = walletManager.getTokenTxHistory(this), ) } diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt index 93c70be9ef..66c90b9f9e 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletAmountsRepository.kt @@ -20,6 +20,7 @@ import com.tangem.domain.common.ScanResponse import com.tangem.domain.common.util.UserWalletId import com.tangem.tap.common.entities.FiatCurrency import com.tangem.tap.common.extensions.replaceByOrAdd +import com.tangem.tap.common.extensions.safeUpdate import com.tangem.tap.domain.model.UserWallet import com.tangem.tap.domain.model.WalletStoreModel import com.tangem.tap.domain.walletStores.WalletStoresError @@ -32,6 +33,7 @@ import com.tangem.tap.domain.walletStores.repository.implementation.utils.update import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithFiatRates import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithMissedDerivation import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithRent +import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithTxHistories import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithUnreachable import com.tangem.tap.domain.walletStores.storage.WalletManagerStorage import com.tangem.tap.domain.walletStores.storage.WalletStoresStorage @@ -203,7 +205,7 @@ internal class DefaultWalletAmountsRepository( updateWalletStoreWithUnreachable(walletStore) } else -> { - withInternetConnection { walletManager.update() } + withInternetConnection { walletManager.safeUpdate() } .map { updateWalletManagerWithAmounts(userWalletId, walletManager) } .flatMap { updateWalletStoreWithAmounts( @@ -318,7 +320,7 @@ internal class DefaultWalletAmountsRepository( it.updateWithDemoAmounts(wallet = updatedWallet) } else { it.updateWithAmounts(wallet = updatedWallet) - } + }.updateWithTxHistories(wallet = updatedWallet) }, ) } diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt index cb49fe33c1..48b0c4d71e 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt @@ -4,6 +4,8 @@ import com.tangem.blockchain.common.AmountType import com.tangem.blockchain.common.BlockchainSdkError import com.tangem.blockchain.common.Wallet import com.tangem.common.core.TangemError +import com.tangem.tap.common.extensions.getBlockchainTxHistory +import com.tangem.tap.common.extensions.getTokenTxHistory import com.tangem.tap.domain.extensions.amountToCreateAccount import com.tangem.tap.domain.getFirstToken import com.tangem.tap.domain.model.WalletDataModel @@ -29,6 +31,29 @@ internal fun List.updateWithFiatRates( } } +internal fun WalletDataModel.updateWithTxHistory(wallet: Wallet): WalletDataModel { + return this.copy( + historyTransactions = when (currency) { + is Currency.Blockchain -> wallet.getBlockchainTxHistory() + is Currency.Token -> wallet.getTokenTxHistory(currency.token) + }, + ) +} + +internal fun List.updateWithTxHistories( + wallet: Wallet, +): List { + return this.map { walletData -> + walletData.updateWithTxHistory(wallet) + } +} + +internal fun List.updateWithAmounts(wallet: Wallet): List { + return this.map { walletData -> + walletData.updateWithAmount(wallet) + } +} + internal fun WalletDataModel.updateWithAmount(wallet: Wallet): WalletDataModel { val pendingTransactions = wallet.getPendingTransactions() return this.copy( @@ -82,12 +107,6 @@ internal fun WalletDataModel.updateWithDemoAmount(wallet: Wallet): WalletDataMod ) } -internal fun List.updateWithAmounts(wallet: Wallet): List { - return this.map { walletData -> - walletData.updateWithAmount(wallet) - } -} - internal fun List.updateWithDemoAmounts(wallet: Wallet): List { return this.map { walletData -> walletData.updateWithDemoAmount(wallet) diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt index 0c3e47861c..4bf5394553 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt @@ -41,6 +41,14 @@ internal fun WalletStoreModel.updateWithError( ) } +internal fun WalletStoreModel.updateWithTxHistories( + wallet: Wallet, +): WalletStoreModel { + return this.copy( + walletsData = walletsData.updateWithTxHistories(wallet = wallet), + ) +} + internal fun WalletStoreModel.updateWithAmounts( wallet: Wallet, ): WalletStoreModel { diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt index 7e37e27a5a..93bc2c36fd 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt @@ -3,6 +3,7 @@ package com.tangem.tap.features.wallet.redux import android.graphics.Bitmap import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.Token +import com.tangem.blockchain.common.TransactionData import com.tangem.blockchain.common.Wallet import com.tangem.blockchain.common.WalletManager import com.tangem.blockchain.common.address.AddressType @@ -325,6 +326,7 @@ data class Artwork( data class WalletData( val pendingTransactions: List = emptyList(), + val historyTransactions: List? = null, val hashesCountVerified: Boolean? = null, val walletAddresses: WalletAddresses? = null, val currencyData: BalanceWidgetData = BalanceWidgetData(), diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/Mapper.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/Mapper.kt index 9433c8b6e7..518b4aba41 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/Mapper.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/Mapper.kt @@ -116,6 +116,7 @@ private fun WalletDataModel.mapToReduxModel( ?.toString(), errorMessage = status.errorMessage, ), + historyTransactions = historyTransactions, ) } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt index 408cd883d5..9c4b0f8cfd 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt @@ -6,6 +6,8 @@ import com.tangem.blockchain.common.AmountType import com.tangem.blockchain.common.Token import com.tangem.blockchain.common.WalletManager import com.tangem.tap.common.extensions.dispatchToastNotification +import com.tangem.tap.common.extensions.getBlockchainTxHistory +import com.tangem.tap.common.extensions.getTokenTxHistory import com.tangem.tap.common.extensions.toFiatString import com.tangem.tap.common.extensions.toFormattedCurrencyString import com.tangem.tap.common.redux.navigation.AppScreen @@ -53,6 +55,7 @@ class MultiWalletReducer { blockchain.derivationPath, ), existentialDepositString = getExistentialDeposit(walletManager), + historyTransactions = walletManager?.getBlockchainTxHistory(), ) WalletStore( @@ -88,6 +91,7 @@ class MultiWalletReducer { action.blockchain.derivationPath, ), existentialDepositString = getExistentialDeposit(walletManager), + historyTransactions = walletManager?.getBlockchainTxHistory(), ) val walletStore = WalletStore( walletManager = walletManager, @@ -151,6 +155,7 @@ class MultiWalletReducer { derivationPath = action.blockchain.derivationPath, ), walletRent = findWalletRent(state.getWalletStore(walletManager.wallet)), + historyTransactions = walletManager.getTokenTxHistory(action.token) ) state.updateWalletData(newTokenWalletData) } @@ -204,8 +209,8 @@ fun Token.toWallet(state: WalletState, blockchain: BlockchainNetwork): WalletDat val currency = Currency.fromBlockchainNetwork(blockchain, this) if (state.currencies.contains(currency)) return null - val walletManager = state.getWalletManager(currency)?.wallet - val walletAddresses = createAddressList(walletManager) + val walletManager = state.getWalletManager(currency) + val walletAddresses = createAddressList(walletManager?.wallet) return WalletData( currencyData = BalanceWidgetData( @@ -216,5 +221,6 @@ fun Token.toWallet(state: WalletState, blockchain: BlockchainNetwork): WalletDat walletAddresses = walletAddresses, mainButton = WalletMainButton.SendButton(false), currency = currency, + historyTransactions = walletManager?.getTokenTxHistory(this), ) } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt index 08a7def4bd..9a8f4ded04 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt @@ -5,7 +5,9 @@ import com.tangem.tap.features.wallet.redux.WalletData import com.tangem.tap.features.wallet.ui.BalanceStatus import java.math.BigDecimal -fun List.findProgressState(): ProgressState { +fun List.findProgressState(initialState: ProgressState = ProgressState.Done): ProgressState { + if (this.isEmpty()) return initialState + return this .mapToProgressState() .reduce(ProgressState::or) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt index f3366b882f..2653083ecf 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt @@ -350,6 +350,7 @@ private fun internalReduce(action: Action, state: AppState, appStateHolder: AppS } is WalletAction.WalletStoresChanged.UpdateWalletStores -> { newState = newState.copy( + state = action.reduxWalletStores.flatMap { it.walletsData }.findProgressState(newState.state), walletsStores = action.reduxWalletStores, ) } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt index 31f63630c1..ab946481ec 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt @@ -186,8 +186,6 @@ class WalletFragment : Fragment(R.layout.fragment_wallet), StoreSubscriber Unit)? = null - protected var fragment: WalletFragment? = null protected var binding: FragmentWalletBinding? = null @@ -22,9 +19,8 @@ abstract class WalletView { binding = null } - //TODO: blueprint open fun onViewDestroy() { - pullToRefreshListener = null + removeFragment() } open fun onDestroyFragment() {} diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/saltPay/SaltPayWalletView.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/saltPay/SaltPayWalletView.kt index 62dccf8f6d..b1d18042d6 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/saltPay/SaltPayWalletView.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/saltPay/SaltPayWalletView.kt @@ -2,19 +2,16 @@ package com.tangem.tap.features.wallet.ui.wallet.saltPay import android.view.LayoutInflater import android.view.ViewGroup -import com.tangem.blockchain.blockchains.ethereum.EthereumWalletManager -import com.tangem.blockchain.extensions.Result +import com.skydoves.androidveil.VeilLayout import com.tangem.domain.common.extensions.debounce import com.tangem.domain.common.extensions.withMainContext import com.tangem.tap.common.ShimmerData import com.tangem.tap.common.ShimmerRecyclerAdapter import com.tangem.tap.common.extensions.animateVisibility -import com.tangem.tap.common.extensions.beginDelayedTransition import com.tangem.tap.common.extensions.formatAmountAsSpannedString import com.tangem.tap.common.extensions.hide import com.tangem.tap.common.extensions.show import com.tangem.tap.common.recyclerView.SpaceItemDecoration -import com.tangem.tap.domain.getFirstToken import com.tangem.tap.features.wallet.redux.ProgressState import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.features.wallet.redux.WalletState @@ -31,40 +28,41 @@ import com.tangem.wallet.databinding.ItemSaltPayTxHistoryShimmerBinding import com.tangem.wallet.databinding.LayoutSaltPayBalanceBinding import com.tangem.wallet.databinding.LayoutSaltPayTxHistoryBinding import com.tangem.wallet.databinding.LayoutSaltPayWalletBinding -import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.rekotlin.Action -import java.util.concurrent.atomic.AtomicBoolean +import timber.log.Timber /** [REDACTED_AUTHOR] */ class SaltPayWalletView : WalletView() { - private lateinit var saltPayBinding: LayoutSaltPayWalletBinding + private var saltPayBinding: LayoutSaltPayWalletBinding? = null + private val actionDebouncer = debounce(500, mainScope) { store.dispatch(it) } - private val balanceWidget: LayoutSaltPayBalanceBinding - get() = saltPayBinding.lSaltPayBalance + private val balanceWidget: LayoutSaltPayBalanceBinding? + get() = saltPayBinding?.lSaltPayBalance - private val txWidget: LayoutSaltPayTxHistoryBinding - get() = saltPayBinding.lSaltPayTxHistory + private val txWidget: LayoutSaltPayTxHistoryBinding? + get() = saltPayBinding?.lSaltPayTxHistory - private var initialized = AtomicBoolean(false) + private val itemDecorator = SpaceItemDecoration.vertical(10F) override fun changeWalletView(fragment: WalletFragment, binding: FragmentWalletBinding) { + Timber.d("changeWalletView") saltPayBinding = binding.lSaltPayWallet - setFragment(fragment, binding) - onViewCreated() showSaltPayView(binding) + onViewCreated() } override fun onViewCreated() { - this.pullToRefreshListener = this::handlePullToRefresh - prepareTxRecyclers() + Timber.d("onViewCreated") + prepareTxRecyclers(txWidget!!) } private fun showSaltPayView(binding: FragmentWalletBinding) = with(binding) { + Timber.d("showSaltPayView") rvWarningMessages.hide() rvPendingTransaction.hide() rvMultiwallet.hide() @@ -78,32 +76,41 @@ class SaltPayWalletView : WalletView() { lSaltPayWallet.root.show() } - private fun prepareTxRecyclers() { + private fun prepareTxRecyclers(txWidget: LayoutSaltPayTxHistoryBinding) = with(txWidget) { + Timber.d("prepareTxRecyclers") val vhViewFactory: (ViewGroup) -> ViewGroup = { val inflater = LayoutInflater.from(it.context) ItemSaltPayTxHistoryShimmerBinding.inflate(inflater, it, false).root } val adapter = ShimmerRecyclerAdapter(vhViewFactory) - txWidget.rvTxHistoryShimmer.adapter = adapter - txWidget.rvTxHistoryShimmer.addItemDecoration(SpaceItemDecoration.vertical(10F)) + rvTxHistoryShimmer.adapter = adapter + rvTxHistoryShimmer.addItemDecoration(itemDecorator) + adapter.submitList( + listOf( + ShimmerData(), + ShimmerData(), + ShimmerData(), + ), + ) - txWidget.rvTxHistory.adapter = TxHistoryAdapter() - txWidget.rvTxHistory.addItemDecoration(SpaceItemDecoration.vertical(10F)) - - handleTxInit() - } - - private fun handlePullToRefresh() { - requestTxHistory(store.state.walletState) + rvTxHistory.adapter = TxHistoryAdapter() + rvTxHistory.addItemDecoration(itemDecorator) } override fun onNewState(state: WalletState) { - if (!initialized.getAndSet(true)) requestTxHistory(state) - setupBalanceWidget(state) + val balanceWidget = balanceWidget ?: return + val txWidget = txWidget ?: return + + setupBalanceWidget(balanceWidget, state) + setupTxHistoryWidget(txWidget, state) } - private fun setupBalanceWidget(state: WalletState) = with(balanceWidget) { - val tokenData = state.primaryTokenData ?: return@with + private fun setupBalanceWidget( + balanceWidget: LayoutSaltPayBalanceBinding, + state: WalletState, + ) = with(balanceWidget) { + Timber.d("setupBalanceWidget") + val tokenData = state.primaryTokenData ?: return val appCurrency = store.state.globalState.appCurrency val mainProgressState = state.state @@ -119,6 +126,7 @@ class SaltPayWalletView : WalletView() { veilBalanceCrypto.animateVisibility(show = mainProgressState != ProgressState.Error) if (tokenData.currencyData.fiatAmount == null) { + veilBalance.veil() actionDebouncer(WalletAction.LoadFiatRate()) } else { veilBalance.unVeil() @@ -140,94 +148,107 @@ class SaltPayWalletView : WalletView() { } } - private fun requestTxHistory(state: WalletState) { + private fun setupTxHistoryWidget(txWidget: LayoutSaltPayTxHistoryBinding, state: WalletState) { + Timber.d("setupTxHistoryWidget") + if (state.state == ProgressState.Loading) { + handleTxLoading(txWidget) + return + } + if (state.state == ProgressState.Error) { + handleTxError(txWidget) + return + } + if (state.state != ProgressState.Done) return + val walletAddress = state.primaryWalletManager?.wallet?.address ?: return + val tokenData = state.primaryTokenData ?: return + val historyTransactions = tokenData.historyTransactions ?: return + if (historyTransactions.isEmpty()) { + handleTxEmpty(txWidget) + return + } + scope.launch { - val walletManager = state.primaryWalletManager as? EthereumWalletManager ?: return@launch - val wallet = walletManager.wallet - val token = wallet.getFirstToken() ?: return@launch - val walletAddress = wallet.address - // val walletAddress = "0xDA94Aae02a4Db0e09E1Cf240E3a0973ba89052cf" - when (val result = walletManager.getTransactionHistory(walletAddress, wallet.blockchain, setOf(token))) { - is Result.Success -> { - val dateAssociatedHistory = mutableMapOf>() - - result.data - .filter { it.contractAddress == token.contractAddress } - .sortedByDescending { it.date?.timeInMillis ?: 0 } - .forEach { - val txData = HistoryTransactionData(it, walletAddress) - val list = dateAssociatedHistory[txData.date] ?: mutableListOf() - list.add(txData) - dateAssociatedHistory[txData.date] = list - } - - val dataList = mutableListOf() - dateAssociatedHistory.forEach { entry -> - dataList.add(HistoryItemData.Date(entry.key)) - entry.value.forEach { dataList.add(HistoryItemData.TransactionData(it)) } - } - // .toMutableList().apply { clear() } - - delay(300) - withMainContext { - if (dataList.isEmpty()) { - handleTxEmpty() - } else { - handleTxSuccess(dataList) - } - } - } - is Result.Failure -> { - delay(300) - withMainContext { handleTxError() } + val dateAssociatedHistory = mutableListOf>>() + historyTransactions.forEach { tx -> + val txHistoryData = HistoryTransactionData(tx, walletAddress) + val item = dateAssociatedHistory.firstOrNull { it.first == txHistoryData.date } + if (item == null) { + dateAssociatedHistory.add(Pair(txHistoryData.date, mutableListOf(txHistoryData))) + } else { + item.second.add(txHistoryData) } } + + val rvDataList = mutableListOf() + dateAssociatedHistory.forEach { (txData, txHistoryData) -> + rvDataList.add(HistoryItemData.Date(txData)) + rvDataList.addAll(txHistoryData.map { HistoryItemData.TransactionData(it) }) + } + + withMainContext { handleTxSuccess(txWidget, rvDataList) } } } - private fun handleTxInit() = with(txWidget) { - (rvTxHistoryShimmer.adapter as? ShimmerRecyclerAdapter)?.submitList( - listOf( - ShimmerData(), - ShimmerData(), - ShimmerData(), - ), - ) + private fun handleTxLoading(txWidget: LayoutSaltPayTxHistoryBinding) = with(txWidget) { + Timber.d("handleTxLoading") groupEmpty.hide() groupError.hide() groupSuccess.hide() - root.beginDelayedTransition() - txWidget.groupShimmer.show() + // root.beginDelayedTransition() + groupShimmer.show() } - private fun handleTxSuccess(dataList: List) = with(txWidget) { + private fun handleTxSuccess( + txWidget: LayoutSaltPayTxHistoryBinding, + dataList: List, + ) = with(txWidget) { + Timber.d("handleTxSuccess") groupShimmer.hide() groupEmpty.hide() groupError.hide() - root.beginDelayedTransition() - updateTxHistoryWidget(dataList) + // root.beginDelayedTransition() + updateTxHistoryWidget(txWidget, dataList) groupSuccess.show() } - private fun handleTxEmpty() = with(txWidget) { + private fun handleTxEmpty(txWidget: LayoutSaltPayTxHistoryBinding) = with(txWidget) { + Timber.d("handleTxEmpty") groupShimmer.hide() groupError.hide() groupSuccess.hide() - root.beginDelayedTransition() - updateTxHistoryWidget(emptyList()) + // root.beginDelayedTransition() + updateTxHistoryWidget(txWidget, emptyList()) groupEmpty.show() } - private fun handleTxError() = with(txWidget) { + private fun handleTxError(txWidget: LayoutSaltPayTxHistoryBinding) = with(txWidget) { + Timber.d("handleTxError") groupShimmer.hide() groupEmpty.hide() groupSuccess.hide() - root.beginDelayedTransition() - updateTxHistoryWidget(emptyList()) + // root.beginDelayedTransition() + updateTxHistoryWidget(txWidget, emptyList()) groupError.show() } - private fun updateTxHistoryWidget(dataList: List) = with(txWidget) { + private fun updateTxHistoryWidget( + txWidget: LayoutSaltPayTxHistoryBinding, + dataList: List, + ) = with(txWidget) { (rvTxHistory.adapter as TxHistoryAdapter).submitList(dataList) } + + override fun onViewDestroy() { + Timber.d("onViewDestroy") + txWidget?.apply { + rvTxHistoryShimmer.removeItemDecoration(itemDecorator) + rvTxHistory.removeItemDecoration(itemDecorator) + } + super.onViewDestroy() + } +} + +private fun VeilLayout.changeVeilState(show: Boolean) { + if (show) this.veil() + else this.unVeil() } \ No newline at end of file diff --git a/app/src/main/res/layout/layout_salt_pay_balance.xml b/app/src/main/res/layout/layout_salt_pay_balance.xml index cdf2d00c6a..8ce65d0b6b 100644 --- a/app/src/main/res/layout/layout_salt_pay_balance.xml +++ b/app/src/main/res/layout/layout_salt_pay_balance.xml @@ -51,7 +51,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" - android:minWidth="152dp" android:textColor="@color/text_primary_1" android:textSize="24sp" android:textStyle="bold" @@ -96,7 +95,7 @@ android:text="@string/wallet_balance_blockchain_unreachable" android:textColor="@color/warning" android:textSize="12sp" - android:visibility="visible" + android:visibility="gone" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/veil_balance" /> diff --git a/buildSrc/src/main/java/Versions.kt b/buildSrc/src/main/java/Versions.kt index aa2ad57abd..73387ff687 100644 --- a/buildSrc/src/main/java/Versions.kt +++ b/buildSrc/src/main/java/Versions.kt @@ -59,7 +59,7 @@ object Versions { // endregion Other libraries // region Tangem - const val tangemBlockchainSdk = "develop-152-hotfix-4.0.1-4.0.2-161" + const val tangemBlockchainSdk = "develop-152-hotfix-4.0.1-4.0.2-162" // const val tangemBlockchainSdk = "0.0.1" const val tangemCardSdk = "develop-179-hotfix-4.0.1-189"