CMB Android, MVP, and Android TV

Brian Fang
Coffee Meets Bagel Engineering
4 min readMar 22, 2017

Hi! My name is Brian and I work as a part of the CMB Android team. Recently at CMB, we’ve been refactoring our code to conform to our form of the Model View Presenter (MVP) paradigm. This ongoing project, which requires a major overhaul of our current architecture has been both fun and challenging. In this post I’ll be detailing one of the benefits we’ve seen since transitioning to an MVP model in our codebase.

Our Philosophy

Our team lead, Jose, has set forth three criteria that ensure quality when refactoring our code to MVP:

1. Is it the responsibility of the class to perform that function?

2. Does something about the method signature reveal how things are implemented underneath? i.e. Is it leaking implementation details which a client might then depend on?

3. Does this class allow somebody without the right context to modify state that I own in an unsafe way?

Very simply put, we want to ensure that each object is delegated the correct responsibilities. We design each module in a way that we can swap out a module for any other at any given time. For example, our persistent storage API should not reveal to its clients that the implementation uses Android’s SQLite database. Our presenters should have no idea that the views they are controlling are Activities or Fragments. Under this paradigm we can easily switch from a phone screen to a TV screen (switching the view, but not the presenter).

This architecture makes our code base extremely flexible and easy to test. Let’s say we have the crazy idea of storing all of our data within a file instead of a SQLite database. Since we have all of our persistent storage logic flowing through a singular class, we can simply swap out our persistent storage API class and the changes will propagate throughout the app. All clients will have no idea that the data source has changed, yet they will be able to use it as before.

Porting our App to Android TV

I am, by no means an expert on Android TV, but for those unfamiliar with Android TV development, the basic structure is the same. Following the Android app paradigms, your Android TV views consist of Activities and Fragments. The challenge, however, is being able to show a different UI for a TV screen versus a mobile device while keeping your code base device-agnostic and reusable.

The key word here is “while”. It’s fairly simple to show a different UI for different screen sizes thanks to how Android has built in target resource directories based on the screen dimensions of a device. Maybe not so simple a task, but feasible, is keeping a clean and manageable code base while maintaining the same functionality.

Traditionally, (pre-MVP) our logic is mixed in within Activities or Fragments. Cursor Loaders are an example of a tightly coupled dependency making the above goals more difficult to achieve, but that is for another discussion. In our case, instead of only showing the UI in a different fashion, we also want our TV experience to behave differently than our mobile app. The way I’ve solved this in other code bases I have worked with in the past is to use conditionals to differentiate between TV or mobile versions. Using this methodology, we can accomplish our first requirement of showing a different experience for our TV app, but would be making our code extremely messy and unmanageable.

How are things different with MVP? Since our views are decoupled from our business logic, we can very easily use the same business logic to control TV specific views, yet have a different visual experience. Just how fast is doing this kind of change, you ask? It only took us a couple of days to get a fully functioning prototype up and running.

Needless to say, our engineer-designed TV UI isn’t going to be featured anywhere anytime soon (and that’s why we have extremely talented in-house designers), but we were able to leverage what we already had to iterate very quickly to expand the experience we provide onto the TV app space.

Coffee Meets Bagel — Android TV

So what does this Android TV app look like? How is it different from our mobile experience? At Coffee Meets Bagel, we are coming to realize that people always want more bagels. Since we delight in pleasing our users, we have rolled out features like our Hybrid Model and Discover Filters to try and satisfy these needs. Our vision for the Android TV experience is to allow a user to browse their Discover Bagels and use the Discover Filter feature on the big screen.

Discover Bagels
Discover Filter Search
Discover Filter Results

Stay on the lookout for updates!

--

--