From 96070a18c206740599371e690f3228bcaef722fe Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 29 May 2024 15:57:18 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../repository/DefaultCurrenciesRepository.kt | 5 ++-- .../repository/DefaultNetworksRepository.kt | 3 +-- .../com/tangem/domain/core/lce/LceFlow.kt | 23 +++++++++++-------- .../com/tangem/domain/core/lce/LceRaise.kt | 4 ++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt index 1b8f9800e3..f17e141216 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt @@ -23,7 +23,6 @@ import com.tangem.domain.common.util.hasDerivation import com.tangem.domain.core.error.DataError import com.tangem.domain.core.lce.LceFlow import com.tangem.domain.core.lce.lceFlow -import com.tangem.domain.core.utils.lceError import com.tangem.domain.demo.DemoConfig import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.CryptoCurrencyStatus @@ -228,7 +227,7 @@ internal class DefaultCurrenciesRepository( ): LceFlow> = lceFlow { val userWallet = getUserWallet(userWalletId) catch({ ensureIsCorrectUserWallet(userWallet, isMultiCurrencyWalletExpected = true) }) { - raise(it.lceError()) + raise(it) } launch(dispatchers.io) { @@ -242,7 +241,7 @@ internal class DefaultCurrenciesRepository( withContext(dispatchers.io) { catch({ fetchTokensIfCacheExpired(userWallet, refresh = false) }) { - raise(it.lceError()) + raise(it) } } } 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 28e83c756e..23a2015bf4 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 @@ -14,7 +14,6 @@ import com.tangem.datasource.local.userwallet.UserWalletsStore import com.tangem.domain.common.util.cardTypesResolver import com.tangem.domain.core.lce.LceFlow import com.tangem.domain.core.lce.lceFlow -import com.tangem.domain.core.utils.lceError import com.tangem.domain.demo.DemoConfig import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.CryptoCurrencyAddress @@ -78,7 +77,7 @@ internal class DefaultNetworksRepository( withContext(dispatchers.io) { catch({ fetchNetworksStatusesIfCacheExpired(userWalletId, networks, refresh = false) }) { - raise(it.lceError()) + raise(it) } } } diff --git a/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceFlow.kt b/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceFlow.kt index 3a5715abf5..9be30e9699 100644 --- a/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceFlow.kt +++ b/domain/core/src/main/kotlin/com/tangem/domain/core/lce/LceFlow.kt @@ -2,6 +2,7 @@ package com.tangem.domain.core.lce import arrow.core.raise.Raise import com.tangem.domain.core.utils.lceContent +import com.tangem.domain.core.utils.lceError import com.tangem.domain.core.utils.lceLoading import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.NonCancellable @@ -30,9 +31,8 @@ typealias LceFlow = Flow> class LceFlowScope @PublishedApi internal constructor( private val raise: LceRaise, private val scope: ProducerScope>, - private val ifLoading: LceRaise.(C) -> Lce, -) : Raise> by raise, - CoroutineScope by scope { + private val ifLoading: suspend LceFlowScope.(C?) -> Unit, +) : Raise, CoroutineScope by scope { /** * Raises an [Lce] instance within the [ProducerScope]. @@ -40,13 +40,13 @@ class LceFlowScope @PublishedApi internal constructor( * * @param r The [Lce] instance to raise. */ - override fun raise(r: Lce): Nothing { + override fun raise(r: E): Nothing { scope.launch(NonCancellable) { - scope.send(r) + scope.send(r.lceError()) scope.close() } - raise.raise(r) + raise.raise(r.lceError()) } /** @@ -59,13 +59,18 @@ class LceFlowScope @PublishedApi internal constructor( */ suspend fun send(content: C, isStillLoading: Boolean = false) { val value = if (isStillLoading) { - ifLoading(raise, content) + ifLoading(content) + return } else { content.lceContent() } scope.send(value) } + + suspend fun send(value: Lce) { + scope.send(value) + } } /** @@ -73,14 +78,14 @@ class LceFlowScope @PublishedApi internal constructor( * * Flow starts with a [Lce.Loading] state. * - * @param ifLoading The function to call if the [block] raises a [Lce.Loading] state. + * @param ifLoading The function to call if received a loading content. * By default, it creates a new [Lce.Loading] state with the value returned by the [block]. * @param block The block to execute within a [LceFlowScope] context. * @return A [LceFlow] representing the result of the [block]. */ @OptIn(ExperimentalTypeInference::class) fun lceFlow( - ifLoading: LceRaise.(C) -> Lce = { lceLoading(partialContent = it) }, + ifLoading: suspend LceFlowScope.(C?) -> Unit = { send(lceLoading(partialContent = it)) }, @BuilderInference block: suspend LceFlowScope.() -> Unit, ): LceFlow { return channelFlow { 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 22a94b519b..90dbc6d172 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 @@ -30,11 +30,11 @@ class LceRaise @PublishedApi internal constructor( * By default, it raises a new [Lce.Loading] state. * @return The content of this [Lce] instance. */ - fun Lce.bind(ifLoading: (partialContent: C?) -> C = { raise(lceLoading()) }): C = when (this) { + fun Lce.bind(): C = when (this) { is Lce.Loading -> { isLoading.set(true) - ifLoading(partialContent) + raise(lceLoading()) } is Lce.Content -> content is Lce.Error -> raise(r = this)