Last active
March 2, 2016 18:46
-
-
Save murki/48209690065a839772a7 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
// NetworkRepository.java - no changes | |
@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.merge( | |
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() | |
.onErrorReturn(new Func1<Throwable, Timestamped<Data>>() { | |
@Override | |
public Timestamped<Data> call(Throwable throwable) { | |
Log.e(CLASSNAME, "Error while fetching data. Swallowing the exception.", throwable); | |
return null; // We return null since we know our filter will ignore null values. | |
} | |
}) | |
.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