Git Product home page Git Product logo

flowredux's Introduction

FlowRedux

Download

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

Usage

Full documentation and best practices 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 { stateSnapshot : LoadingState ->
                    // executes this block whenever we enter LoadingState
                    try {
                        val items = loadItems() // suspending function / coroutine to load items
                        OverrideState( ContentState(items) ) // Transition to ContentState
                    } catch (t : Throwable) {
                        OverrideState( ErrorState(t) ) // Transition to ErrorState
                    }
                }
            }

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

            inState<ContentState> {
                collectWhileInState( flowOf(1,2,3) ) { value : Int, stateSnapshot : ContentState ->
                    // observes the given flow as long as state is ContentState.
                    // Once state is changed to another state the flow will automatically
                    // stop emitting.
                    MutateState<ContentState, State> { 
                        copy( items = this.items + Item("New item $value"))
                    }
                }
            }
        }
    }
}
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 could 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.
  3. compose: contains some convenient extensions to work with FlowReduxStateMachine in Jetpack Compose.

Multiplatform

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

JVM only

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

Native binaries

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

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

JavaScript

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

Jetpack Compose

Contains some convenient extensions to work with FlowReduxStateMachine with Jetpack Compose.

implementation 'com.freeletics.flowredux:compose:0.11.0'

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.11.1-SNAPSHOT'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.