droidconIN 2016

The sixth edition of droidconIN

Manas Chaudhari

@manas_chaudhari

Functional MVVM using RxJava and Android Data Binding

Submitted Jul 9, 2016

It is important to write reusable code to increase development speed and for easier maintenance. Although it is quite easy to share model logic across various components of the app, sharing presentation logic isn’t trivial. Some questions that need to be answered:

How to share presentation logic between multiple layouts?

It is very common to present one model in multiple ways.
For example, one layout shows a mini view, while other shows a detailed view. If both views show same information, we should be able to build the second view without writing any extra code

How to compose different functionalities?

Consider three pages which use different combinations of “basic details”, “reviews”, “customization” functionalities.

  • Menu page: List of items with basic details
  • Item details page: Item’s basic details + reviews
  • Customization page: Item’s basic details + customization

I will demonstrate how MVVM architecture allows you to tackle these scenarios. Binding is essential for connecting ViewModels to Views. However, binding values from RxJava’s Observables to Views is difficult to get right and often leads to boilerplate code. We will explore how this problem can be solved using Google’s Data Binding library.

Memory Leaks

Binding values from RxJava’s Observables to Views requires subscriptions. Cleaning up RxJava subscriptions is necessary to prevent leaks. By writing ViewModels in a functional style, we’ll move all subscriptions to the Data Binding layer and thus, remove the need to cleanup subscriptions.

Eliminate Boilerplate for setting up views

By extending ObservableField, we’ll be able to bind RxJava’s Observables using the minimal Data Binding syntax android:text="@{vm.title}", where title is built from Observable. By making use of Data Binding’s BindingAdapter API, we’ll eliminate boilerplate required to setup complex views such as RecyclerView.

<android.support.v7.widget.RecyclerView
          bind:items="@{vm.itemVms}"
          bind:layout_vertical="@{true}"
          bind:view_provider="@{@layout/row_item}" />

Outline

  • Problems in building User Interfaces
    • Mutation causes refresh chain
    • Boilerplate for view setup
  • Solving Mutation using RxJava
  • Eliminate View Boilerplate
    • Problems in binding rx.Observable to Views
    • Solution using Data Binding
  • Architecture
    • Intro to MVVM
    • View Composition using MVVM
      • Static composition using <include> tag
      • Setting up RecyclerView/ViewPager in XML
    • Managing Dependencies
    • Testability
  • Conclusions

Requirements

Familiarity with RxJava and Data Binding

Speaker bio

Graduating from IIT Bombay in 2014, Manas Chaudhari joined TinyOwl and built the consumer Android and iOS apps. He had been driving the consumer product at TinyOwl, leading a team of Android, iOS and backend (ROR) devs. Currently, he builds end-to-end solutions as a freelance software architect/developer. His core focus has always been to make development faster and bug-free.

As a Microsoft Student Partner, he developed Windows Phone apps and conducted workshops in nearby colleges.

Slides

https://speakerdeck.com/manaschaudhari/functional-mvvm-using-rxjava-and-data-binding

Comments

{{ gettext('Login to leave a comment') }}

{{ gettext('Post a comment…') }}
{{ gettext('New comment') }}
{{ formTitle }}

{{ errorMsg }}

{{ gettext('No comments posted yet') }}

Hosted by

droidconIN is an annual conference on Android, part of the worldwide series of events. more