-
Notifications
You must be signed in to change notification settings - Fork 0
(MVI) Model View Intent
Devrath edited this page Feb 19, 2024
·
19 revisions
- To overcome the drawbacks of
MVCcontrolleroverloading with the business logic. Modifications were done to segregate it to a better representation of architecture calledModel-View-Presenter. - It is a relatively new but popular approach to android application architecture.
Contents |
|---|
MVI specific concepts |
| How the concept of MVI came into existence |
| How MVI works |
| Overview representation of MVI |
| Detailed representation of MVI |
| Immutable State |
| MVI - Layers |
- Uni-directional data flow.
- Immutable state.
- User as a function.
- Its way of simplifying the flow of information which leads to a comprehensive and systematic approach to architecture.
- Many of the advancements in recent times comes from the
web-developmentworld. - Some namely
react,redux,flux, useevent-drivenandfunctionalway of solving things. - They give a unique perspective on how to manage
mutable state. - Such representation fits into
mobile development applications. -
MVIis closely associated withcycle.jsfrom the web development world.
- Much of the code you write in
web developmentisuser focusedinstead ofprocess intensive. -
Architecturein android usually focus on the process instead ofuser. The code gives an interface toaccept inputandsend input. - Place of the
userinmvvmormvpis not transparent. Usually, the user is the kind ofviewin these patterns.
- Application starts up and the user starts interacting with it.
- We call user interaction intents meaning users intents on something to occur based on how they interact with the application. They might
type a textorclick a button. - Our software does something interesting using the intents(intention) of the user and models the data and the output is sent back to the user.
- Thus there are
intents as inputandresults as output.
- We interpret the user intents as actions that have to be taken inside our application.
- We perform this translation from intent to action to take data within our application.
- This translation will take users to the repository part, which is network-based, local storage-based or both.
- We use processors to perform actions on our data.
- The processors produce a result.
- At this point, the user intent is translated into a result.
- Now we need to get the results back to the user.
- Here is the idea of the state coming into the picture.
- State is the current status of all the information shown to the user.
- In the High level of Android application state includes
loading state,empty state, orstate of particular data. - As the events occur, the states are continuously updated based on the events that have occurred.
- Instead of maintaining the states as mutable objects in our system, In MVI a new state is calculated for each new event that occurs.
-
NewState=PreviousState+Results - The process of combining the states with results is called MVI in Android.
- The new state is passed along to the android app which renders the UI to the user
- Due to this the state is never mutable, instead, it's recalculated and passed to the view for rendering.
- one of the sources of bugs in software is mutable states. - > Now by using immutable state -> We can eliminate the source of many bugs in software using MVI.
- Displays the objects in the
layout/composable. - It observes the model state using
rxJava/Flow
- The model represents a state, The models in the MVI should be immutable to ensure unidirectional data flow between them and other layers in the architecture
- The
modelis what theviewobserves. The model is observed by the view. - Model also holds the state of the view.
- The
modelreceives theintentfrom theuser(view). -
Intentin MVI means -> The intent to do something, It is not related to the Android component. -
Ex:User actions.