Skip to content

virendersran01/orbit-mvi

 
 

Repository files navigation

Orbit MVI for Kotlin and Android

CI status codecov Download License

Logo

Orbit is an MVI framework for Kotlin and Android which we use at Babylon Health.

slack logo Join us at the Kotlinlang slack!

If you do not yet have an account with the Kotlinlang slack workspace, sign up here.

Why Orbit

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

  1. MVI overview.
  2. Creating flows in Orbit.

Getting started

Download

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.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE.md file for details

About

An MVI framework for Kotlin and Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%