Skip to content

Instantly share code, notes, and snippets.

View HIFILEO's full-sized avatar

Daniel Leonardis HIFILEO

View GitHub Profile
@HIFILEO
HIFILEO / NowPlayingActivity_UIBindAndPublish.java
Last active December 18, 2017 21:38
ViewController Ui Binding
compositeDisposable.add(RxRecyclerView.scrollEvents(nowPlayingBinding.recyclerView)
.subscribe(recyclerViewScrollEvent -> {
//Send Information To View Model
if (scrollEventCalculator.isAtScrollEnd()) {
nowPlayingViewModel.loadMoreNowPlayingInfo();
}
}
}));
nowPlayingViewModel.getAdapterDataPublishSubject()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(adapterUiCommand -> {
//Update UI (Adapter Only)...
};
@HIFILEO
HIFILEO / NowPlayingViewModel_LongRunningTask.java
Last active December 18, 2017 21:29
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 -> {
compositeDisposable.add(RxRecyclerView.scrollEvents(nowPlayingBinding.recyclerView)
.flatMap(recyclerViewScrollEvent -> {
if (scrollEventCalculator.isAtScrollEnd()) {
return Observable.just(new ScrollEvent(pageNumber + 1));
} else {
return Observable.empty();
}
})
.debounce(scrollEvent -> Observable.<ScrollEvent>empty().delay(250, TimeUnit.MILLISECONDS))
.subscribe(scrollEvent -> nowPlayingViewModel.processUiEvent(scrollEvent), throwable -> {
uiModelObservable = publishRelayUiEvents
.observeOn(Schedulers.computation())
//Translate UiEvents into Actions
.compose(transformEventsIntoActions)
//Asynchronous Actions To Interactor
.publish(actionObservable -> nowPlayingInteractor.processAction(actionObservable))
//Scan Results to Update UiModel
.scan(initialUiModel, (uiModel, result) -> {
if (result instanceof ScrollResult) {
return processScrollResult(uiModel, (ScrollResult) result);
public Observable<Result> processAction(Observable<Action> actions) {
return actions.compose(transformActionIntoResults);
}
transformActionIntoResults = upstream -> upstream.publish(new Function<Observable<Action>, ObservableSource<Result>>() {
@Override
public ObservableSource<Result> apply(Observable<Action> actionObservable) throws Exception {
return Observable.merge(
actionObservable.ofType(ScrollAction.class).compose(transformScrollActionToScrollResult),
actionObservable.ofType(***Action.class).compose(transform***ActionToRestoreResult)
compositeDisposable.add(nowPlayingViewModel.getUiModels()
.subscribe(this::processUiModel,
throwable -> { throw new UnsupportedOperationException("Errors from Model Unsupported: ");}
)
);
private void processUiModel(UiModel uiModel) {
this.latestUiModel = uiModel;
//Update progressBar...
@Test
public void initState() {
//Arrange
TestObserver<UiModel> testObserver;
TestNowPlayingViewModel nowPlayingViewModel = new TestNowPlayingViewModel(***);
nowPlayingViewModel.init(null);
when(mockTestTransformer.transform(any(Action.class))).thenReturn(Observable.<Result>empty());
//Act
testObserver = nowPlayingViewModel.getUiModels().test();
//Save
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(LAST_UIMODEL, latestUiModel);
}
//Init ViewModel with saved state
public void init(@Nullable UiModel restoredUiModel) {
if (initialUiModel == null) {