ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

The Missing Google Sample of Android “Architecture Components” Guide.

--

Android Architecture Components

Google has recently released the Android Architecture Components, a collection of libraries that help you design robust, testable, and maintainable apps.

This one will really change the way android developers design their apps.

Presentation of Android Architecture Components

However, after carefully reading the Google guide about the Android Architecture Components, I was really upset that they don’t provide us a FINISHED sample of the explained use-case… I even checked the GoogleSamples repository dedicated to Android Architecture Components on Github, but nothing…

Where is the sample ?

So I decided to implement it, and create the related application of this famous guide. You can find the final app in this repository:

I chose to follow as much as possible the guide. I just added those famous (but recommended) libraries :

  • Dagger 2
  • Butterknife & Glide
  • Gson

You can find the complete list of libraries used in this project inside the build.gradle.

What’s this app does ?

This simple app is composed of a single screen. When this screen appears, we will fetch (Retrofit) the Github information of Jake Wharton and persist those immediately in the application storage (Room).

Next, when the screen is re-launched (or recreated due to rotation), we will get those same information, first in Room database and only if necessary, refresh those from the Github Api.

1. Configuring Room

After installing those libraries, let’s create the persistent model. Create the User entity, representing a Github user, that will be persisted (via Room) and get from Github Api (via Retrofit).

User.java

Next, we create the DAO to persist the user into Room.

UserDao.java

LiveData is an observable data holder. It lets the components in your app observe LiveData objects for changes without creating explicit and rigid dependency paths between them. LiveData also respects the lifecycle state of your app components (activities, fragments, services) and does the right thing to prevent object leaking so that your app does not consume more memory.

Because Room can’t persist Date object, we have to create a TypeConverter :

DateConverter.java

Finally, we create the database object :

MyDatabase.java

2. Configuring Retrofit

As I previously said, User object will be used both by Retrofit and Room. So now, we only have to create a Retrofit interface :

UserWebservice.java

3. Configuring the Repository

This component is, in my opinion, the most important of all. It’s inside we will choose which one data sources to use to get the user’s data, depending pre-defined scenarios :

  • Use the Webservice (via Retrofit) the first time user launch the app
  • Use the Webservice instead of database when last fetching from API of user’s data was more than 3 minutes ago.
  • Otherwise, use the database (via Room).

Repository modules are responsible for handling data operations. They provide a clean API to the rest of the app. They know where to get the data from and what API calls to make when data is updated. You can consider them as mediators between different data sources (persistent model, web service, cache, etc.).

UserRepository.java

4. Configuring the ViewModel

Before create ViewModel, we have to create a Factory for the ViewModel that will handle the injection of dependency more “cleaner”.

FactoryViewModel.java

So now, let’s create the UserViewModel that will be used in the Fragment.

A ViewModel provides the data for a specific UI component, such as a fragment or activity, and handles the communication with the business part of data handling, such as calling other components to load the data or forwarding user modifications. The ViewModel does not know about the View and is not affected by configuration changes such as recreating an activity due to rotation.

UserProfileViewModel.java

5. Configuring the Dependency Injection

Because Dagger 2 can be sometimes a pain to understand, I group a lot of separate modules into a single one, AppModule. Before that, let’s create all the classes needed by Dagger to inject all those dependencies :

ViewModelKey.java
ViewModelModule.java
FragmentModule.java
ActivityModule.java
AppModule.java
AppComponent.java

Now we have to enable Dagger into the Application class of our project :

App.java

6. Configuring the Fragment

It’s almost finish ! We have to add the ViewModel into our fragment UserProfileFragment (you can find it’s layout here), subscribe to the LiveData stream and finally update UI when data are received.

UserProfileFragment.java

7. Configuring the MainActivity

Finally, we create the activity (you can find it’s layout here) that contains the Fragment UserProfileFragment.

MainActivity.java

That’s all ! 😅

I know, there is a lot of classes (especially because of Dagger), but we have followed perfectly the separation of concerns, that is at the top of Android Architecture Components !

Feel free to comment and share !

Phil, Founder @ CookMinute & Creative Mobile Freelancer

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (31)

Write a response