Updated on 2026-08-14

This commit is contained in:
Tangem 2024-06-24 10:50:03 +01:00
commit bc84d8f5f8
579 changed files with 13604 additions and 3422 deletions

View file

@ -96,4 +96,15 @@ sealed class Lce<out E : Any, out C : Any> {
ifContent = ::identity,
ifError = { null },
)
/**
* Returns the error of this [Lce] if it's a [Lce.Error], `null` otherwise.
*
* @return The error of this [Lce] or `null`.
*/
fun errorOrNull(): E? = fold(
ifLoading = { null },
ifContent = { null },
ifError = ::identity,
)
}

View file

@ -2,8 +2,10 @@ package com.tangem.domain.core.lce
import arrow.atomic.Atomic
import arrow.core.raise.Raise
import arrow.core.raise.RaiseDSL
import arrow.core.raise.recover
import com.tangem.domain.core.utils.lceContent
import com.tangem.domain.core.utils.lceError
import com.tangem.domain.core.utils.lceLoading
import kotlin.experimental.ExperimentalTypeInference
@ -18,18 +20,55 @@ class LceRaise<E : Any> @PublishedApi internal constructor(
private val raise: Raise<Lce<E, Nothing>>,
) : Raise<Lce<E, Nothing>> by raise {
/**
* An [Atomic] boolean flag indicating whether a loading operation is in progress.
*/
val isLoading: Atomic<Boolean> = Atomic(false)
/**
* Helper function to raise an [Lce.Error] state with the given error object.
* */
@RaiseDSL
@JvmName(name = "raiseError")
fun raise(r: E): Nothing = raise(r = r.lceError())
/**
* Helper function to raise an [Lce.Loading] state.
*/
@RaiseDSL
fun raiseLoading(): Nothing = raise(r = lceLoading())
/**
* Execute the [Raise] context function resulting in [C] or any _logical error_ of type [OtherError],
* and transform any raised [OtherError] into [E], which is raised to the outer [Raise].
*
* @see arrow.core.raise.withError
* */
@RaiseDSL
@OptIn(ExperimentalTypeInference::class)
inline fun <OtherError : Any, C : Any> withError(
transform: (OtherError) -> E,
@BuilderInference block: LceRaise<OtherError>.() -> C,
): C = recover(
block = { block(LceRaise(raise = this@recover)) },
recover = { error ->
error.fold<Nothing>(
ifLoading = { raiseLoading() },
ifError = { raise(transform(it)) },
ifContent = { it },
)
},
)
/**
* Binds the content of this [Lce] instance and handles its state.
* If this is a [Lce.Loading] state, sets the [isLoading] flag to true and calls the [ifLoading] function.
* If this is a [Lce.Content] state, returns the content.
* If this is a [Lce.Error] state, raises the error.
*
* @param ifLoading The function to call if this is a [Lce.Loading] state.
* By default, it raises a new [Lce.Loading] state.
* @return The content of this [Lce] instance.
*/
@RaiseDSL
fun <C : Any> Lce<E, C>.bind(): C = when (this) {
is Lce.Loading -> {
isLoading.set(true)
@ -48,6 +87,7 @@ class LceRaise<E : Any> @PublishedApi internal constructor(
*
* @return The content of this [Lce] instance.
*/
@RaiseDSL
fun <C : Any> Lce<E, C>.bindOrNull(): C? = when (this) {
is Lce.Loading -> {
isLoading.set(true)

View file

@ -0,0 +1,21 @@
package com.tangem.domain.core.serialization
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.math.BigDecimal
internal object BigDecimalSerializer : KSerializer<BigDecimal> {
override val descriptor = PrimitiveSerialDescriptor(serialName = "BigDecimal", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: BigDecimal) {
encoder.encodeString(value.toString())
}
override fun deserialize(decoder: Decoder): BigDecimal {
return BigDecimal(decoder.decodeString())
}
}

View file

@ -0,0 +1,6 @@
package com.tangem.domain.core.serialization
import kotlinx.serialization.Serializable
import java.math.BigDecimal
typealias SerializedBigDecimal = @Serializable(with = BigDecimalSerializer::class) BigDecimal