MVP-Understanding the boilerplate Code
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
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.
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 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.
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
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.