I am building an Android MVVM application using RxJava2. What I want is to expose an Observable
in my ViewModel, which I also can receive the last emitted value (like a BehaviourSubject
). I don't want to expose a BehaviourSubject
because I don't want the View to be able to call onNext()
.
For example, in my ViewModel
I expose a date. I now want to subscribe a TextView
to changes, but I also need to be able to access the current value if I want to show a DatePickerDialog
with this date as initial value.
What would be the best way to achieve this?
Delegate:
class TimeSource {
final BehaviorSubject<Long> lastTime = BehaviorSubject.createDefault(
System.currentTimeMillis());
public Observable<Long> timeAsObservable() {
return lastTime;
}
public Long getLastTime() {
return lastTime.getValue();
}
/** internal only */
void updateTime(Long newTime) {
lastTime.onNext(newTime);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With