The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Creating a Controller

Let’s take a closer look at the data flow that we are trying to implement here, as part of our Model-View-Intent architecture:

MVI Flow Diagram
Figure 181: MVI Flow Diagram

The terms used in the diagram are from the architecture pattern. Sometimes, those do not reflect the actual classes that are serving in those roles:

We have only two more nouns to implement. Chief among those is the “Controller”, which is responsible for responding to actions published by the “View” and, among other things, updating our “Model” in response. Since we have no particular ties to other names for this class, we can call our “Controller” Controller.

We need to have the Controller respond to actions published by the fragments. The way that we will do that pulls in another popular library: RxJava. RxJava ushered in the era of reactive programming in Android, despite it being a pure Java library, not tied directly to Android at all. We will have our fragments publish actions using an RxJava Observable type, which represents a stream of events. The Controller will subscribe to that stream on a background thread and process those events.

This is a continuation of the work we did in the previous tutorial. The book’s GitHub repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Read Me!

RxJava has a GitHub repository that has a lot of documentation. However, bear in mind that there are two major generations of RxJava: RxJava 1.x and RxJava 2.x. We are using the newer 2.x generation. The RxJava GitHub repository sometimes blurs the line between these generations, so be careful.

Some introductory material on RxJava can be found in a chapter in The Busy Coder’s Guide to Android Development.

Also, a lot has been written about RxJava and its use in Android. However, due to the aforementioned pair of RxJava generations, focus on material written in 2017 onwards. 2016 material that specifically mentions RxJava 2 is also fine. Older material will refer to RxJava 1, and while the concepts are the same, any specific instructions will be incorrect, due to changes in the way that RxJava works.

Step #1: Adding a Stub Class

The preview of this section was eaten by a grue.

Step #2: Accessing the Repository

The preview of this section was lost due to a rupture in the space-time continuum.

Step #3: Depending Upon RxJava

The preview of this section was traded for a bag of magic beans.

Step #4: Subscribing to Actions

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Step #5: Handling Add, Edit, and Delete Actions

The preview of this section is presently indisposed.

Step #6: Connecting to the Controller

The preview of this section is in an invisible, microscopic font.

Step #7: Publishing List Actions

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Step #8: Publishing Edit Actions

The preview of this section is in the process of being translated from its native Klingon.

What We Changed

The preview of this section will not appear here for a while, due to a time machine mishap.