Chapters

Hide chapters

Advanced Android App Architecture

First Edition · Android 9 · Kotlin 1.3 · Android Studio 3.2

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

18. MVI Sample
Written by Aldo Olivares

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter you learned all the theory behind the Model-View-Intent architecture pattern. You learned how MVI works and how each of its layers interact with each other.

Now you are going to use your new knowledge to rebuild the movies app using MVI. Along the way, you will also learn:

  • How to create an RxJava Observable from scratch.
  • How to create RxJava Observables from previously created objects with methods such as just().
  • How to use a PublishSubject.
  • How to use a Disposable and a CompositeDisposable.
  • How to integrate Room with RxJava.
  • How to integrate Retrofit with RxJava.

Ready? Lets get started.

Getting started

Start by opening the starter project for this chapter. The project contains the following packages:

  • data: contains the Room database components such as your DAOS and your MovieDatabase, your Models and your RetrofitClient.
  • domain: contains the MovieState class which you will use to represent the state of your App.
  • view: contains the activities and adapters.

Take some time to familiarize yourself with the code because you will be spending a lot of time with each file during this chapter.

Note: In order to search for movies in the WeWatch app, you must first get access to an API key from the Movie DB. To get your API own key, sign up for an account at www.themoviedb.org. Then, navigate to your account settings on the website, view your settings for the API, and register for a developer API key. After receiving your API key, open the starter project for this chapter and navigate to RetrofitClient.kt. There, you can replace the existing value for API_KEY with your own.

Build and Run the App to verify that everything is working properly.

Right now it is just an empty canvas, but that is about to change.

Going Reactive

One of the advantages of working with popular libraries developed by reliable sources such as Google is that they often include support for other popular libraries from the Android community such as RxJava.

Creating Interactors and State

Room and Retrofit are now returning Observable callbacks, but they are only emitting plain old objects such as Movie or Long. In the last chapter you learned that one of the main concepts of MVI is that you need to represent the State of your App such as Loading, Error or Data based on your models. You will use an Interactor as a way to interact with Retrofit and Room and to transform the responses into the corresponding State.

Creating the Presenters

To connect the View with an Interactor you are going to need a Presenter for each of your activities.

Creating the MainPresenter

Create a new package under the root directory named presenter. Inside this package create a new Kotlin class named MainPresenter.

Creating the AddPresenter

Create a new class under the presenter package and name it AddPresenter. Replace everything inside with the following:

Creating the SearchPresenter

Create a new class under the presenter package and name it SearchPresenter. Replace everything inside with the following:

Creating the Views

Now that the Presenter layer of your MVI architecture is ready, creating the Views should be a piece of cake :]

Creating the MainView

Open the MainActivity.kt file under the view/activity package. Right now this is an empty activity with a toolbar and a simple method to navigate to the AddMovieActivity once the user presses the + button, but that is about to change.

Creating the AddView

Open the AddMovieActivity.kt file under the view/activity package.

Creating the SearchActivity

Open the SearchMovieActivity.kt file under the view/activity package. Make your SearchMovieActivity class implement the SearchView interface and implement all the missing members.

Final thoughts

Take a closer look at all your Activities. As you can see, there is only one render() method that receives the current State from the Presenter and multiple Intents that represent the actions taken in your UI.

Key points

Where to go from here?

For didactic purposes, you created most of your Observables for this App from scratch, but there are many libraries out there that help you create Observables from almost any kind of object.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now