In Android the MVI Pattern is adopted by Android enthusiast and open source lover Hannes Dorfmann. So,lets go and look what the heck MVI pattern looks like!
The major GUI architectures, MVI is defined fairly loosely. However, at its core, it is a return to the unidirectional data flow that MVC offered… though with few more parts:”
- Intent-Intent is not Intent. The intent simply describes an intention, or action, or command generated by the user as he or she is interacting with the app.An intent represents the interaction with the view by the user. The intent delivers changes that need to be processed by the Model.
- View- The view, actual the UI, observes and depicts the model’s state. The view forwards each interaction as intent.
- Model- Model represents a state (data to be displayed, visibility of your View widgets, RecyclerView scroll position, etc.).This includes interactions with business logic. The business logic updates the current state and thereby the observing View.
If you going going to implement MVI (Model-View-Intent) in your app then instead of starting from scratch you can use mosby library developed by Hannes Dorfmann. Ofcourse it is not mandatory that you should start with a third party library in order yo implement MVI pattern in your app.One advantage of using mosby library is that you get the base classes on which you can build you app.
Differences between MVI and MVP
Model-View-Presenter (MVP) is a software architectural pattern which is based on Model-View-Controller. It is used to implement user interfaces while keeping the model seperated from the view. The interactions are controlled by the presenter which acts as an mediator between model and View.
Model-View-Intent (MVI) is also a derivation of the original MVC architectural pattern. The pattern fulfills the essential purpose of the original MVC as dicussed by André Staltz in his description of MVI. Instead of working with a proactive controller MVI works with the reactive component called intent.
- Seperation between model and view. The presenter acts like a mediator.
- One to one mapping between View and Presenter, with the possibility to use multiple Presenters for more complex Views
- Unidirectional Flow allows us to work without callbacks between the View and in this case the Intent
- Compulsary Usage of RxJava
- Immutable objects
What should you use!
Its completely up to you as you choice could be different according to your need and as MVP is more popular and has a good amount of documentation and tutorials online its easy to work with MVP and in the case of MVI it still don’t have a good documentation and lots of tutorials like MVP But If you are going with MVI the start with Mosby MVI as it offers the benefit that RxJava is already established in its library calls and a loose coupling between UI and business logic is inflicted.