Employing the MVI Pattern for your Android Application

Posted By : Daljeet Singh | 31-Oct-2018

Code Architecture is the way you organize and design the code for your application.It is important to organize and modularize your code into various sub-components as it helps promote separation of concern inside your code.


The need for an architecture pattern

Architecting is especially important as your application code undergoes a lot of changes over a period of time, and these changes can leave your code in a pool of mess if the code is not architectured properly. In addition to this, two of the most commonly faced problems arising out of a lack of proper code architecture are :


  • Testability: Unit testing various parts of your code gets pretty tedious. Also using a Dependency Injection library like Dagger2 to reduce code coupling becomes virtually impossible.
  • Maintenance: Code maintenance and addition/removal of functionalities become quite cumbersome.An increase in the time required to debug and resolve issues arising out of the code.

Because of the aforementioned issues, it becomes essential that we adhere to a coding architecture while writing code for our application. Some of the popularly used patterns for architecting android code are MVC(Model-View-Controller), MVP(Model-View-Presenter), MVVM(Model-View-ViewModel) and MVI(Model-View-Intent).Each of these patterns come with their own advantages and challenges for implementation.


The MVI Pattern

While the MVI pattern is currently not as widely used as the others,it is especially effective if your codebase would implement a significant amount of reactive and functional programming.The MVI pattern for android takes inspiration from the MVI pattern employed by Cycle.JS(a framework for creating reactive applications in JavaScript).The code in a MVI architecture is layered between 3 core abstractions :

  • Intent : An Intent describes the intention of the user while interacting with the app.The Intent receives the input from the user in the form of UI events(e.g button clicks,field inputs) and translates it before delivering it to the Model in a manner that can be processed and understood by the Model.
  • Model : The Model in MVI is immutable and responds to the Intent events.Each Model represents a state inside the app and a new instance of the model is created for every change of state triggered by the Intent events.
  • View : The View represents the actual UI of the app.It observes the Model and propagates any changes in the state of the Model to the UI.

The MVI architecture bodes well with the reactive programming approach as the Model can be implemented as an Observer for the Intent and the View can be a Subscriber listening for any state changes in the model and also emit actions for the Intent to act upon.

If you want to start out with your first project using the MVI pattern, the Mosby MVI library might be a good fit for you as it contains pre-written library calls in RxJava and employs loose coupling between the UI and logic. Although there is not a lot of community support threads and documentation for MVI right now ,it provides an efficient way of organizing your code particularly if your code utilizes the reactive programming paradigm.



Request for Proposal

Recaptcha is required.

Sending message..