All posts
Android, Blog

How to handle Featured and MVP in simple Android app

Marcin Mazepa

Marcin Mazepa

Introduction

During my company workcation in Bali, I noticed that the main mean of transport in Indonesia are scooters, they are used by everyone and I mean it, EVERYONE. They are so popular, that they almost replaced public transportation. With so high number of scooters, there have to be a lot of them needing repair. It inspired me to write a small app for scooter renting companies, that will help them to keep up with broken vehicles.

While programming the app, I’ve decided to try fresh library Featured and connect it with MVP pattern. This simple library allows splitting views into smaller parts called features. These part can handle events connected to specified feature. Let’s look how to implement a simple feature. This is our login screen:

device-2016-09-05-083352

Features

We must create a base feature with all events to all components. Simply extend Feature class and add host as parametrized type. Our feature name is LoginFeature so generated host class will be named LoginFeatureHost. I’ve added onCreate() lifecycle method to pass all needed fields such as LoginPresenter (because of MVP) and initialize other thigs like ButterKnife.

Other two methods: onLoginClick() and showLoginError() are meant to be used in LoginViewFeature. In this class, we simply extend LoginFeature and override previously mentioned methods. There is also views and string binding, and OnClick listener. The result is an obvious class with few lines of code. Some of the fields (like Presenter in LoginFeature) can be injected in dependency injection tool, but this is not included in the scope of this post.

Now let’s take a look at LoginActivity. There is prepareFeatureHost which is responsible for creating a FeatureHost. Instantiate FeatureHost by registering features to it using with() method. Communicating with features which implement specific methods are done by calling dispatch<Event> method like in line 8.

So, this is how activity looks like, there’s only one view (mCoordinatorLayout), and the other views are isolated and available only in features that truly needs them.

Conclusion

It’s possible to mix MVP with Featured library, and you end up with perfectly (MVP) separated logic and nicely split views.

The post is created by Droids On Roids Team Member.
We would love to take care of your project.

Hire US!

Leave comment

  • Oh Really

    “Forgot pasword?” -not my problem… Lol