Skip to content

Commit

Permalink
support only validate specified field.
Browse files Browse the repository at this point in the history
  • Loading branch information
zillachan committed Oct 12, 2017
1 parent e0522f8 commit 8d14f62
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 30 deletions.
19 changes: 16 additions & 3 deletions example/src/main/java/pub/zilla/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import butterknife.BindView;
Expand Down Expand Up @@ -32,13 +33,25 @@ protected void onCreate(Bundle savedInstanceState) {
ButterKnife.bind(this);
}

@OnClick(R.id.button)
public void onViewClicked() {
ValiZilla.vali(this);
@OnClick({R.id.button, R.id.button_first})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.button:
ValiZilla.vali(this);
break;
case R.id.button_first:
ValiZilla.vali(this, 1, 2);
break;
}
}

@ValiSuccess
void onValiSuccess() {
Toast.makeText(this, R.string.vali_success, Toast.LENGTH_LONG).show();
}

@ValiSuccess
void onValiSuccess(int... i) {
Toast.makeText(this, R.string.vali_success, Toast.LENGTH_LONG).show();
}
}
5 changes: 5 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
android:layout_height="wrap_content"
android:text="@string/validate" />

<Button
android:id="@+id/button_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/validate_first" />
</LinearLayout>
1 change: 1 addition & 0 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="reg_error">reg error</string>
<string name="vali_success">Validate success.</string>
<string name="validate">Validate</string>
<string name="validate_first">Validate First</string>
<string name="phone_number">Phone Number</string>
<string name="zip_code">Zip Code</string>
</resources>
86 changes: 66 additions & 20 deletions valizilla/src/main/java/pub/zilla/validzilla/ValiZilla.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import pub.zilla.validzilla.an.NotNull;
import pub.zilla.validzilla.an.Reg;
import pub.zilla.validzilla.an.ValiFailed;
import pub.zilla.validzilla.an.ValiSuccess;
import pub.zilla.validzilla.model.MethodModel;
import pub.zilla.validzilla.model.ValiModel;
import pub.zilla.validzilla.model.ValiWapper;

Expand All @@ -25,6 +29,11 @@
public class ValiZilla {

public static void vali(Object target) {
vali(target, null);
}

public static void vali(Object target, int... orders) {

ValiWapper wapper = getFieldsFromCache(target.getClass());
if (wapper.getValiModel() == null) return;
Class textInputLayout = null;
Expand All @@ -42,34 +51,58 @@ public int compare(ValiModel o1, ValiModel o2) {
}
});
for (ValiModel model : models) {
try {
Class fieldType = model.getField().getType();
if (textInputLayout.isAssignableFrom(fieldType)) {//if is TextInputLayout or extends from TextInputLayout.
Field field = model.getField();
field.setAccessible(true);
TextInputLayout targetField = (TextInputLayout) model.getField().get(target);
String result = targetField.getEditText().getText().toString();//result
if (TextUtils.isEmpty(model.getReg())) {//not null check fail
if (TextUtils.isEmpty(result)) {
if ((orders != null && contains(orders, model.getOrder())) || orders == null) {
try {
Class fieldType = model.getField().getType();
if (textInputLayout.isAssignableFrom(fieldType)) {//if is TextInputLayout or extends from TextInputLayout.
Field field = model.getField();
field.setAccessible(true);
TextInputLayout targetField = (TextInputLayout) model.getField().get(target);
String result = targetField.getEditText().getText().toString();//result
if (TextUtils.isEmpty(model.getReg())) {//not null check fail
if (TextUtils.isEmpty(result)) {
targetField.setError(targetField.getContext().getString(model.getError()));
invokeSuccess(target, ValiFailed.class, orders);
return;
} else {
targetField.setError("");
}
} else if (!result.matches(model.getReg())) {// reg check fail;
targetField.setError(targetField.getContext().getString(model.getError()));
invokeSuccess(target, ValiFailed.class, orders);
return;
} else {
targetField.setError("");
}
} else if (!result.matches(model.getReg())) {// reg check fail;
targetField.setError(targetField.getContext().getString(model.getError()));
return;
} else {
targetField.setError("");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (wapper.getMethod() != null) {
invokeSuccess(target, ValiSuccess.class, orders);
}

/**
* validate callback.
*
* @param target
* @param orders
*/
private static void invokeSuccess(Object target, Class annotationClass, int... orders) {
List<MethodModel> methodModels = getFieldsFromCache(target.getClass()).getMethods();
if (methodModels != null) {
try {
wapper.getMethod().invoke(target);
for (MethodModel methodModel : methodModels) {
if (methodModel.getAnnotation().annotationType().equals(annotationClass)) {
Class<?>[] parameterTypes = methodModel.getMethod().getParameterTypes();
if (parameterTypes.length == 0) {
methodModel.getMethod().invoke(target);
} else {
methodModel.getMethod().invoke(target, orders);
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
Expand All @@ -78,6 +111,13 @@ public int compare(ValiModel o1, ValiModel o2) {
}
}

private static boolean contains(int[] array, int value) {
for (int i : array) {
if (value == i) return true;
}
return false;
}

/**
* un inject
*
Expand Down Expand Up @@ -116,11 +156,17 @@ private static ValiWapper getFieldsFromCache(Class c) {
}
}
//Method
Method[] methods = c.getMethods();
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
ValiSuccess valiSuccess = method.getAnnotation(ValiSuccess.class);
ValiFailed valiFailed = method.getAnnotation(ValiFailed.class);
if (valiSuccess != null) {
wapper.setMethod(method);
MethodModel methodModel = new MethodModel(method, valiSuccess);
wapper.addMethod(methodModel);
}
if (valiFailed != null) {
MethodModel methodModel = new MethodModel(method, valiFailed);
wapper.addMethod(methodModel);
}
}
cache.put(c, wapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pub.zilla.validzilla.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
* Created by zilla on 10/12/17.
*/

public class MethodModel {
private Method method;
private Annotation annotation;

public MethodModel() {

}

public MethodModel(Method method, Annotation annotation) {
this.method = method;
this.annotation = annotation;
}

public Method getMethod() {
return method;
}

public void setMethod(Method method) {
this.method = method;
}

public Annotation getAnnotation() {
return annotation;
}

public void setAnnotation(Annotation annotation) {
this.annotation = annotation;
}

@Override
public String toString() {
return "MethodModel{" +
"method=" + method +
", annotation=" + annotation +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pub.zilla.validzilla.model;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -10,7 +9,7 @@

public class ValiWapper {
private List<ValiModel> valiModels;
private Method method;
private List<MethodModel> methods;

public ValiWapper() {

Expand All @@ -24,12 +23,15 @@ public void setValiModel(List<ValiModel> valiModel) {
this.valiModels = valiModel;
}

public Method getMethod() {
return method;
public void addMethod(MethodModel method) {
if (methods == null) {
methods = new ArrayList<>();
}
methods.add(method);
}

public void setMethod(Method method) {
this.method = method;
public List<MethodModel> getMethods() {
return methods;
}

public void addValiModel(ValiModel model) {
Expand All @@ -43,7 +45,7 @@ public void addValiModel(ValiModel model) {
public String toString() {
return "ValiWapper{" +
"valiModels=" + valiModels +
", method=" + method +
", methods=" + methods +
'}';
}
}

0 comments on commit 8d14f62

Please sign in to comment.