Since November of 2014 I’ve been reading and experimenting some clean architecture approaches of Android code. After trying couple of those approaches I finally decided to implement my own version of it. The initial goal was the creation of interfaces and abstract java classes that would rely my future projects on. Those “helpers” classes would be inside a dependency so I could simply import them into any projects I would work from now. The name of the dependency is nothing original: Base. I called it that way because I’m used to prefix my abstract classes with the word Base (e.g BaseActivity, BaseFragment, BaseWhatever, etc).
The code is available here.
The view logic resides here. The Model-View-Presenter pattern was used to keep all the presentation logic into presenters and away from the fragments and activities (which were considered only views).
One of the links between Base and the Android framework is inside the BaseView class, which connects views with Android Fragments. The views are dumb, meaning that they don’t know how to process any data at all.
The presenters are the connection between the presentation layer and the domain layer. It calls use-cases that are performed in background, outside the UI Thread, and returning what was processed through a callback.
Nothing fancy. Just regular POJOs.
This layer might have all of you business rules.
The BaseUseCase implements the java Runnable, which were made to be processed in the background by any kind of Executor, responding the results in the UI Thread, which forced me to have a connection of this class with the Android framework because of Looper.getMainLooper() of Android (shame on me).
The BaseMapper is a class that I use to transform data, mostly Entities into Models. No big deal here.
The idea is adding common features that I identify in the projects as generic use cases, views or presenters.
Load Data Collection
A very simple thing that I’m use to do is to create a view that shows loading state while I collect a list of data from somewhere, showing the results later on in a list. This feature is available through LoadDataCollectionView, LoadDataCollectionPresenter and LoadCollectionUseCase.
- The very first “trending” thing I’ll do is creating a RxPresenter. It will react to any subscribed UseCases from the domain layer.
- The maintainance of the views according to the fragments/activities lifecycle.
Base is not a framework, it’s a library. You don’t need to follow the exact architectured described in this post if you don’t want to. You can import its modules separately as you prefer and used them with your own pattern. Is important saying that as the time passes, things evolve. I’ll do my best to contanstly evolve this code (and it will be better if you help me out).