Last active
September 2, 2017 11:25
-
-
Save radzio/3a2aed9b1457617b21c1afde8f4dd928 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <T> Observable<T> toObservable(@NonNull final ObservableField<T> observableField) { | |
return Observable.create(new Observable.OnSubscribe<T>() { | |
@Override | |
public void call(final Subscriber<? super T> subscriber) { | |
if (!subscriber.isUnsubscribed()) { | |
subscriber.onNext(observableField.get()); | |
} | |
final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() { | |
@Override | |
public void onPropertyChanged(android.databinding.Observable dataBindingObservable, int propertyId) { | |
if (dataBindingObservable == observableField) { | |
subscriber.onNext(observableField.get()); | |
} | |
} | |
}; | |
observableField.addOnPropertyChangedCallback(callback); | |
subscriber.add(Subscriptions.create(new Action0() { | |
@Override | |
public void call() { | |
observableField.removeOnPropertyChangedCallback(callback); | |
} | |
})); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment