MVP on steroids - how we force the robot to write code for you

Alexander Blinov
red_mad_robot mobile
3 min readOct 21, 2016

In a previous article, “Android with no Lifecycle: MPVsV approach with Moxy library” we spoke about how to overcome the life cycle problem and divide the code for your Android project into layers. However, an MVP (even using the smart code generation view state in Moxy) requires writing a huge amount of extra code (creating classes and interfaces connected with each other with presenter and view). This is the work a robot should do for you! :) It’s time to reduce the boilerplate even more using Android Studio customized templates under Moxy.

The structure of the project

In order to start the code generation, it is necessary to fix the structure of the project.

—model

— presentation

— — presenter

— — piew

— ui

— — activity

— — fragment

The content of the presenter, view, activity and fragment packets, in turn, is divided into logical modules. Often, these modules are used by sections of the application (like intro, offers, feed). Below is an example of a project structure with two Activities (CarActivity and HomeActivity) and one fragment (CarDetailsFragment)

Our goal is to learn how to generate these classes

Template settings

We suggest you our templates, fill free to customise its

Add the templates for Moxy to the project:

  • Download the templates from Github or link
  • Copy the contents of the archive in ANDROID_STUDIO_DIR / plugins / android / lib / templates / activities
  • Restart Android Studio for the changes to take effect

Configure hotkeys for quick access to the templates:

  • Open Settings-> Keymap
  • In the search box, enter Moxy
  • Add the key combination (I use the Alt + A for Activity and Alt + F for the fragment)

Using templates

  • Select the root package and press Alt + A.
  • In the Activity Name field, write «MyFirstMoxyActivity».
  • Other fields fill themselves out.

Change the Package Name field from blank to the sub-packet name and click Finish. Your classes package is ready! It is worth noting that when you upgrade Android Studio, it can carry all of the custom templates. In this case, it is necessary to import them again.

As a result

The MVP concept involves separating the application’s logic into layers and, consequently, increasing the code base. The use of templates and code generation eliminates most of the boilerplate, protects against accidental errors, and allows you to focus on business logic. Let the robot write the code for you!

--

--