Skip to content

Instantly share code, notes, and snippets.

@ntakouris
Created February 26, 2018 14:33
Show Gist options
  • Save ntakouris/33f7c9e4a188575e2004c03b27b95495 to your computer and use it in GitHub Desktop.
Save ntakouris/33f7c9e4a188575e2004c03b27b95495 to your computer and use it in GitHub Desktop.
public class AutoValidatedTextInputEditText extends TextInputEditText implements AutoValidatedUserInputComponent, TextViewHelper {
List<TextValidator> validatorList = new ArrayList<>();
boolean isValid = false;
public AutoValidatedTextInputEditText(Context context) {
super(context);
SimpleCallback callback = payload -> validate();
setOnFocusChangeListener(new OnFocusChangeListener(callback));
addTextChangedListener(new OnTextChangeListener(callback));
}
public AutoValidatedTextInputEditText(Context context, TextValidator... validators) {
super(context);
SimpleCallback callback = payload -> validate();
setOnFocusChangeListener(new OnFocusChangeListener(callback));
addTextChangedListener(new OnTextChangeListener(callback));
addValidators(validators);
}
public AutoValidatedTextInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoValidatedTextInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/* Could not use streams to ensure backwards compatibility */
private void validate() {
String input = getTextAsString(this);
for (TextValidator validator : validatorList) {
String output = validator.validate(input);
if (output != null) {
setError(output);
isValid = false;
return;
}
}
isValid = true;
}
public void addValidators(TextValidator... validators) {
for (TextValidator validator : validators) {
validatorList.add(validator);
}
}
/* Implement the AutoValidatedUserInputComponent interface.
requestFocus() is already implemented on the View */
public boolean hasValidInput() {
validate();
return isValid;
}
/* Simple View.OnFocusChangeListener that calls the provided callback whenever focus is lost */
private class OnFocusChangeListener implements View.OnFocusChangeListener {
SimpleCallback<Void> focusLostCallback;
public OnFocusChangeListener(SimpleCallback<Void> focusLostCallback) {
this.focusLostCallback = focusLostCallback;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!v.hasFocus()) {
focusLostCallback.call(null);
}
}
}
/* Simple TextWatcher that calls the provided callback whenever the text is changed. */
private class OnTextChangeListener implements TextWatcher {
SimpleCallback<Void> textChangedCallback;
public OnTextChangeListener(SimpleCallback<Void> textChangedCallback) {
this.textChangedCallback = textChangedCallback;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textChangedCallback.call(null);
}
@Override
public void afterTextChanged(Editable s){}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment