Disclaimer: The title is the slogan of the framework I’m presenting on this article. I have no intention to shade or diminish any other great framework that may exist. I just want to share my experiences as a mobile developer with you.

It’s been a year now since I decided to widen my programming skills to reach Android Development and as I learned I realized that there is a lot of repetitive job needed in order to accomplish simple things like load data from databases, show it on views, get the input, save it or pass it through different Fragments/Activities.

When you are a newbie it’s nice to do those things by yourself and have full control of what’s done and how it is made. But … as your skill in the platform increases, as well as your challenges on it (you have to make larger and more complex apps) this starts to feel a bit redundant and annoying.

Eventually there comes the time when you get to ask yourself…

Could there be a way to reduce the amount of code written to make this kind of stuff? Let’s say “loading data from a table and injecting its content into a layout” with few lines of code, like:

@Model(query = “Id = 1”) // Get the data from the database

@Populate // Show it on the view (layout)

User_ user; // Yep… I meant some user info

Well … actually, there is :).

Declex

Declex is a framework that combines the use of great libraries such as AndroidAnnotations, ActiveAndroid, EventBus and modern programming techniques like Dependency Injection and Selectors, precisely to fulfill this need.

The previous code is an example of it and works (of course) with the existence of a “User model” and a layout that should have its TextViews Ids starting with a “user_” prefix (e.g. “user_FirstName”, “user_Age” or “user_Role”).

And yes… this is the simplest of things that can be achieved with it. In fact, using the Framework not only makes smoother the interaction with the views, but also handles the requests made to the Server APIs through the use of OkHttp3(for network requests) and GSon(for JSON handling).

Very short video demonstration of Declex code reduction capabilities.

But before going deeper into the amazing capabilities of the Framework let’s take a quick look at how it works.

Deus Ex Machina

One of the main principles of Declex is the generation of a class extension of your current Fragment/Activity. In this way the extended version will have all the code you’ll simply get bored writing (like findViewById, Inflate, setText, etc. ) allowing the original F/A to look much more clean, compact and organized.

This is great for the develop and maintenance of the code but at the same time it doesn’t hides any of what’s done, because you can always access the extended version of the F/A and look at what the Framework is doing for you. So yes, for those “control freaks” (like me), that need to know everything that’s going on in the app to be calm and comfy… you won’t have anything black boxed in here.

What’s explained before is mostly used in 3 of the 4 core features of the Framework which are:

Populating/Recollecting data: Fill the layout views with the models data or get the data entered in the views into the models. Models: Which are the data holder to allow the framework interaction with the database. Events: Generated interaction classes made to enhance the app internal communication (I’ll be talking more about this in further articles).

But what about the fourth one? Well Declex also generates something that could be described as utility functions or as the author baptized them: Actions.

Actions are portions of code generated to speed up the writing of recurrent tasks like: calling another Fragment/Activity and passing them arguments, building an AlertDialog and then reacting to the user’s answer (if needed), and forcing the population and recollection of data towards/from the views, among other things.

It’s important to highlight that when you develop with Declex you are not forced to use all the Framework features in order to have everything going ok. You can merge the raw code with the generated one as you like and it will still be working like a charm. This gives you a lot of room not only during the learning process but also when you face specific tasks that you feel more comfortable in doing by yourself.

That’s enough explanation for now… let’s start with the cooler stuff.

The power of population

Suppose that you need to Populate a ListView with all the users in a database and show their full name, birthday and an icon according to the user sex.

For the clarity of the example let’s write the user model first:

public class User extends Model{

@Column

String Name; @Column

Date Birthday; @Column

boolean Gender; public string getDateString(){

return new SimpleDateFormat(“dd/MM/yyyy”).format(Birthday);

} public int getUserIcon(){

if (gender)

return R.drawable.user_female; // Ladies first! ^^

else

return R.drawable.user_male;

}

}

Next you’ll need a layout with a ListView that has its id set to “usersList” and a item_users layout with two TextViews and one ImageView identified as “usersList_Name”, “usersList_getDateString” and “usersList_getUserIcon”.

Ok… let’s see how much effort takes to Populate all the existing users into this ListView using Declex:

1 : @Model

2 : @Populate

3 ;) List<User_> usersList;

That’s it :).

The Framework deduces that what you need to populate is a ListView called “usersList” because of the field datatype (List) and then injects the content of the model into each of its items.

There is a lot you can do here without even needing an adapter class, like not loading the data at first using @Model(lazy = “true”) or pulling only users who’s names start with M using @Model(query = “Name like ‘M%’”).

But if you really needed it (the adapter) you can always use the @AdapterClass(NameOfAdapter.class) annotation, and it will, still, inject the data for you… I bet you think that’s cool.

As you can see there isn’t a direct reference to the User model but to a User_ class extension (generated by Declex). This extension allows not only to inject text and images into the views but also make layout juggles like changing the visibility of a view, it’s size or anything you can imagine.

To accomplish this you need additional methods in the model that look like this:

public void Name(Textview view){

if (gender)

view.setTextColor(context.getColor(R.color.pink));

else

view.setTextColor(context.getColor(R.color.blue));

}

And just like that you have separate colors for the names of males and females on your list.

Well folks that would be all for this article. I’m thinking of making a series of this, in order to present all the Framework features. There is still a lot of stuff to show that I think are great and would be nice to share with you.

If you are interested in the Framework you can get all the info you need on its Wiki here or read a more in depth article made by the author here.

Hope you liked the article. In the next one I’ll be talking about Actions and Events and how they mix-up with the Population/Recollection mechanism.

Leave your comments below.