Last active
December 18, 2017 21:29
-
-
Save HIFILEO/afecde74800ea2b738dbe8acb2dd90ec to your computer and use it in GitHub Desktop.
MVVM + RX example of ViewModel subscribing to published data from views
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
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