Created
March 6, 2017 15:31
-
-
Save exallium/9e43de9b5e90817f71900f18e8496a97 to your computer and use it in GitHub Desktop.
simple textChanged example
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
| interface View { | |
| fun textChanged(): Single<String> | |
| fun getText(): String | |
| } | |
| interface Model { | |
| fun validateText(text: String) Boolean | |
| } | |
| class Presenter(view: View, model: Model) { | |
| fun setupTextChanged() { | |
| // textChanged emits a signal every time the user changes some text. | |
| // Ideally, it emits the string of text in the field that was changed. | |
| view.textChanged() | |
| // Map does a simple type mapping. Here, we are going from String to Bool | |
| .map { model.validateText(text) } | |
| // do* methods are for side effects. Here, the side effect of invalid data | |
| // is that the specific error should be shown. Think of it as a place within | |
| // the middle of the stream to place IO and other global-state actions | |
| .doOnNext { if (!it) { view.displayErrorForText() } } | |
| // Filters out anything that doesnt match the predicate. In this case, simply | |
| // only emits true events (the text is valid) | |
| .filter { it } | |
| // Subscriber is also allowed to handle side effects and touch global state. So | |
| // here we simply determine if we can enable to submit button. | |
| .subscribe { view.enableSubmit(checkAllFields()) } | |
| } | |
| fun checkAllFields() { | |
| /** run validation checks on all fields */ | |
| return model.validateText(view.getText()) // etc. | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment