Updated on 2026-08-14

This commit is contained in:
Tangem 2024-05-29 15:57:18 +04:00
parent 5d206bd37f
commit 96070a18c2
4 changed files with 19 additions and 16 deletions

View file

@ -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<Throwable, List<CryptoCurrency>> = 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)
}
}
}

View file

@ -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)
}
}
}

View file

@ -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<E, C> = Flow<Lce<E, C>>
class LceFlowScope<E : Any, C : Any> @PublishedApi internal constructor(
private val raise: LceRaise<E>,
private val scope: ProducerScope<Lce<E, C>>,
private val ifLoading: LceRaise<E>.(C) -> Lce<E, C>,
) : Raise<Lce<E, Nothing>> by raise,
CoroutineScope by scope {
private val ifLoading: suspend LceFlowScope<E, C>.(C?) -> Unit,
) : Raise<E>, CoroutineScope by scope {
/**
* Raises an [Lce] instance within the [ProducerScope].
@ -40,13 +40,13 @@ class LceFlowScope<E : Any, C : Any> @PublishedApi internal constructor(
*
* @param r The [Lce] instance to raise.
*/
override fun raise(r: Lce<E, Nothing>): 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<E : Any, C : Any> @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<E, C>) {
scope.send(value)
}
}
/**
@ -73,14 +78,14 @@ class LceFlowScope<E : Any, C : Any> @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 <E : Any, C : Any> lceFlow(
ifLoading: LceRaise<E>.(C) -> Lce<E, C> = { lceLoading(partialContent = it) },
ifLoading: suspend LceFlowScope<E, C>.(C?) -> Unit = { send(lceLoading(partialContent = it)) },
@BuilderInference block: suspend LceFlowScope<E, C>.() -> Unit,
): LceFlow<E, C> {
return channelFlow {

View file

@ -30,11 +30,11 @@ class LceRaise<E : Any> @PublishedApi internal constructor(
* By default, it raises a new [Lce.Loading] state.
* @return The content of this [Lce] instance.
*/
fun <C : Any> Lce<E, C>.bind(ifLoading: (partialContent: C?) -> C = { raise(lceLoading()) }): C = when (this) {
fun <C : Any> Lce<E, C>.bind(): C = when (this) {
is Lce.Loading -> {
isLoading.set(true)
ifLoading(partialContent)
raise(lceLoading())
}
is Lce.Content -> content
is Lce.Error -> raise(r = this)