diff --git a/domain/core/.gitignore b/domain/core/.gitignore new file mode 100644 index 0000000000..796b96d1c4 --- /dev/null +++ b/domain/core/.gitignore @@ -0,0 +1 @@ +/build diff --git a/domain/core/build.gradle.kts b/domain/core/build.gradle.kts new file mode 100644 index 0000000000..63325166f7 --- /dev/null +++ b/domain/core/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + alias(deps.plugins.kotlin.jvm) + id("configuration") +} + +dependencies { + api(deps.kotlin.coroutines) + api(deps.arrow.core) + api(deps.arrow.fx) +} \ No newline at end of file diff --git a/domain/core/src/main/kotlin/com/tangem/domain/core/chain/Chain.kt b/domain/core/src/main/kotlin/com/tangem/domain/core/chain/Chain.kt new file mode 100644 index 0000000000..c61ed6a0bd --- /dev/null +++ b/domain/core/src/main/kotlin/com/tangem/domain/core/chain/Chain.kt @@ -0,0 +1,7 @@ +package com.tangem.domain.core.chain + +import arrow.core.Either + +interface Chain { + suspend operator fun invoke(previousChainResult: Either): Either +} \ No newline at end of file diff --git a/domain/core/src/main/kotlin/com/tangem/domain/core/chain/ChainProcessor.kt b/domain/core/src/main/kotlin/com/tangem/domain/core/chain/ChainProcessor.kt new file mode 100644 index 0000000000..be45957aff --- /dev/null +++ b/domain/core/src/main/kotlin/com/tangem/domain/core/chain/ChainProcessor.kt @@ -0,0 +1,28 @@ +package com.tangem.domain.core.chain + +import arrow.core.Either + +class ChainProcessor { + private val chains: MutableList> = mutableListOf() + + fun addChains(vararg chains: Chain) { + this.chains.addAll(chains) + } + + suspend fun launchChains(initial: R, returnOnFirstError: Boolean = true): Either { + return launchChains(initial = Either.Right(initial), returnOnFirstError) + } + + private suspend fun launchChains(initial: Either, returnOnFirstError: Boolean = true): Either { + var result = initial + + chains.forEach { chain -> + result = chain(result) + .onRight { + if (returnOnFirstError) return result + } + } + + return result + } +} \ No newline at end of file diff --git a/gradle/dependencies.toml b/gradle/dependencies.toml index 556b4d84ca..7598829c76 100644 --- a/gradle/dependencies.toml +++ b/gradle/dependencies.toml @@ -68,6 +68,7 @@ zxingQrBarcodeScanner = "1.9.8" zxingQrCode = "3.5.1" mviCore = "1.3.1" kotlinSerialization = "1.4.1" +arrow = "1.2.0-RC" # endregion Other libraries # region Tangem @@ -206,4 +207,6 @@ zxing-qrBarcodeScanner = { module = "me.dm7.barcodescanner:zxing", version.ref = zxing-qrCore = { module = "com.google.zxing:core", version.ref = "zxingQrCode" } mviCore-watcher = { module = "com.github.badoo.mvicore:mvicore-diff", version.ref = "mviCore" } kotlin-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinSerialization" } +arrow-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" } +arrow-fx = { module = "io.arrow-kt:arrow-fx-coroutines", version.ref = "arrow" } # endregion Other diff --git a/settings.gradle.kts b/settings.gradle.kts index d3f3310ef6..be4258e20d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -62,4 +62,6 @@ include(":features:tester:impl") // TODO: Remove, temporary modules include(":domain:models") include(":domain:legacy") + +include(":domain:core") // endregion Domain modules \ No newline at end of file