Last active
August 29, 2015 14:26
-
-
Save marcelpinto/6ea32a509e656d5d1786 to your computer and use it in GitHub Desktop.
Class use to create the RxForms explained in ...
This file contains 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.eatfirst.android.utils; | |
import android.support.design.widget.TextInputLayout; | |
import java.util.concurrent.TimeUnit; | |
import rx.Observable; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.android.widget.OnTextChangeEvent; | |
import rx.android.widget.WidgetObservable; | |
import rx.functions.Func1; | |
/** | |
* Created by marcel on 31/07/15. | |
*/ | |
public class RxViewUtils { | |
public static Observable<Boolean> verifyInput(TextInputLayout input, | |
Func1<? super String, Boolean> action) { | |
return WidgetObservable.text(input.getEditText(), true).compose(apply(action)); | |
} | |
public static Func1<String, Boolean> checkPhone() { | |
// TODO implement | |
return s -> s.length() > 4; | |
} | |
public static Func1<String, Boolean> checkText(int minLength) { | |
return s -> s.length() > minLength; | |
} | |
public static Func1<String, Boolean> checkEmail() { | |
return s -> s.length() > 4 && s.contains("@") && s.contains("."); | |
} | |
private static Observable.Transformer<? super OnTextChangeEvent, Boolean> apply( | |
Func1<? super String, Boolean> action) { | |
return observable -> observable.debounce(600, TimeUnit.MILLISECONDS) | |
.map(event -> event.text().toString()) | |
.map(action) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribeOn(AndroidSchedulers.mainThread()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment