Skip to content

Instantly share code, notes, and snippets.

@MariusBudin-zz
Created September 1, 2015 09:57
Show Gist options
  • Save MariusBudin-zz/9202a25e303a37f240b8 to your computer and use it in GitHub Desktop.
Save MariusBudin-zz/9202a25e303a37f240b8 to your computer and use it in GitHub Desktop.
Some simple Rx bindings on primitives based on android.databinding library. This allows you to apply, maps, filters and all the goodies in rxJava/rxAndroid on simple primitive data observers.
import android.databinding.ObservableBoolean;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Me sobren paraules
*/
public class RxBoolean extends ObservableBoolean implements RxValue<Boolean> {
private BehaviorSubject<Boolean> valueSubject = BehaviorSubject.create();
public RxBoolean(boolean value) {
super(value);
}
public RxBoolean() {
super();
}
@Override
public void set(boolean value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Boolean> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Boolean value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableByte;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Vehicle Lunar
*/
public class RxByte extends ObservableByte implements RxValue<Byte> {
private BehaviorSubject<Byte> valueSubject = BehaviorSubject.create();
public RxByte(byte value) {
super(value);
}
public RxByte() {
super();
}
@Override
public void set(byte value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Byte> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Byte value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableChar;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Vehicle Lunar
*/
public class RxChar extends ObservableChar implements RxValue<Character> {
private BehaviorSubject<Character> valueSubject = BehaviorSubject.create();
public RxChar(char value) {
super(value);
}
public RxChar() {
super();
}
@Override
public void set(char value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Character> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Character value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableDouble;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Vehicle Lunar
*/
public class RxDouble extends ObservableDouble implements RxValue<Double> {
private BehaviorSubject<Double> valueSubject = BehaviorSubject.create();
public RxDouble(double value) {
super(value);
}
public RxDouble() {
super();
}
@Override
public void set(double value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Double> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Double value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableField;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Me sobren paraules
*/
public class RxField<T> extends ObservableField<T> implements RxValue<T> {
private BehaviorSubject<T> valueSubject = BehaviorSubject.create();
public RxField(T value) {
super(value);
}
public RxField() {
super();
}
@Override
public void set(T value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<T> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(T value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableFloat;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Vehicle Lunar
*/
public class RxFloat extends ObservableFloat implements RxValue<Float> {
private BehaviorSubject<Float> valueSubject = BehaviorSubject.create();
public RxFloat(float value) {
super(value);
}
public RxFloat() {
super();
}
@Override
public void set(float value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Float> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Float value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableInt;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Me sobren paraules
*/
public class RxInt extends ObservableInt implements RxValue<Integer> {
private BehaviorSubject<Integer> valueSubject = BehaviorSubject.create();
public RxInt(int value) {
super(value);
}
public RxInt() {
super();
}
@Override
public void set(int value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Integer> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Integer value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableLong;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Vehicle Lunar
*/
public class RxLong extends ObservableLong implements RxValue<Long> {
private BehaviorSubject<Long> valueSubject = BehaviorSubject.create();
public RxLong(long value) {
super(value);
}
public RxLong() {
super();
}
@Override
public void set(long value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Long> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Long value) {
valueSubject.onNext(value);
}
}
import android.databinding.ObservableShort;
import rx.Observable;
import rx.subjects.BehaviorSubject;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Alegria
*/
public class RxShort extends ObservableShort implements RxValue<Short> {
private BehaviorSubject<Short> valueSubject = BehaviorSubject.create();
public RxShort(short value) {
super(value);
}
public RxShort() {
super();
}
@Override
public void set(short value) {
super.set(value);
valueSubject.onNext(value);
}
@Override
public Observable<Short> valueObservable() {
return valueSubject.asObservable();
}
@Override
public void valueChanged(Short value) {
valueSubject.onNext(value);
}
}
import rx.Observable;
/**
* Created by marius on 31/8/15.
*
* @music Antonia Font - Alegria
*/
public interface RxValue<T> {
Observable<T> valueObservable();
void valueChanged(T value);
}
/**
* Created by marius on 1/9/15.
*
* @music Beware of safety - Kaura
*/
public class RxValuesSampleViewModel {
public RxBoolean enabled = new RxBoolean();
public void toggle() {
enabled.set(!enabled.get());
}
[...]
}
public class RxValuesSampleView {
private final RxValuesSampleViewModel viewModel = new RxValuesSampleViewModel();
[...]
public void bind() {
viewModel.enabled.valueObservable().
subscribe(button::setEnabled);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment