From 4a6dee8e568f07b865121042f426b9f68fdc118c Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 15 Jul 2024 23:12:51 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../balancehiding/error/HideBalancesError.kt | 2 +- .../kotlin/com/tangem/domain/core/lce/Lce.kt | 39 +++++++++++++++++++ .../com/tangem/domain/core/lce/LceRaise.kt | 2 +- .../tokens/GetWalletTotalBalanceUseCase.kt | 20 +++++++--- .../CurrenciesStatusesLceOperations.kt | 4 +- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/domain/balance-hiding/src/main/kotlin/com/tangem/domain/balancehiding/error/HideBalancesError.kt b/domain/balance-hiding/src/main/kotlin/com/tangem/domain/balancehiding/error/HideBalancesError.kt index 7438c20918..fb0e72e862 100644 --- a/domain/balance-hiding/src/main/kotlin/com/tangem/domain/balancehiding/error/HideBalancesError.kt +++ b/domain/balance-hiding/src/main/kotlin/com/tangem/domain/balancehiding/error/HideBalancesError.kt @@ -2,7 +2,7 @@ package com.tangem.domain.balancehiding.error sealed class HideBalancesError { - object HidingDisabled : HideBalancesError() + data object HidingDisabled : HideBalancesError() data class DataError(val cause: Throwable) : HideBalancesError() } \ No newline at end of file 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 0812a3d523..2d6902a58a 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 @@ -107,4 +107,43 @@ sealed class Lce { ifContent = { null }, ifError = ::identity, ) + + /** + * Returns `true` if this [Lce] is a [Lce.Loading] state and the given predicate is `true`. + * + * @param predicate The predicate to apply to the partial content. + * By default, the predicate is `true` for any partial content. + * @return `true` if this [Lce] is a [Lce.Loading] state and the given predicate is `true`, `false` otherwise. + */ + fun isLoading(predicate: (maybeContent: C?) -> Boolean = { true }): Boolean = fold( + ifLoading = { predicate(it) }, + ifContent = { false }, + ifError = { false }, + ) + + /** + * Returns `true` if this [Lce] is a [Lce.Error] state and the given predicate is `true`. + * + * @param predicate The predicate to apply to the error. + * By default, the predicate is `true` for any error. + * @return `true` if this [Lce] is a [Lce.Error] state and the given predicate is `true`, `false` otherwise. + */ + fun isError(predicate: (error: E) -> Boolean = { true }): Boolean = fold( + ifLoading = { false }, + ifContent = { false }, + ifError = { predicate(it) }, + ) + + /** + * Returns `true` if this [Lce] is a [Lce.Content] state and the given predicate is `true`. + * + * @param predicate The predicate to apply to the content. + * By default, the predicate is `true` for any content. + * @return `true` if this [Lce] is a [Lce.Content] state and the given predicate is `true`, `false` otherwise. + */ + fun isContent(predicate: (content: C) -> Boolean = { true }): Boolean = fold( + ifLoading = { false }, + ifContent = { predicate(it) }, + ifError = { false }, + ) } \ No newline at end of file diff --git a/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceRaise.kt b/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceRaise.kt index 2040b0774a..1484205426 100644 --- a/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceRaise.kt +++ b/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceRaise.kt @@ -46,7 +46,7 @@ class LceRaise @PublishedApi internal constructor( * */ @RaiseDSL @OptIn(ExperimentalTypeInference::class) - inline fun withError( + inline fun withError( transform: (OtherError) -> E, @BuilderInference block: LceRaise.() -> C, ): C = recover( diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetWalletTotalBalanceUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetWalletTotalBalanceUseCase.kt index ce783509f9..0f4d3477da 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetWalletTotalBalanceUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetWalletTotalBalanceUseCase.kt @@ -1,5 +1,6 @@ package com.tangem.domain.tokens +import arrow.atomic.update import arrow.core.raise.ensureNotNull import arrow.core.toNonEmptyListOrNull import com.tangem.domain.core.lce.Lce @@ -16,9 +17,10 @@ import com.tangem.domain.tokens.repository.CurrenciesRepository import com.tangem.domain.tokens.repository.NetworksRepository import com.tangem.domain.tokens.repository.QuotesRepository import com.tangem.domain.wallets.models.UserWalletId +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.transform +import kotlinx.coroutines.flow.transformLatest class GetWalletTotalBalanceUseCase( private val currenciesRepository: CurrenciesRepository, @@ -28,9 +30,9 @@ class GetWalletTotalBalanceUseCase( ) { suspend operator fun invoke( - userWallestIds: Collection, + userTallestIds: Collection, ): LceFlow> { - val flows = userWallestIds.distinct() + val flows = userTallestIds.distinct() .map { userWalletId -> invoke(userWalletId).map { maybeBalance -> userWalletId to maybeBalance @@ -39,17 +41,23 @@ class GetWalletTotalBalanceUseCase( return combine(flows) { balances -> lce { - balances.associate { (userWalletId, maybeBalance) -> - userWalletId to maybeBalance.bind() + balances.fold(mutableMapOf()) { acc, (userWalletId, maybeBalance) -> + val balance = maybeBalance.bindOrNull() ?: TotalFiatBalance.Loading + + isLoading.update { it || balance is TotalFiatBalance.Loading } + + acc[userWalletId] = balance + acc } } } } + @OptIn(ExperimentalCoroutinesApi::class) suspend operator fun invoke(userWalletId: UserWalletId): LceFlow { val currenciesStatuses = getStatuses(userWalletId) - return currenciesStatuses.transform { maybeStatuses -> + return currenciesStatuses.transformLatest { maybeStatuses -> val balance = createBalance(maybeStatuses) emit(balance) diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesLceOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesLceOperations.kt index a4265dd621..0a542d0874 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesLceOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesLceOperations.kt @@ -33,7 +33,7 @@ internal class CurrenciesStatusesLceOperations( return transformToCurrenciesStatuses( userWalletId = userWalletId, flow = if (isSingleCurrencyWalletsAllowed) { - getWalletCurrenies(userWalletId) + getWalletCurrencies(userWalletId) } else { getMultiCurrencyWalletCurrencies(userWalletId) }, @@ -105,7 +105,7 @@ internal class CurrenciesStatusesLceOperations( return statuses } - private fun getWalletCurrenies(userWalletId: UserWalletId): LceFlow> { + private fun getWalletCurrencies(userWalletId: UserWalletId): LceFlow> { return currenciesRepository.getWalletCurrenciesUpdates(userWalletId) .map { maybeCurrencies -> maybeCurrencies.mapError { TokenListError.DataError(it) }