Skip to content

Commit

Permalink
Merge pull request #96 from matheus-corregiari/release/1.6.0
Browse files Browse the repository at this point in the history
[Release] 1.6.0
  • Loading branch information
matheus-corregiari committed Jul 8, 2024
2 parents 1895084 + 6616c18 commit 0207fe2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("TooManyFunctions")

package br.com.arch.toolkit.splinter

import androidx.lifecycle.DefaultLifecycleObserver
Expand All @@ -9,6 +11,7 @@ import br.com.arch.toolkit.livedata.MutableResponseLiveData
import br.com.arch.toolkit.livedata.ResponseLiveData
import br.com.arch.toolkit.result.DataResultStatus
import br.com.arch.toolkit.splinter.extension.invokeCatching
import br.com.arch.toolkit.splinter.strategy.MirrorFlow
import br.com.arch.toolkit.splinter.strategy.OneShot
import br.com.arch.toolkit.splinter.strategy.Strategy
import br.com.arch.toolkit.util.dataResultNone
Expand Down Expand Up @@ -339,6 +342,13 @@ class Splinter<RETURN : Any> internal constructor(
fun oneShotStrategy(strategyConfig: OneShot<RETURN>.Config.() -> Unit) = apply {
this.strategy = Strategy.oneShot(strategyConfig)
}

/**
* Define and configure a MirrorFlow strategy to this splinter
*/
fun mirrorFlowStrategy(strategyConfig: MirrorFlow<RETURN>.Config.() -> Unit) = apply {
this.strategy = Strategy.mirrorFlow(strategyConfig)
}
//endregion

//region Execution Policy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package br.com.arch.toolkit.splinter.strategy

import androidx.annotation.WorkerThread
import br.com.arch.toolkit.result.DataResult
import br.com.arch.toolkit.result.DataResultStatus
import br.com.arch.toolkit.splinter.Splinter
import br.com.arch.toolkit.splinter.extension.emitData
import br.com.arch.toolkit.splinter.extension.emitError
import br.com.arch.toolkit.splinter.extension.emitLoading
import br.com.arch.toolkit.splinter.extension.invokeCatching
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.catch

/**
* Strategy to mirror results from another flow using the DataResult
*
* @see Strategy
*/
class MirrorFlow<RESULT : Any> : Strategy<RESULT>() {

/**
* Block that configure how this Strategy will work ^^
*
* @see MirrorFlow.Config
*/
private val config = Config()
fun config(config: Config.() -> Unit) = apply { this.config.run(config) }

@WorkerThread
override suspend fun execute(
collector: FlowCollector<DataResult<RESULT>>,
executor: Splinter<RESULT>
) {
val onError: suspend (Throwable) -> Unit = { error ->
if (executor.status != DataResultStatus.SUCCESS) {
executor.logError("\t[MirrorFlow] Emit - Error! - $error")
flowError(error, collector, executor)
} else {
executor.logWarning("\t[MirrorFlow] Got some error but I did not emit it - $error")
}
}

kotlin.runCatching {
collector.emitLoading(executor.data)
requireNotNull(config.flow) { "Flow value mist be set!" }.invoke()
.catch { error -> onError.invoke(error) }
.collect { data ->
executor.logInfo("\t[MirrorFlow] Executed with success, data: $data")
executor.logInfo("\t[MirrorFlow] Emit - Success Data! - $data")
collector.emitLoading(data)
}
if (executor.error == null) executor.data?.let { collector.emitData(it) }
}.onFailure { error -> onError.invoke(error) }
}

override suspend fun flowError(
error: Throwable,
collector: FlowCollector<DataResult<RESULT>>,
executor: Splinter<RESULT>
) = collector.emitError(
error = config.mapError?.invoke(error) ?: error,
data = executor.data ?: config.fallback?.invokeCatching(error)?.getOrNull()
)

/**
*
*/
inner class Config internal constructor() {
internal var mapError: (suspend (Throwable) -> Throwable)? = null
internal var fallback: (suspend (Throwable) -> RESULT)? = null
internal var flow: (suspend () -> Flow<RESULT>)? = null

fun flow(flow: suspend () -> Flow<RESULT>) = apply { this.flow = flow }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class OneShot<RESULT : Any> : Strategy<RESULT>() {
fun config(config: Config.() -> Unit) = apply { this.config.run(config) }

@WorkerThread
@Suppress("LongMethod")
override suspend fun execute(
collector: FlowCollector<DataResult<RESULT>>,
executor: Splinter<RESULT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import kotlinx.coroutines.flow.FlowCollector
/**
* Main class that defines how the Splinter will work to emit the events!
*/
sealed class Strategy<RESULT : Any> {
abstract class Strategy<RESULT : Any> {

/**
* Here, all the magic happens
*
* This method will implement how the job inside splinter should run
*/
@WorkerThread
internal abstract suspend fun execute(
abstract suspend fun execute(
collector: FlowCollector<DataResult<RESULT>>,
executor: Splinter<RESULT>
)
Expand All @@ -26,7 +26,7 @@ sealed class Strategy<RESULT : Any> {
* In case of error inside the job, some uncaught exception on flow, this method will trigger inside splinter
*/
@WorkerThread
internal open suspend fun flowError(
open suspend fun flowError(
error: Throwable,
collector: FlowCollector<DataResult<RESULT>>,
executor: Splinter<RESULT>
Expand All @@ -36,7 +36,7 @@ sealed class Strategy<RESULT : Any> {
* In case of major error inside the job, some uncaught exception on job, this method will trigger inside splinter
*/
@WorkerThread
internal open suspend fun majorError(
open suspend fun majorError(
error: Throwable,
collector: FlowCollector<DataResult<RESULT>>,
executor: Splinter<RESULT>
Expand All @@ -52,5 +52,14 @@ sealed class Strategy<RESULT : Any> {
* @return br.com.arch.toolkit.splinter.strategy.OneShot
*/
fun <T : Any> oneShot(config: OneShot<T>.Config.() -> Unit) = OneShot<T>().config(config)

/**
* Helper method that creates a MirrorFlow strategy and configure it
*
* @param config - Block that will configure the mirrorFlow strategy
*
* @return br.com.arch.toolkit.splinter.strategy.MirrorFlow
*/
fun <T : Any> mirrorFlow(config: MirrorFlow<T>.Config.() -> Unit) = MirrorFlow<T>().config(config)
}
}
1 change: 1 addition & 0 deletions tools/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ID>Filename:_liveData.kt$br.com.arch.toolkit.util._liveData.kt</ID>
<ID>Filename:_util.kt$br.com.arch.toolkit._util.kt</ID>
<ID>LongParameterList:SampleData.kt$SampleData.NewData$( var nullableValue: T?, var value: T, var nullableListOfValue: List&lt;T>?, var listOfValue: List&lt;T>, var nullableMapOfValue: Map&lt;String, T>?, var mapOfValue: Map&lt;String, T>, )</ID>
<ID>ComplexCondition:Splinter.kt$Splinter$job.isActive || job.isCompleted || job.isCancelled</ID>
</ManuallySuppressedIssues>
<CurrentIssues/>
</SmellBaseline>
11 changes: 5 additions & 6 deletions tools/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ androidx-appcompat = "1.7.0"
androidx-annotation = "1.8.0"
androidx-constraint = "2.2.0-alpha13"
androidx-recyclerview = "1.3.2"
androidx-lifecycle-livedata = "2.8.2"
androidx-lifecycle-runtime = "2.8.2"
androidx-lifecycle = "2.8.3"
androidx-window = "1.3.0"
androidx-splash = "1.0.1"
androidx-security = "1.0.0"
Expand All @@ -44,7 +43,7 @@ androidx-compose-material = "1.6.8"
androidx-compose-material3 = "1.2.1"
androidx-compose-constraint = "1.0.1"
androidx-compose-core = "1.6.8"
androidx-compose-lifecycle = "2.8.2"
androidx-compose-lifecycle = "2.8.3"
androidx-compose-activity = "1.9.0"

## SquareUp
Expand Down Expand Up @@ -79,8 +78,8 @@ androidx-annotation = { group = "androidx.annotation", name = "annotation", vers
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-constraint = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraint" }
androidx-recycler = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "androidx-recyclerview" }
androidx-lifecycle-livedata = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "androidx-lifecycle-livedata" }
androidx-lifecycle-runtime = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "androidx-lifecycle-runtime" }
androidx-lifecycle-livedata = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "androidx-lifecycle" }
androidx-window = { group = "androidx.window", name = "window", version.ref = "androidx-window" }
androidx-splash = { group = "androidx.core", name = "core-splashscreen", version.ref = "androidx-splash" }
androidx-security = { group = "androidx.security", name = "security-crypto", version.ref = "androidx-security" }
Expand Down Expand Up @@ -136,7 +135,7 @@ x-normalize-006 = { group = "androidx.core", name = "core", version = "1.13.1" }
x-normalize-007 = { group = "androidx.core", name = "core-ktx", version = "1.13.1" }
x-normalize-008 = { group = "androidx.customview", name = "customview", version = "1.1.0" }
x-normalize-009 = { group = "androidx.drawerlayout", name = "drawerlayout", version = "1.2.0" }
x-normalize-010 = { group = "androidx.lifecycle", name = "lifecycle-common", version = "2.8.2" }
x-normalize-010 = { group = "androidx.lifecycle", name = "lifecycle-common", version.ref = "androidx-lifecycle" }
x-normalize-011 = { group = "androidx.viewpager2", name = "viewpager2", version = "1.1.0" }
x-normalize-012 = { group = "androidx.test", name = "monitor", version = "1.7.1" }
x-normalize-013 = { group = "androidx.test", name = "runner", version = "1.6.1" }
Expand Down

0 comments on commit 0207fe2

Please sign in to comment.