Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/freeletics/FlowRedux

Kotlin Multiplatform Statemachine library with nice DSL based on Flow from Kotlin Coroutine's.
https://github.com/freeletics/FlowRedux

android architecture coroutines kotlin mvi mvi-android

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

        

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

```kotlin
sealed interface State

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

sealed interface Action
object RetryLoadingAction : Action

class MyStateMachine : FlowReduxStateMachine(initialState = Loading){
init {
spec {
inState {
onEnter { state : State ->
// 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 {
on { action : RetryLoadingAction, state : State ->
// 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 {
collectWhileInState( flowOf(1,2,3) ) { value : Int, state : State ->
// 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"))
}
}
}
}
}
}
```

```kotlin
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:

```kotlin
class MyViewModel @Inject constructor(private val stateMachine : MyStateMachine) : ViewModel() {
val state = MutableLiveData()

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](https://developer.android.com/jetpack/compose).

[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/freeletics/flowredux?logo=github&sort=semver)](https://github.com/freeletics/flowredux/releases)

### JVM / Android only
```groovy
implementation 'com.freeletics.flowredux:flowredux-jvm:'
implementation 'com.freeletics.flowredux:compose:'
```

### Multiplatform
```groovy
implementation 'com.freeletics.flowredux:flowredux:'
```

FlowRedux is supported on:

- JVM / Android
- iOS
- watchOS
- tvOS
- macOS
- Linux
- Windows

We do plan to add support for JavaScript but it’s not available yet.

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

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

Then just use `-SNAPSHOT`suffix as version name like

```groovy
implementation 'com.freeletics.flowredux:flowredux:1.2.1-SNAPSHOT'
```