An open API service indexing awesome lists of open source software.

https://github.com/skedgo/rxproperty

RxJava binding APIs for observable fields and observable collections from the Data Binding Library
https://github.com/skedgo/rxproperty

android data-binding kotlin rxjava

Last synced: 12 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# RxProperty
[![](https://jitpack.io/v/skedgo/RxProperty.svg)](https://jitpack.io/#skedgo/RxProperty) [![Build Status](https://travis-ci.org/skedgo/RxProperty.svg?branch=master)](https://travis-ci.org/skedgo/RxProperty)

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](https://github.com/ReactiveX/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`. 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:

```kotlin
class PersonViewModel {
val firstName = ObservableField()
val lastName = ObservableField()
val fullName = ObservableField()

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` 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:

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

As of now, the library only supports [`ObservableBoolean`](https://developer.android.com/reference/android/databinding/ObservableBoolean.html), [`ObservableField`](https://developer.android.com/reference/android/databinding/ObservableField.html), and [`ObservableList`](https://developer.android.com/reference/android/databinding/ObservableList.html) 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.