Updated on 2026-08-14

This commit is contained in:
Tangem 2025-04-01 13:10:07 +05:00
parent aea7f5f415
commit c83ce2ada3
53 changed files with 1062 additions and 238 deletions

View file

@ -42,7 +42,7 @@ internal class DefaultNFTRuntimeStore(
)
}
override fun getAsset(collectionId: NFTCollection.Identifier, assetId: NFTAsset.Identifier): Flow<NFTAsset> =
override fun getAsset(collectionId: NFTCollection.Identifier, assetId: NFTAsset.Identifier): Flow<NFTAsset?> =
collectionsRuntimeStore
.get()
.combine(getSalePrice(assetId)) { collectionsData, price ->
@ -50,13 +50,17 @@ internal class DefaultNFTRuntimeStore(
.getCollection(collectionId)
?.getAsset(assetId)
?.mergeWithPrice(price)
?: NFTAsset.Error(assetId)
}
override fun getSalePrice(assetId: NFTAsset.Identifier): Flow<NFTSalePrice> = pricesRuntimeStore
.get()
.map { it[assetId] ?: NFTSalePrice.Empty(assetId) }
override suspend fun getSalePriceSync(assetId: NFTAsset.Identifier): NFTSalePrice = pricesRuntimeStore
.getSyncOrNull()
?.let { it[assetId] }
?: NFTSalePrice.Empty(assetId)
override suspend fun saveCollections(collections: NFTCollections) {
collectionsRuntimeStore.store(collections)
}
@ -72,8 +76,13 @@ internal class DefaultNFTRuntimeStore(
?.collections
?.firstOrNull { it.id == collectionId }
private fun NFTCollection.getAsset(assetId: NFTAsset.Identifier): NFTAsset? =
assets.firstOrNull { it.id == assetId }
private fun NFTCollection.getAsset(assetId: NFTAsset.Identifier): NFTAsset? = when (val assets = assets) {
is NFTCollection.Assets.Empty,
is NFTCollection.Assets.Loading,
is NFTCollection.Assets.Failed,
-> null
is NFTCollection.Assets.Value -> assets.items.firstOrNull { it.id == assetId }
}
private fun NFTCollections.mergeWithPrices(prices: Map<NFTAsset.Identifier, NFTSalePrice>): NFTCollections =
when (val content = this.content) {
@ -89,18 +98,23 @@ internal class DefaultNFTRuntimeStore(
copy(
collections = this.collections?.map { data ->
data.copy(
assets = data.assets.map { asset ->
asset.mergeWithPrice(prices[asset.id] ?: NFTSalePrice.Empty(asset.id))
assets = when (val assets = data.assets) {
is NFTCollection.Assets.Empty,
is NFTCollection.Assets.Loading,
is NFTCollection.Assets.Failed,
-> assets
is NFTCollection.Assets.Value -> assets.copy(
items = assets.items.map { asset ->
asset.mergeWithPrice(prices[asset.id] ?: NFTSalePrice.Empty(asset.id))
},
)
},
)
},
source = this.source,
)
private fun NFTAsset.mergeWithPrice(price: NFTSalePrice): NFTAsset = when (this) {
is NFTAsset.Error -> this
is NFTAsset.Value -> copy(
salePrice = price,
)
}
private fun NFTAsset.mergeWithPrice(price: NFTSalePrice): NFTAsset = copy(
salePrice = price,
)
}

View file

@ -16,7 +16,9 @@ interface NFTRuntimeStore {
fun getAsset(collectionId: NFTCollection.Identifier, assetId: NFTAsset.Identifier): Flow<NFTAsset?>
fun getSalePrice(assetId: NFTAsset.Identifier): Flow<NFTSalePrice?>
fun getSalePrice(assetId: NFTAsset.Identifier): Flow<NFTSalePrice>
suspend fun getSalePriceSync(assetId: NFTAsset.Identifier): NFTSalePrice
suspend fun saveCollections(collections: NFTCollections)

View file

@ -12,7 +12,7 @@ object NFTSdkAssetConverter : Converter<Pair<Network, SdkNFTAsset>, NFTAsset> {
val (network, asset) = value
val assetId = NFTSdkAssetIdentifierConverter.convert(asset.identifier)
val collectionId = NFTSdkCollectionIdentifierConverter.convert(asset.collectionIdentifier)
return NFTAsset.Value(
return NFTAsset(
id = assetId,
collectionId = collectionId,
network = network,
@ -25,23 +25,22 @@ object NFTSdkAssetConverter : Converter<Pair<Network, SdkNFTAsset>, NFTAsset> {
assetId = assetId,
value = it.value,
symbol = it.symbol,
source = StatusSource.CACHE,
)
} ?: NFTSalePrice.Empty(assetId = assetId),
rarity = asset.rarity?.let {
NFTAsset.Value.Rarity(
NFTAsset.Rarity(
rank = it.rank,
label = it.label,
)
},
media = asset.media?.let {
NFTAsset.Value.Media(
NFTAsset.Media(
url = it.url,
mimetype = it.mimetype,
)
},
traits = asset.traits.map {
NFTAsset.Value.Trait(
NFTAsset.Trait(
name = it.name,
value = it.value,
)

View file

@ -1,5 +1,6 @@
package com.tangem.datasource.local.nft.converter
import com.tangem.domain.models.StatusSource
import com.tangem.domain.nft.models.NFTAsset
import com.tangem.domain.nft.models.NFTCollection
import com.tangem.domain.tokens.model.Network
@ -23,6 +24,16 @@ object NFTSdkCollectionConverter : Converter<Pair<Network, SdkNFTCollection>, NF
}
.filter {
it.id !is NFTAsset.Identifier.Unknown
}
.let {
if (it.isEmpty()) {
NFTCollection.Assets.Empty
} else {
NFTCollection.Assets.Value(
items = it,
source = StatusSource.CACHE,
)
}
},
)
}