FlowReduxDsl

FlowRedux-DSL

License

License

GroupId

GroupId

com.freeletics.flowredux
ArtifactId

ArtifactId

flowredux-dsl
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

FlowReduxDsl
FlowRedux-DSL
Project URL

Project URL

http://github.com/freeletics/FlowRedux/
Source Code Management

Source Code Management

http://github.com/freeletics/FlowRedux/

Download flowredux-dsl

How to add to project

<!-- https://jarcasting.com/artifacts/com.freeletics.flowredux/flowredux-dsl/ -->
<dependency>
    <groupId>com.freeletics.flowredux</groupId>
    <artifactId>flowredux-dsl</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.freeletics.flowredux/flowredux-dsl/
implementation 'com.freeletics.flowredux:flowredux-dsl:0.1.0'
// https://jarcasting.com/artifacts/com.freeletics.flowredux/flowredux-dsl/
implementation ("com.freeletics.flowredux:flowredux-dsl:0.1.0")
'com.freeletics.flowredux:flowredux-dsl:jar:0.1.0'
<dependency org="com.freeletics.flowredux" name="flowredux-dsl" rev="0.1.0">
  <artifact name="flowredux-dsl" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.freeletics.flowredux', module='flowredux-dsl', version='0.1.0')
)
libraryDependencies += "com.freeletics.flowredux" % "flowredux-dsl" % "0.1.0"
[com.freeletics.flowredux/flowredux-dsl "0.1.0"]

Dependencies

runtime (3)

Group / Artifact Type Version
com.freeletics.flowredux : flowredux jar 0.1.0
org.jetbrains.kotlin : kotlin-reflect jar 1.3.50
org.jetbrains.kotlin : kotlin-stdlib jar 1.3.50

Project Modules

There are no modules declared in this project.

FlowRedux

Download

Building async. running Kotlin Multiplatform state machine made easy with a DSL and coroutines.

Usage

Full documentation and best bractices can be found here: https://freeletics.github.io/FlowRedux/

sealed class State

object LoadingState : State()
data class ContentState(val items : List<Item>) : State()
data class ErrorState(val error : Throwable) : State()


sealed class Action
object RetryLoadingAction : Action()


class MyStateMachine : FlowReduxStateMachine<State, Action>(LoadingState){
    init {
        spec {
            inState<LoadingState> {
                onEnter  { getState, setState ->
                    // executes this block whenever we enter LoadingState
                    try {
                        val items = loadItems() // suspending function / coroutine to load items
                        setState { ContentState(items) } // Transition to ContentState
                    } catch (t : Throwable) {
                        setState { ErrorState(t) } // Transition to ErrorState
                    }
                }
            }

            inState<ErrorState> {
                on<RetryLoadingAction> { action, getState, setState ->
                    // executes this block whenever
                    // ErrorState is current state and RetryLoadingAction is emitted
                    setState { LoadingState } // Transition to LoadingState which loads list again
                 }
            }

            inState<ContentState> {
                collectWhileInState( flowOf(1,2,3) ) { getState, setState ->
                    // observes the given flow as long as state is ContentState.
                    // Once state is changed to another state the flow will automatically
                    // stop emitting.
                }
            }
        }
    }
}
val statemachine = MyStateMachine()

launch {  // Launch a coroutine
    statemachine.state.collect { state ->
      // do something with new state like update UI
      renderUI(state)
    }
}

// emit an Action
launch { // Launch a coroutine
    statemachine.dispatch(Action)
}

In an Android Application you would use it with AndroidX ViewModel like that:

class MyViewModel @Inject constructor(private val stateMachine : StateMachine) : ViewModel() {
    val state = MutableLiveData<State>()

    init {
        viewModelScope.launch { // automatically canceled once ViewModel lifecycle reached destroyed.
            stateMachine.state.collect { newState ->
                state.value = newState
            }
        }
    }

    fun dispatch(action : Action) {
        viewModelScope.launch {
            stateMachine.dispatch(action)
        }
    }
}

Dependencies

There are two artifacts that you can include as dependency::

  1. flowredux: this is the core library. Usually you dont want to use the core library directly but rather use the dsl.
  2. dsl which provides a convenient DSL on top of the core library. Usually this is what you want.

Multiplatform

implementation 'com.freeletics.flowredux:flowredux:0.4.0'
implementation 'com.freeletics.flowredux:dsl:0.4.0'

JVM only

implementation 'com.freeletics.flowredux:flowredux-jvm:0.4.0'
implementation 'com.freeletics.flowredux:dsl-jvm:0.4.0'

Native binaries

implementation 'com.freeletics.flowredux:flowredux-iosx64:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-iosarm64:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-iosarm32:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-watchosx86:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-watchosarm64:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-watchosarm32:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-tvosx64:0.4.0'
implementation 'com.freeletics.flowredux:flowredux-tvosxarm64:0.4.0'

implementation 'com.freeletics.flowredux:dsl-iosx64:0.4.0'
implementation 'com.freeletics.flowredux:dsl-iosarm64:0.4.0'
implementation 'com.freeletics.flowredux:dsl-iosarm32:0.4.0'
implementation 'com.freeletics.flowredux:dsl-watchosx86:0.4.0'
implementation 'com.freeletics.flowredux:dsl-watchosarm64:0.4.0'
implementation 'com.freeletics.flowredux:dsl-watchosarm32:0.4.0'
implementation 'com.freeletics.flowredux:dsl-tvosx64:0.4.0'
implementation 'com.freeletics.flowredux:dsl-tvosxarm64:0.4.0'

JavaScript

No javascript version release yet but its on our TODO list.

Snapshot

Latest snapshot (directly published from master branch from Travis CI):

allprojects {
    repositories {
        // Your repositories.
        // ...
        // Add url to snapshot repository
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
}

Then just use -SNAPSHOTsuffix as version like

implementation 'com.freeletics.flowredux:dsl:0.3.1-SNAPSHOT'
com.freeletics.flowredux

Freeletics

Freeletics is more than an app. It's a lifestyle powered by one of the most passionate and dedicated communities in the world.

Versions

Version
0.1.0