Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/freeletics/FlowRedux
- Owner: freeletics
- License: apache-2.0
- Created: 2019-05-03T12:44:23.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-31T22:05:53.000Z (3 months ago)
- Last Synced: 2024-11-01T00:12:14.543Z (3 months ago)
- Topics: android, architecture, coroutines, kotlin, mvi, mvi-android
- Language: Kotlin
- Homepage: https://freeletics.github.io/FlowRedux/
- Size: 6.38 MB
- Stars: 709
- Watchers: 27
- Forks: 27
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/contributing.md
- License: LICENSE
Awesome Lists containing this project
- kmp-awesome - FlowRedux - Statemachine library with nice DSL (Libraries / π Architecture)
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 Stateobject Loading : State
data class ContentState(val items : List) : State
data class Error(val error : Throwable) : Statesealed interface Action
object RetryLoadingAction : Actionclass 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
- WindowsWe 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'
```