GeezerVison is showing github.com — feat: add info text on .combineLatest example · ttylinux/Android-RxJava@7d6a92c
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,16 @@ Have a look at the accompanying blog posts for details on this demo: |
72 | 72 | 2. [DebouncedBuffer used for the fancier variant of the demo](http://nerds.weddingpartyapp.com/tech/2014/12/24/secret-bonus-part-debouncedbuffer-used-in-rxbus-example/) |
73 | 73 | 3. [share/publish/refcount](http://nerds.weddingpartyapp.com/tech/2014/12/24/rxjava-share-publish-refcount-and-all-that-jazz/) |
74 | 74 |
|
| 75 | +### Form validation using combineLatest |
| 76 | + |
| 77 | +courtesy Dan Lew for this idea that he gave me on the [fragmented podcast episode #5](http://fragmentedpodcast.com/episodes/4/) (around the 4:30 mark). |
| 78 | + |
| 79 | +`.combineLatest` allows you to monitor the state of multiple observables at once compactly at a single location. The example demonstrated shows how you can use `.combineLatest` to validate a basic form. There are 3 primary inputs for this form to be considered "valid" (an email, a password and a number). The form will turn valid (the text below turns blue :P) once all the inputs are valid. If they are not, an error is shown against the invalid inputs. |
| 80 | + |
| 81 | +We have 3 independent observables that track the text/input changes for each of the form fields (RxAndroid's `WidgetObservable` comes in handy to monitor the text changes). After an event change is noticed from all 3 inputs, the result is "combined" and the form is evaluated for validity. |
| 82 | + |
| 83 | +The value of this technique becomes more apparent when you have more number of input fields in a form. Handling it otherwise with a bunch of booleans makes the code cluttered and kind of difficult to follow. But using `.combineLatest` all that logic is concentrated in a nice compact block of code (I still use booleans but that was to make the example more readable). |
| 84 | + |
75 | 85 |
|
76 | 86 | ## Work in Progress: |
77 | 87 |
|
|