Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Created April 14, 2017 15:57
Show Gist options
  • Save benigumocom/cbd744abadc8a554aa5024a72f5dbe55 to your computer and use it in GitHub Desktop.
Save benigumocom/cbd744abadc8a554aa5024a72f5dbe55 to your computer and use it in GitHub Desktop.
Reactive State 5
Observable<SubmitEvent> events = RxView.clicks(submitView)
.map(ignored -> new SubmitEvent(nameView.getText().toString()));
Observable<SubmitUiModel> models = events
.flatMap(event -> service.setName(event.name)
.map(response -> SubmitUiModel.success())
.onErrorReturn(t -> SubmitUiModel.failure(t.getMessage()))
.observeOn(AndroidSchedulers.mainThread())
.startWith(SubmitUiModel.inProgress()));
disposables.add(models.subscribe(model -> {
submitView.setEnabled(!model.inProgress);
progressView.setVisibility(model.inProgress ? VISIBLE : GONE);
if (!model.inProgress) {
if (model.success) finish()
else Toast.makeText(this, "Failed to set name: " + model.errorMessage, LENGTH_SHORT).show();
}
}, t -> { throw new OnErrorNotImplementedException(t); }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment