Orbit is an MVI framework for Kotlin and Android which we use at Babylon Health.
Join us at the Kotlinlang slack!
If you do not yet have an account with the Kotlinlang slack workspace, sign up here.
Orbit provides the minimum structure possible around your redux implementation to make it easy to use, yet leave you open to use RxJava's power.
When we created Orbit, we initially looked at other redux libraries out there but felt they didn't meet our needs. Some didn't handle Android lifecycle, and others had elaborate structured APIs while some provided custom functionality that RxJava gives you out of the box.
We drew inspiration from Managing State with RxJava by Jake Wharton, RxFeedback and Mosby MVI.
For more details about MVI and our implementation, please read
Include the following dependencies in your build.gradle.kts file:
implementation("com.babylon.orbit:orbit:<latest-version>")
implementation("com.babylon.orbit:orbit-android:<latest-version>")
A real-world redux system might look as follows:
data class State(val total: Int = 0)
data class AddAction(val number: Int)
sealed class SideEffect {
data class Toast(val text: String) : SideEffect()
}
class CalculatorViewModel : OrbitViewModel<State, SideEffect> (State(), {
perform("addition")
.on<AddAction>()
.sideEffect { post(SideEffect.Toast("Adding ${event.number}")) }
.reduce {
currentState.copy(currentState.total + event.number)
}
...
})
And then in your activity / fragment
// Example of injection using koin, your DI system might differ
private val viewModel by viewModel<CalculatorViewModel>()
override fun onCreate() {
...
addButton.setOnClickListener { viewModel.sendAction(AddAction) }
}
override fun onStart() {
viewModel.connect(this, ::handleState, ::handleSideEffect)
}
private fun handleState(state: State) {
...
}
private fun handleSideEffect(sideEffect: SideEffect) {
when (sideEffect) {
is SideEffect.Toast -> toast(sideEffect.text)
}
}
Read more about what makes an orbit.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under the Apache License, Version 2.0 - see the LICENSE.md file for details