RxProperty

Build Status

RxJava binding APIs for observable fields and observable collections from the Data Binding Library

Usage

A main feature of this library is to provide some extension functions asObservable() to model the changes of observable fields and observable collections via RxJava's Observable. The library is specially helpful for the cases of implementing dependent properties. For example, assume that we have a PersonViewModel that specifies firstName and lastName. Both are represented by ObservableField<String>. There is another fullName that is computed based on firstName and lastName. Whenever one of the 2 properties is updated, fullName should be updated accordingly. By utilizing asObservable() functions, an implementation can be:

class PersonViewModel {
  val firstName = ObservableField<String>()
  val lastName = ObservableField<String>()
  val fullName = ObservableField<String>()

  init {
    Observable.combineLatest(
      firstName.asObservable(),
      lastName.asObservable(), {
      firstName, lastName ->
      if (firstName.isNullOrEmpty() && lastName.isNullOrEmpty()) {
        return firstName
      } else if (firstName.isNullOrEmpty()) {
        return lastName
      } else if (lastName.isNullOrEmpty()) {
        return firstName
      } else {
        return "${firstName}, ${lastName}"
      }
    }).subscribe { fullName.set(it) }
  }
}

That is achieved because when we subscribe to an Observable created by asObservable(), the Observable will emit the current value of ObservableField<String> and also emit any future changes. But what if we are only interested in observing future changes without the current value? Easy, just skip(1), for instance:

val onFullNameChanged = fullName.asObservable().skip(1)

As of now, the library only supports ObservableBoolean, ObservableField, and ObservableList due to our current need. In the future, we may add more asObservable() functions for other kinds. That said, we welcome any external contribution. Pull requests are very highly appreciated.

Become a better Android Developer
Millions of developers are learning at MindOrks

Online Training Program

Featured Blogs

Our Team

MindOrks is Certainly one of the best online blog to stay on top of all the Android development news, coding and design patterns. Finally a blog I can count on to keep myself updated with latest and greatest things happening in Android world.

Vipul Shah
Android Developer
Microsoft

Become Pro in Android by watching videos

OUR LEARNERS WORK AT