Code Monkey home page Code Monkey logo

mvi's Introduction

Model View Intent - MVI

Reactive and simple Model View Intent example using Kotlin and RxJava. Use a BehaviourSubject to save your State when your app rotates.

Example of Action

 override fun execute(): Observable<PartialState> {
   return service.get()
        .onBackground()
        .toObservable()
        .map { price -> ModifyBTCPrice(price[“USD”]?.toFloat()) as PartialState }
        .startWith(ModifyLoading(true) as PartialState)
        .doOnError { e -> println(“Ouuu yeah!!…”)) }
        .doOnNext { logger.log("=> GePrice") }
    }
 }

Store to save your state, listen for action and publish changes

  
  fun getDispatch(action: Action) {
    inPipeline.onNext(action)
  }
  
  fun changes() = outPipeline.hide()
  
  private fun mainPipe() {
    disposable = inPipeline
      .switchMap { it.execute() }
      .scan(state) { state, action -> Reducer.reduce(state, action) }
      .distinctUntilChanged()
      .doOnNext {
        logger.log(it.toString())
      }
      .subscribeOn(ioScheduler)
      .observeOn(uiScheduler)
      .subscribe { outPipeline.onNext(it) }
  }

Pure reducer to generate new states

object Reducer {
 
 fun reduce(state: State, partialState: PartialState): State =
    when (partialState) {
      is ModifyBTCPrice -> {
        state.copy(btcCurrentPrice = partialState.price, loading = false)
      }
      is ModifyLoading -> {
        state.copy(loading = partialState.value)
      }
      is ModifyBalance -> {
        state.copy(result = (state.btcCurrentPrice * partialState.balance))
      }
      is ModifyError -> {
        state.copy(error = partialState.error, loading = false)
      }
    }
}

How can you send actions?

store.getDispatch(
   GetBTCPrice(Service())
)

How can you listen for changes?

disposable = store.changes().subscribe { render(it) }

Why you should use MVI?

  • Unidirectional flow
  • One source of truth. No more flags, arrays, variables...everywhere
  • No more callbacks to check if the view is not null or attached
  • Debug your code like a Pro
  • Easy to tests.
  • If you love Rx. To use corutine here...weeeell you can...

Why you should not use

  • Model your app like a state machine, it is harder
  • Boilerplate, a simple boolean change needs an action, add a new entry in your reducer and take care about it on yout render method
  • Heavy memory use. We do not update the state, we create a new one every time we need a modification
  • Heavy render method...(Jetpack compose maybe helps)

Want to contribute? Great!

mvi's People

Contributors

sasij avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.