Skip to content

Instantly share code, notes, and snippets.

@HIFILEO
Last active December 18, 2017 21:29
Show Gist options
  • Save HIFILEO/afecde74800ea2b738dbe8acb2dd90ec to your computer and use it in GitHub Desktop.
Save HIFILEO/afecde74800ea2b738dbe8acb2dd90ec to your computer and use it in GitHub Desktop.
MVVM + RX example of ViewModel subscribing to published data from views
compositeDisposable.add(loadMorePublishSubject
.debounce(LoadMoreCommand -> {
//Do Work...
})
.doOnNext(loadMoreCommand -> {
//Update UI...
loadMoreEnabledBehaviorSubject.onNext(false);
})
.observeOn(Schedulers.computation())
.flatMap(loadMoreCommand -> {
//Call Network...
return serviceGateway.getNowPlaying(++pageNumber);
})
.delay(sleepSeconds, TimeUnit.SECONDS)
.flatMap(new NowPlayingViewModel.MovieListFetcher())
.flatMap(new TranslateForUiFunction())
.doOnError(throwable -> {
//Do Work (For Errors)...
})
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(movieViewInfos -> {
//Update UI...
if (!firstLoad.get()) {
movieViewInfoList.remove(movieViewInfoList.size() - 1);
adapterDataPublishSubject.onNext(AdapterUiCommand.createRemoveNull());
}
})
.onErrorResumeNext(observer ->{
//Update UI (For Errors)...
showErrorPublishSubject.onNext(true);
//try again
loadMorePublishSubject.onNext(new LoadMoreCommand(SCROLL));
})
.subscribe(movieViewInfos -> {
//Update UI
movieViewInfoList.addAll(movieViewInfos);
adapterDataPublishSubject.onNext(new AdapterUiCommand(AdapterUiCommand.CommandType.ADD, movieViewInfos));
/*...More Code...*/
} throwable -> {
throw new RuntimeException("Subject Crashed!");
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment