From 55fcdc6e8306320853bffce0d7d6680cb3834bdc Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 4 Oct 2023 22:37:13 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../ui/appsettings/AppSettingsViewModel.kt | 2 ++ .../data/common/cache/DefaultCacheRegistry.kt | 30 +++++++++++-------- .../repository/DefaultNetworksRepository.kt | 4 ++- .../repository/DefaultQuotesRepository.kt | 8 +++-- .../wallet/viewmodels/WalletViewModel.kt | 2 +- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt index aaaae0a8d1..4876447e4c 100644 --- a/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt +++ b/app/src/main/java/com/tangem/tap/features/details/ui/appsettings/AppSettingsViewModel.kt @@ -196,6 +196,8 @@ internal class AppSettingsViewModel( appCurrencyRepository .getSelectedAppCurrency() .onEach { + if (it.code == store.state.globalState.appCurrency.code) return@onEach + val fiatCurrency = with(it) { FiatCurrency(code, name, symbol) } store.dispatchWithMain(DetailsAction.AppSettings.ChangeAppCurrency(fiatCurrency)) } diff --git a/data/common/src/main/kotlin/com/tangem/data/common/cache/DefaultCacheRegistry.kt b/data/common/src/main/kotlin/com/tangem/data/common/cache/DefaultCacheRegistry.kt index 57f6e0e76f..6d6f19cd0c 100644 --- a/data/common/src/main/kotlin/com/tangem/data/common/cache/DefaultCacheRegistry.kt +++ b/data/common/src/main/kotlin/com/tangem/data/common/cache/DefaultCacheRegistry.kt @@ -2,6 +2,8 @@ package com.tangem.data.common.cache import com.tangem.datasource.local.cache.CacheKeysStore import com.tangem.datasource.local.cache.model.CacheKey +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.withContext import org.joda.time.Duration import org.joda.time.LocalDateTime import timber.log.Timber @@ -20,17 +22,17 @@ internal class DefaultCacheRegistry( override suspend fun invalidate(key: String) { Timber.d("Invalidate the cache key: $key") - cacheKeysStore.remove(key) + withContext(NonCancellable) { cacheKeysStore.remove(key) } } override suspend fun invalidate(keys: Collection) { Timber.d("Invalidate cache keys: $keys") - cacheKeysStore.remove(keys) + withContext(NonCancellable) { cacheKeysStore.remove(keys) } } override suspend fun invalidateAll() { Timber.d("Invalidate all cache keys") - cacheKeysStore.clear() + withContext(NonCancellable) { cacheKeysStore.clear() } } override suspend fun invokeOnExpire( @@ -44,18 +46,22 @@ internal class DefaultCacheRegistry( try { Timber.d("Invoke the action associated with the cache key: $key") + + cacheKeysStore.store( + key = CacheKey( + id = key, + updatedAt = LocalDateTime.now(), + expiresIn = expireIn, + ), + ) + block() } catch (e: Throwable) { - Timber.w(e, "The action related to the cache key has failed: $key") + Timber.e(e, "The action related to the cache key has failed: $key") + + invalidate(key) + throw e } - - cacheKeysStore.store( - key = CacheKey( - id = key, - updatedAt = LocalDateTime.now(), - expiresIn = expireIn, - ), - ) } } \ No newline at end of file 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 e1d3826183..7a8a4e726f 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 @@ -97,7 +97,9 @@ internal class DefaultNetworksRepository( extraTokens = currencies.filterIsInstance().toSet(), ) - invalidateCacheKeyIfNeeded(userWalletId, network, result) + withContext(NonCancellable) { + invalidateCacheKeyIfNeeded(userWalletId, network, result) + } val networkStatus = networkStatusFactory.createNetworkStatus( network = network, diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultQuotesRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultQuotesRepository.kt index e63ea16071..e557875dc8 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultQuotesRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultQuotesRepository.kt @@ -35,9 +35,11 @@ internal class DefaultQuotesRepository( } withContext(dispatchers.io) { - selectedAppCurrencyStore.get().collectLatest { appCurrency -> - fetchExpiredQuotes(currenciesIds, appCurrency.id, refresh = false) - } + selectedAppCurrencyStore.get() + .distinctUntilChanged() + .collectLatest { appCurrency -> + fetchExpiredQuotes(currenciesIds, appCurrency.id, refresh = false) + } } }.cancellable() diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt index 3944531b1c..09b9aff20b 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt @@ -298,7 +298,7 @@ internal class WalletViewModel @Inject constructor( update = { it.copy(scanResponse = scannedCardResponse) }, ) .onRight { - fetchTokenListUseCase(userWalletId = it.walletId, refresh = true) + fetchTokenListUseCase(userWalletId = it.walletId) } } }