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
- Host: GitHub
- URL: https://github.com/skedgo/rxproperty
- Owner: skedgo
- License: mit
- Created: 2017-03-13T09:41:50.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-06-10T13:17:39.000Z (almost 7 years ago)
- Last Synced: 2025-03-24T15:04:32.840Z (about 1 year ago)
- Topics: android, data-binding, kotlin, rxjava
- Language: Kotlin
- Size: 69.3 KB
- Stars: 18
- Watchers: 15
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RxProperty
[](https://jitpack.io/#skedgo/RxProperty) [](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.