amrfarid140 / orbit-mvi

An MVI framework for Kotlin and Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What's in this Fork

This fork replaces the use of RxJava with the use of the Flow API from Kotlin coroutines. As it stands, this forks is experimental so use it as your own risk.

Orbit MVI for Kotlin and Android

CI status Download License

Logo

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

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>()
        .postSideEffect { SideEffect.Toast("Adding ${action.number}") }
        .withReducer { state.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.postAction(AddAction) }
}

override fun onStart() {
    viewModel.connect(this, actions, ::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

License:Apache License 2.0


Languages

Language:Kotlin 100.0%