Last active
April 27, 2016 09:18
-
-
Save benoitjadinon/4d74f28ffa88f9a9fce4438dc4afb387 to your computer and use it in GitHub Desktop.
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
| RxFormValidationHelper.subscribeValidateButton(butLogin, | |
| RxFormValidationHelper.validateText(etMail, mailLayout, RxFormValidationHelper.RuleIsEmail, getString(R.string.webservice_error_invalid_parameter_email)), | |
| RxFormValidationHelper.validateText(etPass, passLayout, RxFormValidationHelper.RuleMinChars.call(8), getString(R.string.webservice_error_invalid_parameter_password)) | |
| ); |
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
| package com.bja.whatever; | |
| import android.support.design.widget.TextInputLayout; | |
| import android.widget.Button; | |
| import android.widget.EditText; | |
| import com.jakewharton.rxbinding.view.RxView; | |
| import com.jakewharton.rxbinding.widget.RxTextView; | |
| import java.util.Arrays; | |
| import rx.Observable; | |
| import rx.functions.*; | |
| public class RxFormValidationHelper { | |
| public static Func1<CharSequence, Boolean> RuleNotEmpty; | |
| public static Func1<Integer, Func1<CharSequence, Boolean>> RuleMinChars, RuleMaxChars; | |
| public static Func1<CharSequence, Boolean> RuleIsEmail; | |
| static { | |
| RuleMaxChars = len -> txt -> txt.length() <= len; | |
| RuleMinChars = len -> txt -> txt.length() >= len; | |
| RuleNotEmpty = RuleMinChars.call(1); | |
| RuleIsEmail = txt -> RuleNotEmpty.call(txt) && android.util.Patterns.EMAIL_ADDRESS.matcher(txt).matches(); | |
| } | |
| public static Observable<Boolean> validateText(EditText etEmail, TextInputLayout emailLayout, Func1<CharSequence, Boolean> rule, String errorString) { | |
| return RxTextView.textChanges(etEmail) | |
| .map(rule) | |
| .distinctUntilChanged() | |
| .doOnNext(b -> emailLayout.setError(!b ? errorString : null)); | |
| } | |
| public static void subscribeValidateButton(Button butLogin, Observable<Boolean>... booleanObservables) { | |
| RxView.clicks(butLogin).map(aVoid -> true) // avoids triggering the rest if the button hasn't been clicked once | |
| .flatMap(b -> | |
| Observable.combineLatest(Arrays.asList(booleanObservables), args -> { | |
| for (Object arg : args) { | |
| if (!((Boolean) arg)) | |
| return false; | |
| } | |
| return true; | |
| }) | |
| .distinctUntilChanged() | |
| ) | |
| .subscribe(RxView.clickable(butLogin)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment