diff --git a/data/nft/src/main/kotlin/com/tangem/data/nft/DefaultNFTRepository.kt b/data/nft/src/main/kotlin/com/tangem/data/nft/DefaultNFTRepository.kt index af884a8963..851849db2a 100644 --- a/data/nft/src/main/kotlin/com/tangem/data/nft/DefaultNFTRepository.kt +++ b/data/nft/src/main/kotlin/com/tangem/data/nft/DefaultNFTRepository.kt @@ -38,6 +38,8 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.* import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import timber.log.Timber import java.util.concurrent.ConcurrentHashMap @@ -63,6 +65,7 @@ internal class DefaultNFTRepository @Inject constructor( private val cryptoCurrencyFactory = CryptoCurrencyFactory(excludedBlockchains) private val nftRuntimeStores = ConcurrentHashMap() + private val nftRuntimeStoresMutex = Mutex() private val nftPersistenceStores = ConcurrentHashMap() private val collectionIdConverter = NFTSdkCollectionIdentifierConverter @@ -426,16 +429,17 @@ internal class DefaultNFTRepository @Inject constructor( private suspend fun getNFTRuntimeStore(userWalletId: UserWalletId, network: Network): NFTRuntimeStore { val storeId = (userWalletId to network).formatted() - return nftRuntimeStores.getOrPut(storeId) { - nftRuntimeStoreFactory.provide(network).also { - nftRuntimeStores[storeId] = it - val storedCollections = getStoredCollections(userWalletId, network) - val storedPrices = getStoredPrices(userWalletId, network) - it.initialize( - collections = storedCollections, - prices = storedPrices, - ) - } + return nftRuntimeStoresMutex.withLock { + nftRuntimeStores[storeId]?.let { return it } + val store = nftRuntimeStoreFactory.provide(network) + val storedCollections = getStoredCollections(userWalletId, network) + val storedPrices = getStoredPrices(userWalletId, network) + store.initialize( + collections = storedCollections, + prices = storedPrices, + ) + nftRuntimeStores[storeId] = store + store } } diff --git a/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetNFTCollectionsUseCase.kt b/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetNFTCollectionsUseCase.kt index db4c13cacc..95b2fd53cb 100644 --- a/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetNFTCollectionsUseCase.kt +++ b/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetNFTCollectionsUseCase.kt @@ -34,15 +34,15 @@ class GetNFTCollectionsUseCase( @OptIn(ExperimentalCoroutinesApi::class) fun invokeForAccounts(userWalletId: UserWalletId): Flow { - fun Account.flowOfNFTCollections(): Flow>> { + fun Account.flowOfNFTCollections(): Flow>>? { val currencies = (this as? Account.CryptoPortfolio)?.cryptoCurrencies.orEmpty() - + if (currencies.isEmpty()) return null return nftCollections(userWalletId = userWalletId, cryptoCurrencies = currencies.toList()) .map { nfts -> this to nfts } } return singleAccountListSupplier(userWalletId) - .mapLatest { statusList -> statusList.accounts.map { it.flowOfNFTCollections() } } + .mapLatest { statusList -> statusList.accounts.mapNotNull { it.flowOfNFTCollections() } } .flatMapLatest { flows -> combine(flows) { WalletNFTCollections(it.toMap()) } } } @@ -53,6 +53,7 @@ class GetNFTCollectionsUseCase( val networks = cryptoCurrencies .map { cryptoCurrency -> cryptoCurrency.network } .distinct() + if (networks.isEmpty()) return flowOf(emptyList()) return nftRepository.observeCollections(userWalletId, networks) } } \ No newline at end of file diff --git a/features/nft/impl/src/main/kotlin/com/tangem/features/nft/collections/entity/transformer/UpdateDataStateTransformer.kt b/features/nft/impl/src/main/kotlin/com/tangem/features/nft/collections/entity/transformer/UpdateDataStateTransformer.kt index 431de33406..0e4605da31 100644 --- a/features/nft/impl/src/main/kotlin/com/tangem/features/nft/collections/entity/transformer/UpdateDataStateTransformer.kt +++ b/features/nft/impl/src/main/kotlin/com/tangem/features/nft/collections/entity/transformer/UpdateDataStateTransformer.kt @@ -84,7 +84,7 @@ internal class UpdateDataStateTransformer( if (isAccountMode) { val result = mutableListOf() walletNFTCollections.collections.forEach { (account, nfts) -> - if (nfts.isEmpty()) return@forEach + if (nfts.hasNoContentCollections()) return@forEach result.add(account.toAccountPortfolioUM()) result.addAll(createNFTsUM(nfts)) } @@ -105,6 +105,12 @@ internal class UpdateDataStateTransformer( ), ) + private fun List.hasNoContentCollections() = this + .all { it.contentCollections().isEmpty() } + + private fun NFTCollections.contentCollections() = (this.content as? NFTCollections.Content.Collections) + ?.collections.orEmpty() + private fun NFTCollectionsStateUM.createNFTsUM(nftCollections: List): Sequence = nftCollections .map { it.content }