Introduction to MVP in SUSI Android App
Recently in SUSI Android app, we started shifting to MVP Design Pattern. We have shifted some part of it and some part is remaining and will be shifted soon. In this blog post, I am not going to tell you about “What is MVP”. Neither will I talk about advantages and disadvantages of using MVP design pattern over other design patterns. There are many good blogs by very experienced Android Developers about the same on the internet (Some good resources at the end of this blog). Rather in this post, I will talk about how to shift your already written app code to MVP design steadily without breaking the app. Also if you are willing to contribute in SUSI Android app, this post will guide you a little about how the things are been implemented and explains the code a little. Brief Intro I will not go into detail about MVP Design but just a formal introduction and explanation of some terms that will make you understand the blog post better. If you want to go into details, I have added resources at the bottom, read those. So, MVP stands for Model View Presenter. Initially when you write code for an Android app, what you do is make an XML file storing all views and design and a Java Class storing everything else, which may include sending a request over the Internet, fetching through databases, updating views, etc. But in MVP design, the logic for getting data like network request and UI updates like showing the data is separated. As the name suggests, MVP has three components: Model: A Model is a place where the code for all the business logic is written. It includes making network calls, fetching data from the database etc. View: All the UI updates like showing data, showing dialog boxes and errors, taking inputs from click listeners, animations etc are done here. Presenter: This acts as a middleman between View and Model. Takes input from View, gives it to Model. Takes data from Model and gives back to the View. The Process goes like this I have made this schematic to explain things better. Implementation Let’s take the example of Login functionality and go step by step to convert it to MVP Design Pattern. Requirements Before starting, make a new package named “login” and add following 6 files in it: ILoginView -> Interface ILoginPresenter -> Interface ILoginModel -> Interface LoginActivity -> Class LoginPresenter -> Class LoginModel -> Class Explanation Let’s see what the above files do. Just remember, the main aim of MVP Pattern is to keep business logic separate from UI Updates. LoginActivity -> Takes input and Makes UI Updates. Asks LoginPresenter to do word using ILoginPresenter Interface. LoginPresenter -> Acts as a presentation layer which does all checks on data and invokes LoginModel using ILoginModel interface and in return asks LoginActivity to update UI using ILoginView interface. LoginModel -> Stores all business logic like making network calls and fetching data. Notifies LoginPresenter about…
