Android MVVM Architecture Components using The Movie Database API.

Jaewoong Eum
Sep 2, 2018 · 4 min read
The Movie Database

This posting is about how to implement android project based on mvvm architecture using the movie db API. I wanted to implement a simple example project using an open API. But thankfully, we can use movies API for free at the movie db. So I used that and made public a simple project.

This simple project is based on MVVM architecture. And written Unit Test code about business logic.

  • 100% kotlin, based with anko
  • Architecture Components (Lifecycle, LiveData, ViewModel, Room Persistence)
  • DataBinding
  • Material Design & Animations
  • The Movie DB API
  • Dagger2 for dependency injection
  • Retrofit2 & Gson for constructing the REST API
  • BaseRecyclerViewAdapter for implementing adapters and viewHolders
  • Mockito-kotlin for Junit mock test
  • etc..

How to build on your environment

tmdb_api_key = YOUR_API_KEY

Development Process

development process
  1. API Service -> API Service Unit Test with API response mock files
  2. DAO -> DAO Unit Test
  3. Repository -> Repository Unit Test
  4. ViewModel -> ViewModel Unit Test
  5. DI & Refactoring
  6. Implementing UI & Layouts

Architecture

architecture

1. Configuring the ViewModel

TvDetailViewModel requests dependency injection using @inject annotation. The constructor receives only a repository, So ViewModel does not know about actual business logic like how-to-get or how-to-save. It makes easier unit testing to ViewModel. And ViewModel just publishes data or get data from UI components using LiveData. LiveData holds data from the repository(fetching from the network or getting from DB), it makes easier implementing landscape screen mode. And next, we should create a ViewModel in activities or fragments. but how should we do? ViewModelProvider.Factory makes it really easier.

AppViewModelFactory

And next, we can create the ViewModel just by three lines with Dagger!

MainActivity Example

Inject instances using AndroidInjection.inject(this).
then viewModelFactory who annotated with @inject will be injected by Dagger. next, declare ViewModel using ViewModelProvider. Wow! we created a new ViewModel!

2. Configuring the Room Persistence

The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Movie Entity Model

Firstly, we should create an entity model with @Entity annotation.

MovieDao

And next, we should create DAO(Database Access Object) with @Dao annotation.

AppDatabase

Finally, create Database class extends RoomDatabase with @Database annotation. It’s all! and we can use it right now.

3. Configuring the Repository

DiscoverRepository

@Singleton annotation makes DiscoverRepository can be injected by Dagger. And requests dependency injection using @inject annotation on the constructor. So we can get network service and DAO singleton instances without initializing by yourself.

4. Unit Testing

MovieRepositoryTest

mockito-kotlin include original mockito library. And it has some nice expression about verifying like whenever than `when`. I did Unit Testing about Api Services, DAO, ViewModels, Repositories or etc.

5. Implementing Circular Revealed Animation

ActivityExtension

Circular revealed animation can be used over API 21. So we have to check the device version before using. As you can see, requestGlideListener extension is for use with Glide.

Glide listener

That’s all!

If this posting was helped, please give a star at GitHub!

Jaewoong Eum

Written by

A software engineer: Android, IoT. Interested in Psychology, Magic Tricks and writing poems. https://github.com/skydoves

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade