diff --git a/app/src/main/java/com/tangem/tap/di/domain/NFTDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/NFTDomainModule.kt index def43853a4..26161f3d76 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/NFTDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/NFTDomainModule.kt @@ -6,6 +6,7 @@ import com.tangem.domain.nft.repository.NFTRepository import com.tangem.domain.quotes.single.SingleQuoteFetcher import com.tangem.domain.quotes.single.SingleQuoteSupplier import com.tangem.domain.tokens.repository.CurrenciesRepository +import com.tangem.domain.wallets.repository.WalletsRepository import com.tangem.utils.coroutines.CoroutineDispatcherProvider import dagger.Module import dagger.Provides @@ -104,4 +105,26 @@ internal object NFTDomainModule { ): FetchNFTPriceUseCase { return FetchNFTPriceUseCase(nftRepository, singleQuoteFetcher) } + + @Provides + @Singleton + fun provideEnableWalletNFTUseCase(walletsRepository: WalletsRepository): EnableWalletNFTUseCase { + return EnableWalletNFTUseCase(walletsRepository) + } + + @Provides + @Singleton + fun provideDisableWalletNFTUseCase( + walletsRepository: WalletsRepository, + nftRepository: NFTRepository, + currenciesRepository: CurrenciesRepository, + ): DisableWalletNFTUseCase { + return DisableWalletNFTUseCase(walletsRepository, nftRepository, currenciesRepository) + } + + @Provides + @Singleton + fun provideGetWalletNFTEnabledUseCase(walletsRepository: WalletsRepository): GetWalletNFTEnabledUseCase { + return GetWalletNFTEnabledUseCase(walletsRepository) + } } \ No newline at end of file diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTPersistenceStore.kt b/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTPersistenceStore.kt index 51b17b2bbf..3018b1b4be 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTPersistenceStore.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTPersistenceStore.kt @@ -47,5 +47,10 @@ internal class DefaultNFTPersistenceStore( } } + override suspend fun clear() { + collectionsPersistenceStore.updateData { emptyList() } + pricesPersistenceStore.updateData { emptyList() } + } + private fun NFTCollection.getAsset(assetId: NFTAsset.Identifier) = assets.firstOrNull { it.identifier == assetId } } \ No newline at end of file diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTRuntimeStore.kt b/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTRuntimeStore.kt index cd70259814..0b615f36ae 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTRuntimeStore.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/local/nft/DefaultNFTRuntimeStore.kt @@ -71,6 +71,16 @@ internal class DefaultNFTRuntimeStore( } } + override suspend fun clear() { + collectionsRuntimeStore.store( + NFTCollections( + network = network, + content = NFTCollections.Content.Collections(null, StatusSource.ONLY_CACHE), + ), + ) + pricesRuntimeStore.store(emptyMap()) + } + private fun NFTCollections.getCollection(collectionId: NFTCollection.Identifier): NFTCollection? = (content as? NFTCollections.Content.Collections) ?.collections diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTPersistenceStore.kt b/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTPersistenceStore.kt index 3a6b27d9f3..feee48db2d 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTPersistenceStore.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTPersistenceStore.kt @@ -18,4 +18,6 @@ interface NFTPersistenceStore { suspend fun saveCollections(collections: List) suspend fun saveSalePrice(assetId: NFTAsset.Identifier, salePrice: NFTAsset.SalePrice) + + suspend fun clear() } \ No newline at end of file diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTRuntimeStore.kt b/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTRuntimeStore.kt index 8c211d1f8e..92b2292e76 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTRuntimeStore.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/local/nft/NFTRuntimeStore.kt @@ -23,4 +23,6 @@ interface NFTRuntimeStore { suspend fun saveCollections(collections: NFTCollections) suspend fun saveSalePrice(salePrice: NFTSalePrice) + + suspend fun clear() } \ No newline at end of file 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 d5d3a63d50..d32cf88b34 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 @@ -215,6 +215,13 @@ internal class DefaultNFTRepository @Inject constructor( assetIdentifier = assetIdConverter.convertBack(assetIdentifier), ) + override suspend fun clearCache(userWalletId: UserWalletId, networks: List) { + networks.forEach { + getNFTPersistenceStore(userWalletId, it).clear() + getNFTRuntimeStore(userWalletId, it).clear() + } + } + private suspend fun refreshCollectionsInternal( userWalletId: UserWalletId, networks: List, diff --git a/domain/nft/build.gradle.kts b/domain/nft/build.gradle.kts index 6151f94e1b..6752c4736d 100644 --- a/domain/nft/build.gradle.kts +++ b/domain/nft/build.gradle.kts @@ -23,6 +23,7 @@ dependencies { implementation(projects.domain.quotes) implementation(projects.domain.tokens) implementation(projects.domain.tokens.models) + implementation(projects.domain.wallets) implementation(projects.domain.wallets.models) // endregion diff --git a/domain/nft/src/main/kotlin/com/tangem/domain/nft/DisableWalletNFTUseCase.kt b/domain/nft/src/main/kotlin/com/tangem/domain/nft/DisableWalletNFTUseCase.kt new file mode 100644 index 0000000000..75afd3e094 --- /dev/null +++ b/domain/nft/src/main/kotlin/com/tangem/domain/nft/DisableWalletNFTUseCase.kt @@ -0,0 +1,21 @@ +package com.tangem.domain.nft + +import com.tangem.domain.nft.repository.NFTRepository +import com.tangem.domain.tokens.repository.CurrenciesRepository +import com.tangem.domain.wallets.models.UserWalletId +import com.tangem.domain.wallets.repository.WalletsRepository + +class DisableWalletNFTUseCase( + private val walletsRepository: WalletsRepository, + private val nftRepository: NFTRepository, + private val currenciesRepository: CurrenciesRepository, +) { + + suspend operator fun invoke(userWalletId: UserWalletId) { + walletsRepository.disableNFT(userWalletId) + + val currencies = currenciesRepository.getMultiCurrencyWalletCachedCurrenciesSync(userWalletId) + val networks = currencies.map { it.network } + nftRepository.clearCache(userWalletId, networks) + } +} \ No newline at end of file diff --git a/domain/nft/src/main/kotlin/com/tangem/domain/nft/EnableWalletNFTUseCase.kt b/domain/nft/src/main/kotlin/com/tangem/domain/nft/EnableWalletNFTUseCase.kt new file mode 100644 index 0000000000..4df3fa1c23 --- /dev/null +++ b/domain/nft/src/main/kotlin/com/tangem/domain/nft/EnableWalletNFTUseCase.kt @@ -0,0 +1,13 @@ +package com.tangem.domain.nft + +import com.tangem.domain.wallets.models.UserWalletId +import com.tangem.domain.wallets.repository.WalletsRepository + +class EnableWalletNFTUseCase( + private val walletsRepository: WalletsRepository, +) { + + suspend operator fun invoke(userWalletId: UserWalletId) { + walletsRepository.enableNFT(userWalletId) + } +} \ No newline at end of file diff --git a/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetWalletNFTEnabledUseCase.kt b/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetWalletNFTEnabledUseCase.kt new file mode 100644 index 0000000000..db97202c30 --- /dev/null +++ b/domain/nft/src/main/kotlin/com/tangem/domain/nft/GetWalletNFTEnabledUseCase.kt @@ -0,0 +1,13 @@ +package com.tangem.domain.nft + +import com.tangem.domain.wallets.models.UserWalletId +import com.tangem.domain.wallets.repository.WalletsRepository +import kotlinx.coroutines.flow.Flow + +class GetWalletNFTEnabledUseCase( + private val walletsRepository: WalletsRepository, +) { + + operator fun invoke(userWalletId: UserWalletId): Flow = walletsRepository + .nftEnabledStatus(userWalletId) +} \ No newline at end of file diff --git a/domain/nft/src/main/kotlin/com/tangem/domain/nft/repository/NFTRepository.kt b/domain/nft/src/main/kotlin/com/tangem/domain/nft/repository/NFTRepository.kt index 5f8817884c..fd6548f6b8 100644 --- a/domain/nft/src/main/kotlin/com/tangem/domain/nft/repository/NFTRepository.kt +++ b/domain/nft/src/main/kotlin/com/tangem/domain/nft/repository/NFTRepository.kt @@ -32,4 +32,6 @@ interface NFTRepository { suspend fun getNFTSupportedNetworks(userWalletId: UserWalletId): List suspend fun getNFTExploreUrl(network: Network, assetIdentifier: NFTAsset.Identifier): String? + + suspend fun clearCache(userWalletId: UserWalletId, networks: List) } \ No newline at end of file diff --git a/features/wallet-settings/impl/build.gradle.kts b/features/wallet-settings/impl/build.gradle.kts index 25dc7dd5c6..9b694a9073 100644 --- a/features/wallet-settings/impl/build.gradle.kts +++ b/features/wallet-settings/impl/build.gradle.kts @@ -34,6 +34,7 @@ dependencies { implementation(projects.domain.wallets) implementation(projects.domain.wallets.models) implementation(projects.domain.demo) + implementation(projects.domain.nft) /* AndroidX */ implementation(deps.androidx.fragment.ktx) diff --git a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt index b22130a159..7e895820be 100644 --- a/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt +++ b/features/wallet-settings/impl/src/main/kotlin/com/tangem/feature/walletsettings/model/WalletSettingsModel.kt @@ -19,8 +19,10 @@ import com.tangem.domain.common.util.cardTypesResolver import com.tangem.domain.demo.IsDemoCardUseCase import com.tangem.domain.models.scan.CardDTO import com.tangem.domain.models.scan.ScanResponse +import com.tangem.domain.nft.DisableWalletNFTUseCase +import com.tangem.domain.nft.EnableWalletNFTUseCase +import com.tangem.domain.nft.GetWalletNFTEnabledUseCase import com.tangem.domain.wallets.models.UserWallet -import com.tangem.domain.wallets.repository.WalletsRepository import com.tangem.domain.wallets.usecase.DeleteWalletUseCase import com.tangem.domain.wallets.usecase.GetUserWalletUseCase import com.tangem.domain.wallets.usecase.ShouldSaveUserWalletsSyncUseCase @@ -54,8 +56,10 @@ internal class WalletSettingsModel @Inject constructor( private val analyticsContextProxy: AnalyticsContextProxy, private val getShouldSaveUserWalletsSyncUseCase: ShouldSaveUserWalletsSyncUseCase, private val isDemoCardUseCase: IsDemoCardUseCase, - private val walletsRepository: WalletsRepository, private val nftFeatureToggles: NFTFeatureToggles, + private val getWalletNFTEnabledUseCase: GetWalletNFTEnabledUseCase, + private val enableWalletNFTUseCase: EnableWalletNFTUseCase, + private val disableWalletNFTUseCase: DisableWalletNFTUseCase, ) : Model() { val params: WalletSettingsComponent.Params = paramsContainer.require() @@ -71,7 +75,7 @@ internal class WalletSettingsModel @Inject constructor( init { getWalletUseCase.invokeFlow(params.userWalletId) .distinctUntilChanged() - .combine(walletsRepository.nftEnabledStatus(params.userWalletId)) { maybeWallet, nftEnabled -> + .combine(getWalletNFTEnabledUseCase.invoke(params.userWalletId)) { maybeWallet, nftEnabled -> val wallet = maybeWallet.getOrNull() ?: return@combine val isRenameWalletAvailable = getShouldSaveUserWalletsSyncUseCase() state.update { value -> @@ -169,9 +173,9 @@ internal class WalletSettingsModel @Inject constructor( private fun onCheckedNFTChange(isChecked: Boolean) { modelScope.launch { if (isChecked) { - walletsRepository.enableNFT(params.userWalletId) + enableWalletNFTUseCase.invoke(params.userWalletId) } else { - walletsRepository.disableNFT(params.userWalletId) + disableWalletNFTUseCase.invoke(params.userWalletId) } } }