Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View DavidRyan's full-sized avatar

David Ryan DavidRyan

  • Spothero
  • Chicago
View GitHub Profile
inline fun <Boolean> Boolean.guard(func: () -> Unit): Boolean {
if (this == true) func(); return this
}
(thing == null).guard {
return
}

Reactive Forms with RxAndroid and TextInputLayout

Reactive Programming has been getting a lot of attention in the Android community lately. While it has uses throughout the application stack, we're going to focus here on using it to validate forms (exciting!). This approach cuts down on ugly nested if statements and transforms all of the validation logic to just a few simple lines using the RxJava framework. More, its robust and testable.

This post assumes some knowledge of how RxJava and lambdas work. If you need more of a refresher RxJava Retrolambda

Setup layout with TextInputLayout

Reactive Forms with RxAndroid and TextInputLayout

//TODO Introduction

This post assumes some knowledge of how RxJava and lambdas work. If you need more of a refresher RxJava Retrolambda

Setup layout with TextInputLayout

# Reactive Forms with RxAndroid and TextInputLayout
//TODO Introduction
*This post assumes some knowledge of how RxJava and lambdas work. If you need more of a refresher [RxJava](https://github.com/ReactiveX/RxJava/wiki) [Retrolambda](https://github.com/orfjackal/retrolambda)*
## Setup layout with TextInputLayout
# Reactive Forms with RxAndroid and TextInputLayout
//TODO Introduction
*This post assumes some knowledge of how RxJava and lambdas work. If you need more of a refresher [RxJava](https://github.com/ReactiveX/RxJava/wiki) [Retrolambda](https://github.com/orfjackal/retrolambda)*
## Setup layout with TextInputLayout
# @gf3’s Sexy Bash Prompt, inspired by “Extravagant Zsh Prompt”
# Shamelessly copied from https://github.com/gf3/dotfiles
# Screenshot: http://i.imgur.com/s0Blh.png
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM=gnome-256color
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM=xterm-256color
fi
Pbservable.combineLatest(
validate(text(mFirstControl)),
validate(text(mSecondControl)),
(boolean1, boolean2) -> {
return boolean1 || boolean2
}).subscribe()

Form Validation and In-line Errors with RxAndroid and TextInputLayout

Functional Reactive Programming in Android using RxAndroid has been talked about a lot recently in the Android community. It is great for for cleaning up a variety of aspects of Android programming, but in this post I'm going to focus on form validation (exciting, right?). The problem that needs to be solved is showing in-line error messages on a payment form and enabling a button if all input forms check out. Observing character changes on the EditTexts, using the new Design Support Library's InputFieldLayouts, and making use of some nifty RxAndroid operators makes solving this problem pretty simple. We'll be using the RetroLambda plugin to give us lambda support for RxJava.

(http://i.imgur.com/SouI7G6.gif)

This post assumes some knowledge of how RxJava and lambdas work. If you need more of a refresher[]

Setup layout with textInputLayout

public class RxValidationActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx_validation);
TextView mInputField = (TextView) findViewById(R.id.input);
TextInputLayout mInputLayout = (TextInputLayout) findViewById(R.id.input_layout);
Observable<Boolean> passwordObservable = Observable.combineLatest(
validatePassword(RxViewUtils.text(mPassword)),
(RxViewUtils.text(mPasswordConfirm)),
(password, confirm) -> password.equals(confirm));
Observable.combineLatest(
validateFirstName(RxViewUtils.text(mUsername)),
passwordObservable,
(username, password) -> username && password