• Stars
    star
    706
  • Rank 64,138 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kotlin Multiplatform Statemachine library with nice DSL based on Flow from Kotlin Coroutine's.

FlowRedux

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/dsl/

sealed interface State

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


sealed interface Action
object RetryLoadingAction : Action


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

            inState<Error> {
                on<RetryLoadingAction> { action : RetryLoadingAction, state : State<Error> ->
                    // executes this block whenever Error state is current state and RetryLoadingAction is emitted
                    state.override { Loading } // Transition to Loading state which loads list again
                 }
            }

            inState<ContentState> {
                collectWhileInState( flowOf(1,2,3) ) { value : Int, state : State<ContentState> ->
                    // observes the given flow as long as state is ContentState.
                    // Once state is changed to another state the flow will automatically
                    // stop emitting.
                    state.mutate {
                        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 : MyStateMachine) : 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 dependencis:

  1. flowredux: this is the core library and includes the DSL.
  2. compose: contains some convenient extensions to work with FlowReduxStateMachine in Jetpack Compose.

GitHub release (latest SemVer)

JVM / Android only

implementation 'com.freeletics.flowredux:flowredux-jvm:<latest-version>'
implementation 'com.freeletics.flowredux:compose:<latest-version>'

Multiplatform

implementation 'com.freeletics.flowredux:flowredux:<latest-version>'

JavaScript

No javascript version released yet but it is on our roadmap.

Snapshot

Latest snapshot (directly published from main branch from CI on each change):

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

Then just use -SNAPSHOTsuffix as version name like

implementation 'com.freeletics.flowredux:flowredux:1.0.3-SNAPSHOT'

More Repositories

1

RxRedux

Redux implementation based on RxJava
Kotlin
568
star
2

CoRedux

Opinionated Redux store implementation using Kotlin coroutines
Kotlin
157
star
3

khonshu

Khonshu is the foundation of how we build apps at Freeletics. It provides the general set up for screens as well as navigation.
Kotlin
79
star
4

array_enum

String to integer mapping for PostgreSQL array columns
Ruby
39
star
5

creds

Encrypted & plain text credentials for multiple environments
Ruby
38
star
6

RxSmartLock

Reactive implementation of the Google Smart Lock for passwords
Kotlin
33
star
7

FLTextView

FLTextView: UITextView + Placeholder in Swift
Swift
21
star
8

migrator

General purpose library for android to do in app migrations.
Kotlin
18
star
9

FlowRecorder

A utility class to make it easier to test emissions on kotlin's Flow Type over time
Kotlin
18
star
10

MotionLayoutWorkshop

Kotlin
13
star
11

StateLayout

Simple state management in UI layer
Kotlin
6
star
12

freeletics-gradle-plugins

A collection of convention plugins that are used internally at Freeletics.
Kotlin
5
star
13

libdmtx-mobile

Android and iOS ports of libdmtx (https://github.com/dmtx/libdmtx)
Starlark
5
star
14

rxworkshop

Kotlin
4
star
15

proguard-presentation

An introduction to how ProGuard works. Presented at Freeletics on August 31, 2017.
Shell
2
star
16

actions

2
star
17

advent2018

Collection of advent 2018 codes in different languages
Ruby
1
star
18

optivo_api

Ruby
1
star
19

PullRequestCommentor-Collector

A plugin based command line tool for collecting code quality reports
Kotlin
1
star
20

fl-snowflake-lambda-layer

The repo has the required code to create an AWS lambda layer for snowflake POC
1
star
21

techops-challenge

HCL
1
star
22

ASO-collector

This is Aso collector repo
JavaScript
1
star