MVP-Understanding the boilerplate Code

Anamika Tripathi
3 min readJun 18, 2018

--

This is a fifth week blog made during GSoC’18 period under Systers Organization. Read about basics of MVP part 1. I’m shifting a existing project Malaria-app android from Systers to follow MVP.

The entire app is packaged into four parts: 1. data 2. services 3. ui 4. utils

infrast

Step 1: Shift all the existing java code to a new directory.

I shifted all code i.e activities, adapters, fragments, interface, models, notification & receivers to a new directory to ease the purpose.

shifted directories

Step 2: Create data directory & it’s sub-directory.

Since, Data handling in the proper way is the toughest job to do, I started with creating boilerplate for it.

data directory

Data contains three major directories & two others files:

  • db: It’s for sqlite database which will be using Room library for implementation. Inside db, I’ve create sub-directories for entities, dao, AppDatabase( to create database using Room database), DbHelper(an interface which will contain all the db functions to implemented) & AppDbHelper( It’ll override all functions are DbHelper).
  • prefs: It’s for handling preferences in the project. PreferencesHelper is an interface which will contain all the function to be implemented & AppPreferencesHelper implements the functions.
  • network: I haven’t created any classes/interfaces but It’ll contain code for handling network calls in the application.
  • DataManger: An interface which contains all the functions & extends interfaces from sub-directories i.e db, prefs, network.
  • AppDataManager: A class which will implement DataManager interface. It’ll be used throughout the project for any access to the data.

Step 3: Create ui directory & it’s sub-directory.

sample ui directory

Currently, ui directory contains only one directory i.e it only handles one screen(userprofile).

In future, It’ll have one directory for each screen in application. base directory contains base code for all the project.

Step 4: Create base sub-directory in ui directory

base directory

It contains five files:

  • MvpView, MvpPresenter both are interfaces & contains code which must be implemented by BasePresenter & BaseActivity.
  • BaseActivity, BaseFragment will be inherited by every activity/fragment in the project.
  • BasePresenter will contain the functions that must be present in all presenter in the project.

That’s all for boilerplate code for MVP architecture. You might not understand the need of so many interfaces now but It’ll be clear with upcoming blogs on MVP. Stay Tuned!

Thanks for reading! Be sure to click claps below to recommend this article if you liked it.

You can connect with me on Github, Twitter, Linkedin :)

--

--

Anamika Tripathi

Android Developer @Zomato | Google Udacity Scholar | GSoC19 Mentor @Mifos | GSoC18 student @systers_org | GCI Mentor | Tech speaker