Great Android App Architecture [Part 5 – Finishing up!]

Thanks you people for bearing with me for last 4 articles [ 1, 2, 3, 4 ] This will be the last one in the series. Hopefully! Without wasting any time in chitter chatter, lets get to business.

So as of now, our app displays only one quote. Now we need to make it display multiple quotes (say 10), we have to call the API with second parameter as 10 and make little changes to our data model and repository classes. Before we do it, I want you to imagine kind of changes you would require to do if you don’t use the current architecture which we are using. First of all, you will have to change the API call (which will be ultra easy in our case, thank you Retrofit), then changes in DBHelper classes (again, thanks to Room, it will be easy in our case) and changes in JSON to Java objects conversion mechanism you would have used. Seems like lot of work to me.

To get multiple quotes, we have to convert our LiveData<Quote> to LiveData<List<Quote>>. QuoteAPI interface will stay exactly same as before, only return type of getQuote method will change.

In similar manner, wherever we have written LiveData<Quote> in QuoteRepository.java, QuoteViewModel.java and QuoteDAO.java, we will replace with LiveData<List<Quote>>. Easy peasy! I will not paste code again here for brevity. You can always get source on GitHub.

Only notable difference we are going to make in our data side is in our pojo class Quote.java

We set autogenerate property of PrimaryKey annotation to make sure that multiple quote records will be stored in DB. And we are done with our changes in data side.

As far as, our view side matters, only difference is that, now we will be receiving list of quotes instead of single quote. So I wrote simple Recycler View adapter class for displaying quotes. For implementing Recycler View, we need to write view of item in xml and adapter class to fill it with data. I will not be discussing about recycler view implementation here. You can learn more about Recycler View here and here.

Code here is pretty much self explanatory, still if you face any problem in understanding any part, just leave a comment and I will respond as soon as possible. Or you can reach out directly to me via mail.

As you can see, how easily we changed our app from displaying one quote to multiple quotes. Within minutes. Changes in data side and view side were totally independent and nothing to with each other.

Now imagine, tomorrow you are installing your own server and want to use your own REST API for fetching the quote. No matter how complex your app has become in future, how big it has grown or how sophisticated UI you have written for it, if you have kept structure intact, it will be a piece of cake for changing the data source. (Only change in QuoteRepository and QuoteAPI classes in our case!)

I would like to demonstrate one more functionality we will add right now for our app very quickly. We would like if our users can save/star any quote he/she likes with one touch and separate fragment to access that quotes. For that, let’s create one more table in our db for storing starred quotes. Procedure to do so, very easy!

Create another POJO for our saved quote and name it SavedQuote.java. It will be same as Quote.java, only difference being extra parameter for storing timestamp at which quote was starred. I will skip code for brevity. (Refer github for code)
In MyQuoteDB.java, add our new POJO class in @Database annotation in entities. Like this

And we are done with the data part here. How easy it was! I will leave the UI part as exercise for you. Just create one more recycler view item for showing starred quotes, fragment to display them (similar to what we are using for earlier quotes) and button on each quote item to star the quote. And that will be pretty much of it. Invoke the methods we written for saving the quote appropriately on button clicks. After little more UI tweaks, our application looks something like shown below. Decent enough!

Some of you might argue, why you have written all set off different classes for the functionality of starred quotes, we could have managed it in existing classes and stuff. But our goal here is not to create highly optimized, small size apk but maintainable, testable and robust apk. If we want to make changes to any functionality in future, each of those should be independent enough so as to reduce development work and regression testing time.

You can always fork the repository on github to play around the application and make changes if you want. Feel free to reach me in case of any doubt!

I will be writing more articles about tips and tricks for making most of your android application in future, subscribe our newsletter for getting the articles directly in your inbox. Subscription box can be found on top right side of this article of you are on PC and at the bottom of this article if you are on mobile device.