Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Created April 14, 2017 15:55
Show Gist options
  • Save benigumocom/976e2cde30b6b66219ff409b9e682b8a to your computer and use it in GitHub Desktop.
Save benigumocom/976e2cde30b6b66219ff409b9e682b8a to your computer and use it in GitHub Desktop.
Reactive State 4
disposables.add(RxView.clicks(submitView)
.map(ignored -> new SubmitEvent(nameView.getText().toString()))
.flatMap(event -> service.setName(event.name)
.map(response -> SubmitUiModel.success())
.onErrorReturn(t -> SubmitUiModel.failure(t.getMessage()))
.observeOn(AndroidSchedulers.mainThread())
.startWith(SubmitUiModel.inProgress()))
.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