From 1b4df199307d424a51e7643c72562890b8b978e3 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 21 May 2024 18:58:30 +0500 Subject: [PATCH] Updated on 2026-08-14 --- .../repository/DefaultNetworksRepository.kt | 25 +++++++++++-- .../tokens/GetNetworkAddressesUseCase.kt | 19 ++-------- .../tokens/repository/NetworksRepository.kt | 2 + .../SendRecipientWalletListConverter.kt | 31 +++++++++------- .../presentation/viewmodel/SendViewModel.kt | 37 ++++++++----------- 5 files changed, 61 insertions(+), 53 deletions(-) diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt index f25a65291c..57f55c1aab 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt @@ -1,6 +1,7 @@ package com.tangem.data.tokens.repository import com.tangem.blockchain.common.Blockchain +import com.tangem.blockchain.common.address.AddressType import com.tangem.data.common.cache.CacheRegistry import com.tangem.data.tokens.utils.CardCryptoCurrenciesFactory import com.tangem.data.tokens.utils.NetworkStatusFactory @@ -77,6 +78,21 @@ internal class DefaultNetworksRepository( return blockchain == Blockchain.Aptos } + override suspend fun getNetworkAddresses(userWalletId: UserWalletId, network: Network): List { + // Get list of currencies matching [network] + val currencies = getCurrencies(userWalletId) + .filter { currency -> network.id == currency.network.id } + + // There is no currencies matching given [networks] in [userWalletId] + if (currencies.toList().isEmpty()) return emptyList() + + return currencies.toList().map { currency -> + walletManagersFacade.getAddresses(userWalletId, currency.network) + .firstOrNull { it.type == AddressType.Default } + ?.value.orEmpty() + } + } + private suspend fun fetchNetworksStatusesIfCacheExpired( userWalletId: UserWalletId, networks: Set, @@ -174,11 +190,16 @@ internal class DefaultNetworksRepository( } private suspend fun getCurrencies(userWalletId: UserWalletId, networks: Set): Sequence { + val currencies = getCurrencies(userWalletId) + return currencies.filter { networks.contains(it.network) } + } + + private suspend fun getCurrencies(userWalletId: UserWalletId): Sequence { val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { "Unable to find user wallet with provided ID: $userWalletId" } - val currencies = if (userWallet.isMultiCurrency) { + return if (userWallet.isMultiCurrency) { val response = requireNotNull(userTokensStore.getSyncOrNull(userWalletId)) { "Unable to find tokens response for user wallet with provided ID: $userWalletId" } @@ -194,8 +215,6 @@ internal class DefaultNetworksRepository( sequenceOf(currency) } } - - return currencies.filter { networks.contains(it.network) } } private suspend fun invalidateCacheKeyIfNeeded( diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetNetworkAddressesUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetNetworkAddressesUseCase.kt index aea1cad8eb..0fa37a5a07 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetNetworkAddressesUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetNetworkAddressesUseCase.kt @@ -1,27 +1,14 @@ package com.tangem.domain.tokens import com.tangem.domain.tokens.model.Network -import com.tangem.domain.tokens.model.NetworkStatus import com.tangem.domain.tokens.repository.NetworksRepository import com.tangem.domain.wallets.models.UserWalletId -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map class GetNetworkAddressesUseCase( internal val networksRepository: NetworksRepository, ) { - operator fun invoke(userWalletId: UserWalletId, network: Network): Flow> = - networksRepository.getNetworkStatusesUpdates(userWalletId, setOf(network)) - .map { networkStatuses -> - networkStatuses.filter { it.network.id == network.id } - .map { networkStatus -> - when (val status = networkStatus.value) { - is NetworkStatus.NoAccount -> status.address.defaultAddress.value - is NetworkStatus.Unreachable -> status.address?.defaultAddress?.value.orEmpty() - is NetworkStatus.Verified -> status.address.defaultAddress.value - else -> "" - } - } - } + suspend fun invokeSync(userWalletId: UserWalletId, network: Network): List { + return networksRepository.getNetworkAddresses(userWalletId, network) + } } \ No newline at end of file diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/NetworksRepository.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/NetworksRepository.kt index 6ec3fbb11a..ebece1356e 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/NetworksRepository.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/NetworksRepository.kt @@ -45,4 +45,6 @@ interface NetworksRepository { ): Set fun isNeedToCreateAccountWithoutReserve(network: Network): Boolean + + suspend fun getNetworkAddresses(userWalletId: UserWalletId, network: Network): List } \ No newline at end of file diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientWalletListConverter.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientWalletListConverter.kt index dae1452696..35d56f9846 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientWalletListConverter.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientWalletListConverter.kt @@ -9,7 +9,6 @@ import com.tangem.features.send.impl.presentation.state.recipient.utils.emptyLis import com.tangem.utils.converter.Converter import kotlinx.collections.immutable.PersistentList import kotlinx.collections.immutable.toPersistentList -import kotlinx.coroutines.flow.filter internal class SendRecipientWalletListConverter : Converter, PersistentList> { @@ -24,19 +23,25 @@ internal class SendRecipientWalletListConverter : return this.filterNotNull() .filter { it.address.isNotBlank() } .groupBy { item -> item.name } - .values.map { - it.mapIndexed { index, item -> - val name = if (it.size > 1) { - "${item.name} ${index.inc()}" - } else { - item.name + .values.map { wallets -> + val groupedByWallet = wallets.groupBy { it.userWalletId } + var i = 0 + groupedByWallet + .flatMap { item -> + item.value.map { wallet -> + val name = if (groupedByWallet.size > 1) { + "${wallet.name} ${++i}" + } else { + wallet.name + } + + SendRecipientListContent( + id = "${WALLET_KEY_TAG}${walletsCounter++}", + title = TextReference.Str(wallet.address), + subtitle = TextReference.Str(name), + ) + } } - SendRecipientListContent( - id = "${WALLET_KEY_TAG}${walletsCounter++}", - title = TextReference.Str(item.address), - subtitle = TextReference.Str(name), - ) - } } .flatten() .toPersistentList() diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt index 921c3124e0..65d93773c7 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/viewmodel/SendViewModel.kt @@ -4,6 +4,7 @@ import android.os.SystemClock import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue +import androidx.compose.ui.util.fastDistinctBy import androidx.lifecycle.* import arrow.core.Either import arrow.core.getOrElse @@ -271,6 +272,7 @@ internal class SendViewModel @Inject constructor( .saveIn(balanceHidingJobHolder) } + // TODO [REDACTED_JIRA] private fun getCurrenciesStatusUpdates(isSingleWalletWithToken: Boolean, isMultiCurrency: Boolean) { if (cryptoCurrency is CryptoCurrency.Coin) { getCurrencyStatusUpdates( @@ -405,43 +407,36 @@ internal class SendViewModel @Inject constructor( ?.toAvailableWallets() .orEmpty() }.onSuccess { result -> - combine(*result.toTypedArray()) { it } - .onEach { wallets -> - userWallets = wallets.flatMap { it }.toList() - uiState = stateFactory.onLoadedWalletsList(wallets = userWallets) - } - .flowOn(dispatchers.main) - .launchIn(viewModelScope) + userWallets = result + uiState = stateFactory.onLoadedWalletsList(wallets = userWallets) }.onFailure { uiState = stateFactory.onLoadedWalletsList(wallets = emptyList()) } } } - private suspend fun List.toAvailableWallets(): List>> = + private suspend fun List.toAvailableWallets(): List = filterNot { it.walletId == userWalletId || it.isLocked } .mapNotNull { wallet -> - val status = if (!wallet.isMultiCurrency) { + val addresses = if (!wallet.isMultiCurrency) { getCryptoCurrencyUseCase(wallet.walletId).getOrNull()?.let { if (it.network.id == cryptoCurrency.network.id) { - getNetworkAddressesUseCase(wallet.walletId, it.network) + getNetworkAddressesUseCase.invokeSync(wallet.walletId, it.network) } else { null } } } else { - getNetworkAddressesUseCase(wallet.walletId, cryptoCurrency.network) + getNetworkAddressesUseCase.invokeSync(wallet.walletId, cryptoCurrency.network) } - status?.map { addresses -> - addresses.map { address -> - AvailableWallet( - name = wallet.name, - address = address, - userWalletId = wallet.walletId, - ) - } - } - } + addresses?.map { address -> + AvailableWallet( + name = wallet.name, + address = address, + userWalletId = wallet.walletId, + ) + }?.fastDistinctBy { it.address } + }.flatten() private suspend fun getTxHistory() { val txHistoryList = getFixedTxHistoryItemsUseCase.getSync(