Skip to content

Instantly share code, notes, and snippets.

@murki
Created November 24, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murki/1b662672f0b9bc879b1ca446c1e079de to your computer and use it in GitHub Desktop.
Save murki/1b662672f0b9bc879b1ca446c1e079de to your computer and use it in GitHub Desktop.
// NetworkRepository.java
@RxLogObservable
public Observable<Data> getData() {
// implementation
}
// DiskRepository.java
@RxLogObservable
public Observable<Timestamped<Data>> getData() {
// implementation
}
public void saveData(Timestamped<Data> data) {
// implementation
}
// DomainService.java
@RxLogObservable
private Observable<Timestamped<Data>> getMergedData() {
return Observable.mergeDelayError(
diskRepository.getData().subscribeOn(Schedulers.io()),
networkRepository.getData()
.timestamp() // <-- This is where the magic happens
.doOnNext(new Action1<Timestamped<Data>>() {
@Override
public void call(Timestamped<Data> data) {
diskRepository.saveData(data);
}
}).subscribeOn(Schedulers.io())
);
}
@RxLogObservable
public Observable<Timestamped<Data>> getData(IDisplayedData displayedData) {
return getMergedData()
.filter(
new Func1<Timestamped<Data>, Boolean>() {
@Override
public Boolean call(Timestamped<Data> timestampedData) {
return timestampedData != null &&
timestampedData.getValue() != null &&
// we check against the already displayed data's timestamp on each emission
timestampedData.getTimestampMillis() >
displayedData.getTimestampMillis();
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment