ReactiveState

Kotlin Multiplatform ViewModels and reactive state management based on StateFlow.

See the reactivestate-core module documentation as a starting point with several usage examples.

Short examples

Map/transform a StateFlow:

val number = MutableStateFlow(0)
val doubledNumber: StateFlow<Int> = derived { 2 * get(number) }

Collect two StateFlows (just collect without transforming):

val base = MutableStateFlow(0)
val extra = MutableStateFlow(0)
autoRun {
    if (get(base) + get(extra) 10) {
        alert("You're flying too high")
    }
}

Multiplatform ViewModels with automatic error handling and loading indicator tracking:

class ExampleViewModel(scope: CoroutineScope, val repository: ExampleRepository) : ReactiveViewModel(scope) {
    val inputFieldValue = MutableStateFlow("default")

    fun submit() {
        // The launch function automatically catches exceptions and increments/decrements the loading indicator.
        // This way you can't forget the fundamentals that have to be always handled correctly.
        launch {
            repository.submit(inputFieldValue.value)
        }
    }
}

Intercept MutableStateFlow:

public val state: MutableStateFlow<String> = MutableStateFlow("value").afterUpdate {
    // This is called every time after someone sets `state.value = ...`
}

Convert StateFlow to MutableStateFlow:

val readOnly: StateFlow<Int> = getSomeStateFlow()
val mutable: MutableStateFlow<Int> = readOnly.toMutable { value: Int ->
    // This is executed whenever someone sets `mutable.value = ...`.
}

Supported platforms

  • Android

  • JVM

  • All native (including iOS)

  • JS

  • WASM

Installation

Add the package to your build.gradle:

dependencies {
    // Add the BOM using the desired ReactiveState version
    api platform("com.ensody.reactivestate:reactivestate-bom:VERSION")
    // Leave out the version number from now on.

    // Jetpack Compose integration
    implementation "com.ensody.reactivestate:reactivestate-compose"

    // Android-only integration for Activity/Fragment
    implementation "com.ensody.reactivestate:reactivestate-android"

    // UI-independent core APIs
    implementation "com.ensody.reactivestate:reactivestate-core"

    // Utils for unit tests that want to use coroutines
    implementation "com.ensody.reactivestate:reactivestate-core-test"

    // Android-only unit test extensions
    implementation "com.ensody.reactivestate:reactivestate-android-test"
}

Also, make sure you've integrated the Maven Central repo, e.g. in your root build.gradle:

subprojects {
    repositories {
        // ...
        mavenCentral()
        // ...
    }
}

All modules:

Link copied to clipboard

APIs mostly useful for classic Android Fragment/Activity.

Link copied to clipboard

Provides a simple AndroidCoroutineTest helper that also sets up an InstantTaskExecutorRule.

Link copied to clipboard

Multiplatform Jetpack Compose integration for com.ensody.reactivestate.ReactiveViewModel and com.ensody.reactivestate.ReactiveState. Also provides utilities for State/MutableState mutation interception.

Link copied to clipboard

Core APIs for multiplatform ViewModels (ReactiveViewModel), transforming StateFlow, intercepting the MutableStateFlow value setter, coroutine-locals (like thread-locals, or CompositionLocal for coroutines) and many other utilities.

Link copied to clipboard

Utilities for writing coroutine tests.