📜 ⬆️ ⬇️

MVP on steroids: make the robot write the code for you

In the article “Moxy - MVP implementation for Android with a pinch of magic” we told how to overcome the problem of the life cycle and divide the code of your Android project into layers. However, MVP (even with smart code generation of the view state in Moxy) makes it necessary to write a huge amount of redundant code (to create presenter and view classes and interfaces connected to each other). In fact, the robot should do this work for you! :) It is time to reduce boilerplate even more with customization of Android Studio templates for Moxy.


Project structure


In order to start code generation, you need to fix the structure of the project.


The contents of the presenter, view, activity and fragment packages, in turn, are divided into logical modules. Often, such modules are sections of the application (for example, intro, offers, feed). Below is an example of a project structure with two Activities (CarActivity and HomeActivity) and one fragment (CarDetailsFragment)

image
')
Our goal is to learn how to generate these classes.

Customize templates


How to create templates in Android Studio can be read in the article from Fi5t about Total Templating

We add templates for Moxy to the project:


Customize hot keys for quick access to templates:


image

Use of templates



Remaining fields will be typed.

image

Next we change the word blank in the Package Name field to the name of the subpackage and click Finish. Your class pack is ready! It is worth noting that when updating Android Studio can demolish all custom templates. In this case, you will have to import them again.

What is the result


The concept of MVP implies the division of application logic into layers and, as a result, an increase in the code base. The use of patterns and code generation frees you from the boilerplate as much as possible, protects against random errors and allows you to focus on business logic. Let the code be written by a robot for you!

Materials on the topic


Source: https://habr.com/ru/post/305798/


All Articles