Created
April 14, 2017 15:57
-
-
Save benigumocom/cbd744abadc8a554aa5024a72f5dbe55 to your computer and use it in GitHub Desktop.
Reactive State 5
This file contains hidden or 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
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