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 059501fd80..cd0a3639c9 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 @@ -38,16 +38,29 @@ class CachedCurrenciesStatusesOperations( ) } + @OptIn(ExperimentalCoroutinesApi::class) private fun transformToCurrenciesStatuses( userWalletId: UserWalletId, currenciesFlow: EitherFlow>, ): LceFlow> = lceFlow { - currenciesFlow.collectLatest { maybeCurrencies -> + currenciesFlow.flatMapLatest { maybeCurrencies -> val isUpdating = MutableStateFlow(value = true) val nonEmptyCurrencies = maybeCurrencies.bind().toNonEmptyListOrNull() ensureNotNull(nonEmptyCurrencies) { TokenListError.EmptyTokens } + // This is only 'true' when the flow here is empty, such as during initial loading + if (isLoading.get()) { + val loadingCurrencies = createCurrenciesStatuses( + currencies = nonEmptyCurrencies, + maybeNetworkStatuses = null, + maybeQuotes = null, + maybeYieldBalances = null, + isUpdating = true, + ) + send(loadingCurrencies) + } + val (networks, currenciesIds) = getIds(nonEmptyCurrencies) fun createCurrenciesStatuses( @@ -63,6 +76,9 @@ class CachedCurrenciesStatusesOperations( isUpdating = isUpdating, ) + launch { fetchComponents(userWalletId, networks, currenciesIds, nonEmptyCurrencies) } + .invokeOnCompletion { isUpdating.value = false } + combine( flow = getQuotes(currenciesIds), flow2 = getNetworksStatuses(userWalletId, networks), @@ -71,17 +87,9 @@ class CachedCurrenciesStatusesOperations( transform = ::createCurrenciesStatuses, ) .distinctUntilChanged() - .onEach { maybeCurrenciesStatuses -> - send(maybeCurrenciesStatuses) - } - .launchIn(scope = this) - - launch { - fetchComponents(userWalletId, networks, currenciesIds, nonEmptyCurrencies) - }.invokeOnCompletion { - isUpdating.value = false - } } + .onEach(::send) + .launchIn(scope = this) } private suspend fun Raise.fetchComponents( diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/LceCurrenciesStatusesOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/LceCurrenciesStatusesOperations.kt index 2ad13c510a..255d2f86b5 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/LceCurrenciesStatusesOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/LceCurrenciesStatusesOperations.kt @@ -51,7 +51,6 @@ class LceCurrenciesStatusesOperations( maybeNetworkStatuses = null, maybeQuotes = null, maybeYieldBalances = null, - ) send(loadingCurrencies) }