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
interface DataRepository { | |
Observable<Lce<Data>> getDataEventStream(); | |
} |
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
Observable<Lce<Data>> getDataEventStream() { | |
return api.getData() | |
.map(data -> Lce.data(data)) | |
.startWith(Lce.loading()) | |
.onErrorReturn(e -> Lce.error(e)) | |
} |
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
class UserManager { | |
Observable<Lce<User>> getUserEventStream() { | |
return updateEventStream().startWith(cachedBundle().map(toLce())) | |
} | |
} |
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
class StoreRepository { | |
Observable<Lce<List<Store>> getStoreEventStream() { | |
return userBundleManager.switchMap { bundleEvent -> | |
if (bundleEvent.isLoading()) { | |
return Observable.just(Lce.loading()) | |
} else if (bundleEvent.hasError()) { | |
return Observable.just(Lce.error(bundleEvent.getError())) | |
} else { | |
// Stores can come from cached source or network | |
return storeListEventStream(bundleEvent.data.getZipCode()) |
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
class UserContent { | |
// requires both the user and list of published content | |
} | |
Observable<Lce<User>> getUserEventStream(String userId); | |
Observable<Lce<List<Content>> getContentEventStream(String userId); | |
Observable<Lce<UserContent>> getProfileEventStream(String id) { | |
return Observable.combineLatest( | |
getUserEventStream(id), |
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 void onAttach(View view) { | |
repository.getLatestDataStream().subscribe({ data -> | |
view.showData(data) | |
}) | |
} |
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 void onAttach(View view) { | |
view.showLoading(true) | |
repository.getData().subscribe({ data -> | |
view.showLoading(false) | |
view.showData(data) | |
}, { error -> | |
view.showLoading(false) | |
// should we have a retry action? | |
view.showError("boo!") | |
}) |
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
// Lce -> Loading / Content / Error | |
class Lce<T> { | |
public static <T> Lce<T> data(T data) { | |
// implementation | |
} | |
public static <T> Lce<T> error(Throwable error) { | |
// implementation | |
} | |
public static <T> Lce<T> loading() { | |
// implementation |
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
final PublishRelay<String> refreshRelay = PublishRelay.create() | |
void refreshData() { | |
refreshRelay.call("refresh event") | |
} | |
Observable<Lce<Data>> getDataEventStream() { | |
return refreshRelay | |
.startWith("initial") | |
.switchMap { event -> |
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 void onAttach(View view) { | |
// Remember to clean the subscription! | |
repository.getDataEventStream().subscribe({ event -> | |
if (event.isLoading) { | |
view.showLoading(true) | |
} else if (event.hasError()) { | |
view.showError(event.getError()) | |
} else { | |
view.showData(event.getData()) | |
} |
OlderNewer