MVI Architecture for Android
Understanding the architecture
Architecture, it is a word every developer goes through a lot in their lifetime and we are always in search for the best Architecture, but it is a question for everyone which is best, which to follow and why is it best
So one thing I learned over these past few years is that there is technically no BEST architecture. The ultimate goal of every developer is to code simple, understandable and optimised.
Below are the few architecture patterns widely used in Android.
- Model View Presenter(MVP)
- Model View View Model(MVVM)
The latest addition to these patterns is Model View Intent(MVI).
Overview of MVI
The Model View Intent (MVI) architecture is a pattern that aims at organizing the UI of your application to make its development simple.User interaction with the UI is processed by business logic which brings change in the state. The UI may have several states such as loading, error, displaying data, displaying empty view, This new state is rendered on view and this newly updated view is shown to the user
WHY MVI?
MVI helps in Solving the problem of State Management by making our Model as State
To understand problem of State Management lets take an example:
Consider a screen which contains a list of data, progress bar, error text and a pull to refresh.
When a user presses a button,
- The View first sets progress bar loading to true and calls ViewModel to getData()
- ViewModel makes call to the DataSource to get the list of data.
- Data source returns data to ViewModel which then tells the view to showData() by sending the list and also sets progress bar loading to false
But now consider the same scenario if user performs a pull to refresh on the screen, the same flow again repeats but now one thing to note is that View sets refreshing to true rather than progress bar but ViewModel still tells the view to ShowData() and hideLoading()
In this scenario, the hideLoading() method is not required as there is no loading indicator instead there is a refreshing indicator in the view. This is a common issue that happens with most of the developers as there are lots of states to handle and we may get lost in those.
In Order to solve this issue, MVI Proposes to use
MODEL AS STATE
In MVI, models are arranged as the container of states. Every time a new immutable model is created which is then observed by the view
SINGLE SOURCE OF TRUTH
In MVI the state is created at one level (Presenter/ViewModel) and passed to another level (View). The only way to change the state is by firing an Interaction by the user which is called an Intent. This approach restricts any change to the State of the system only via a defined set of actions. So at any point of time you can be sure there will be a single state between all the layers in your app and so state is the single source of truth for UI
CALLBACKS
Normally, views are updated through callbacks following the asynchronous call. The view may be separated while the asynchronous call returns the outcome. But by leveraging architecture components we may overcome this issue by LiveData which is lifecycle aware
FINDING CRASHES BECOME EASIER
With the State(Model) it becomes easier to trace crash at any given point. Even with crash reports you may be able to see where the crash was but with MVI you will know the view’s state flow before crashing so it becomes easier to reproduce
ONE VIEWMODEL WITH MULTIPLE VIEWS
MVI also helps in using the same ViewModel with multiple views by leveraging the State(Model) for multiple UI components and thus provides a way for organizing the data. With MVVM approach, we may end up with multiple LiveData objects for different data and it can be very cluttered to handle all those.
Conclusion
All these points added together I feel MVI is a strong architecture pattern and it can help us in organisation of our data in a fully closed way.
This is my understanding of MVI. If you know more about this architecture you can let me know in the comments section below. Also do clap if you liked my small contribution in understanding MVI ;)