diff --git a/domain/core/src/main/kotlin/com/tangem/domain/core/lce/Lce.kt b/domain/core/src/main/kotlin/com/tangem/domain/core/lce/Lce.kt index 2d6902a58a..2fef58e761 100644 --- a/domain/core/src/main/kotlin/com/tangem/domain/core/lce/Lce.kt +++ b/domain/core/src/main/kotlin/com/tangem/domain/core/lce/Lce.kt @@ -146,4 +146,10 @@ sealed class Lce { ifContent = { predicate(it) }, ifError = { false }, ) + + fun onError(action: (error: E) -> Unit): Lce { + if (this is Error) action(this.error) + + return this + } } \ No newline at end of file diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CachedCurrenciesStatusesOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CachedCurrenciesStatusesOperations.kt index cfb27d170e..d4c4fcb9d9 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CachedCurrenciesStatusesOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CachedCurrenciesStatusesOperations.kt @@ -73,8 +73,6 @@ class CachedCurrenciesStatusesOperations( ): LceFlow> = lceFlow { val prevStatuses = MutableStateFlow(value = emptyList()) - val isUpdating = MutableStateFlow(value = true) - val nonEmptyCurrencies = currenciesFlow.mapNotNull { it.getOrNull() }.firstOrNull()?.toNonEmptyListOrNull() if (!isFetchingStarted(userWalletId) && nonEmptyCurrencies != null) { @@ -84,7 +82,7 @@ class CachedCurrenciesStatusesOperations( val (networks, currenciesIds) = getIds(nonEmptyCurrencies) fetchComponents(userWalletId, networks, currenciesIds, nonEmptyCurrencies) } - .invokeOnCompletion { isUpdating.value = false } + .invokeOnCompletion { setFetchFinished(userWalletId) } } currenciesFlow.flatMapLatest { maybeCurrencies -> @@ -143,14 +141,18 @@ class CachedCurrenciesStatusesOperations( fetchComponents(userWalletId, networks, currenciesIds, currencies) } - .invokeOnCompletion { isUpdating.value = false } + .invokeOnCompletion { setFetchFinished(userWalletId) } } combine( flow = getQuotes(currenciesIds), flow2 = getNetworksStatuses(userWalletId, networks), flow3 = getYieldBalances(userWalletId, currencies), - flow4 = isUpdating, + flow4 = fetchingState.map { + val state = it[userWalletId] ?: return@map false + + !state.isFinished() + }, transform = ::createCurrenciesStatuses, ) .distinctUntilChanged() @@ -419,20 +421,35 @@ class CachedCurrenciesStatusesOperations( } private fun isFetchingStarted(userWalletId: UserWalletId): Boolean { - return fetchingState.value[userWalletId] ?: false + return fetchingState.value[userWalletId]?.let { it.isStarted() || it.isFinished() } ?: false } private fun setFetchStarted(userWalletId: UserWalletId) { fetchingState.update { it.toMutableMap().apply { - put(key = userWalletId, value = true) + put(key = userWalletId, value = FetchingState.STARTED) } } } + private fun setFetchFinished(userWalletId: UserWalletId) { + fetchingState.update { + it.toMutableMap().apply { + put(key = userWalletId, value = FetchingState.FINISHED) + } + } + } + + enum class FetchingState { + STARTED, FINISHED; + + fun isStarted() = this == STARTED + fun isFinished() = this == FINISHED + } + companion object { internal const val RETRY_DELAY = 2000L - private val fetchingState = MutableStateFlow(value = emptyMap()) + private val fetchingState = MutableStateFlow(value = emptyMap()) } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/OrganizeTokensComponent.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/OrganizeTokensComponent.kt index 2763610e1f..495ea6f0ca 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/OrganizeTokensComponent.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/OrganizeTokensComponent.kt @@ -32,8 +32,6 @@ internal class OrganizeTokensComponent( override fun Content(modifier: Modifier) { val uiState by model.uiState.collectAsStateWithLifecycle() - OrganizeTokensScreen( - state = uiState, - ) + OrganizeTokensScreen(state = uiState) } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/model/OrganizeTokensModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/model/OrganizeTokensModel.kt index 9d064cf565..0cc0cdffde 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/model/OrganizeTokensModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/organizetokens/model/OrganizeTokensModel.kt @@ -10,11 +10,12 @@ import com.tangem.core.decompose.model.ParamsContainer import com.tangem.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.balancehiding.GetBalanceHidingSettingsUseCase -import com.tangem.domain.core.utils.getOrElse +import com.tangem.domain.core.lce.Lce import com.tangem.domain.tokens.ApplyTokenListSortingUseCase import com.tangem.domain.tokens.GetTokenListUseCase import com.tangem.domain.tokens.ToggleTokenListGroupingUseCase import com.tangem.domain.tokens.ToggleTokenListSortingUseCase +import com.tangem.domain.tokens.error.TokenListError import com.tangem.domain.tokens.model.TokenList import com.tangem.feature.wallet.child.organizetokens.OrganizeTokensComponent import com.tangem.feature.wallet.presentation.organizetokens.OrganizeTokensIntents @@ -36,13 +37,13 @@ import javax.inject.Inject @ModelScoped internal class OrganizeTokensModel @Inject constructor( paramsContainer: ParamsContainer, + getBalanceHidingSettingsUseCase: GetBalanceHidingSettingsUseCase, override val dispatchers: CoroutineDispatcherProvider, private val getTokenListUseCase: GetTokenListUseCase, private val toggleTokenListGroupingUseCase: ToggleTokenListGroupingUseCase, private val toggleTokenListSortingUseCase: ToggleTokenListSortingUseCase, private val applyTokenListSortingUseCase: ApplyTokenListSortingUseCase, private val getSelectedAppCurrencyUseCase: GetSelectedAppCurrencyUseCase, - private val getBalanceHidingSettingsUseCase: GetBalanceHidingSettingsUseCase, private val analyticsEventsHandler: AnalyticsEventHandler, ) : Model(), OrganizeTokensIntents { @@ -167,21 +168,14 @@ internal class OrganizeTokensModel @Inject constructor( } private suspend fun getTokenList(): TokenList? { - val tokenList = getTokenListUseCase.launch(userWalletId) - .transform { maybeTokenList -> - val tokenList = maybeTokenList.getOrElse( - ifLoading = { return@transform }, - ifError = { error -> - stateHolder.updateStateWithError(error) + val maybeTokenList = getTokenListUseCase.launch(userWalletId) + .filterNot(Lce::isLoading) + .firstOrNull() + ?: return null - return@transform - }, - ) - - emit(tokenList) - } - - return tokenList.firstOrNull() + return maybeTokenList + .onError(stateHolder::updateStateWithError) + .getOrNull(isPartialContentAccepted = false) } private fun bootstrapDragAndDropUpdates() { diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt index 6fe75e5285..5ca74067c5 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/GetMultiWalletWarningsFactory.kt @@ -16,8 +16,8 @@ import com.tangem.domain.wallets.models.SeedPhraseNotificationsStatus import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.usecase.IsNeedToBackupUseCase import com.tangem.domain.wallets.usecase.SeedPhraseNotificationUseCase -import com.tangem.feature.wallet.presentation.wallet.state.model.WalletNotification import com.tangem.feature.wallet.child.wallet.model.intents.WalletClickIntents +import com.tangem.feature.wallet.presentation.wallet.state.model.WalletNotification import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.flow.Flow @@ -179,7 +179,7 @@ internal class GetMultiWalletWarningsFactory @Inject constructor( } private fun Lce.getMissingAddressCurrencies(): List { - val tokenList = getOrNull(isPartialContentAccepted = false) ?: return emptyList() + val tokenList = getOrNull(isPartialContentAccepted = true) ?: return emptyList() return tokenList .flattenCurrencies()