Skip to content

Commit

Permalink
[Validator] Facilitate accessing context from ValidationContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragunathjawahar committed Jul 31, 2015
1 parent d365fb7 commit f2dc8f3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public class ValidationContext {

// Attributes
Map<View, ArrayList<Pair<Rule, ViewDataAdapter>>> mViewRulesMap;
private Context mContext;

ValidationContext() {
ValidationContext(final Context context) {
this.mContext = context;
}

/**
Expand Down Expand Up @@ -106,16 +108,12 @@ public Object getData(final View view, Class<? extends Annotation> saripaarAnnot
}

/**
* Retrieves a {@link Context} from one of the {@link View}s marked with a Saripaar annotation.
* Get a {@link Context}.
*
* @param saripaarAnnotation An annotation class that is used to annotation one of the views
* in the current controller.
*
* @return A {@link Context} retrieved from one of the annotated {@link View}s.
* @return A {@link Context}.
*/
public Context getContext(final Class<? extends Annotation> saripaarAnnotation) {
List<View> annotatedViews = getAnnotatedViews(saripaarAnnotation);
return annotatedViews.size() == 0 ? null : annotatedViews.get(0).getContext();
public Context getContext() {
return mContext;
}

void setViewRulesMap(final Map<View, ArrayList<Pair<Rule, ViewDataAdapter>>> viewRulesMap) {
Expand Down
24 changes: 23 additions & 1 deletion saripaar/src/main/java/com/mobsandgeeks/saripaar/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@

package com.mobsandgeeks.saripaar;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Pair;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

import com.mobsandgeeks.saripaar.adapter.CheckBoxBooleanAdapter;
import com.mobsandgeeks.saripaar.adapter.RadioButtonBooleanAdapter;
import com.mobsandgeeks.saripaar.adapter.RadioGroupBooleanAdapter;
import com.mobsandgeeks.saripaar.adapter.SpinnerIndexAdapter;
import com.mobsandgeeks.saripaar.adapter.ViewDataAdapter;
import com.mobsandgeeks.saripaar.annotation.AssertFalse;
Expand Down Expand Up @@ -152,7 +155,6 @@ public Validator(final Object controller) {
assertNotNull(controller, "controller");
mController = controller;
mValidationMode = Mode.BURST;
mValidationContext = new ValidationContext();
mSequenceComparator = new SequenceComparator();
mViewValidatedAction = new DefaultViewValidatedAction();
}
Expand Down Expand Up @@ -584,6 +586,10 @@ private Pair<Rule, ViewDataAdapter> getRuleAdapterPair(final Annotation saripaar
throw new UnsupportedOperationException(message);
}

if (mValidationContext == null) {
mValidationContext = new ValidationContext(getContext(viewField));
}

final Class<? extends AnnotationRule> ruleType = getRuleType(saripaarAnnotation);
final AnnotationRule rule = Reflector.instantiateRule(ruleType,
saripaarAnnotation, mValidationContext);
Expand All @@ -610,6 +616,17 @@ private ViewDataAdapter getDataAdapter(final Class<? extends Annotation> annotat
return dataAdapter;
}

private Context getContext(final Field viewField) {
Context context = null;
try {
View view = (View) viewField.get(mController);
context = view.getContext();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return context;
}

private Class<? extends AnnotationRule> getRuleType(final Annotation ruleAnnotation) {
ValidateUsing validateUsing = ruleAnnotation.annotationType()
.getAnnotation(ValidateUsing.class);
Expand Down Expand Up @@ -925,6 +942,11 @@ protected void onPostExecute(final ValidationReport validationReport) {
new CheckBoxBooleanAdapter(),
AssertFalse.class, AssertTrue.class, Checked.class);

// RadioGroupBooleanAdapter
SARIPAAR_REGISTRY.register(RadioGroup.class, Boolean.class,
new RadioGroupBooleanAdapter(),
Checked.class);

// RadioButtonBooleanAdapter
SARIPAAR_REGISTRY.register(RadioButton.class, Boolean.class,
new RadioButtonBooleanAdapter(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@
* @since 2.0
*/
public class FutureRule extends ContextualAnnotationRule<Future, String> {
private DateFormat mDateFormat;
private int mDateFormatResId;
private String mDateFormatString;

protected FutureRule(ValidationContext validationContext, Future future) {
protected FutureRule(final ValidationContext validationContext, final Future future) {
super(validationContext, future);
Context context = validationContext.getContext(future.getClass());
String dateFormatString = future.dateFormatResId() != -1
? context.getString(future.dateFormatResId()) : future.dateFormat();
mDateFormat = new SimpleDateFormat(dateFormatString);
mDateFormatResId = future.dateFormatResId();
mDateFormatString = future.dateFormat();
}

@Override
public boolean isValid(final String dateString) {
Date parsedDate;
DateFormat dateFormat = getDateFormat();
Date parsedDate = null;
try {
parsedDate = mDateFormat.parse(dateString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
parsedDate = dateFormat.parse(dateString);
} catch (ParseException ignored) {}

Date now = new Date();
return parsedDate.after(now);
return parsedDate != null && parsedDate.after(now);
}

private DateFormat getDateFormat() {
Context context = mValidationContext.getContext();
String dateFormatString = mDateFormatResId != -1
? context.getString(mDateFormatResId) : mDateFormatString;
return new SimpleDateFormat(dateFormatString);
}
}
31 changes: 18 additions & 13 deletions saripaar/src/main/java/com/mobsandgeeks/saripaar/rule/PastRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@
* @since 2.0
*/
public class PastRule extends ContextualAnnotationRule<Past, String> {
private DateFormat mDateFormat;
private int mDateFormatResId;
private String mDateFormatString;

protected PastRule(ValidationContext validationContext, Past past) {
protected PastRule(final ValidationContext validationContext, final Past past) {
super(validationContext, past);
Context context = validationContext.getContext(past.getClass());
String dateFormatString = past.dateFormatResId() != -1
? context.getString(past.dateFormatResId()) : past.dateFormat();
mDateFormat = new SimpleDateFormat(dateFormatString);
mDateFormatResId = past.dateFormatResId();
mDateFormatString = past.dateFormat();
}

@Override
public boolean isValid(final String dateString) {
Date parsedDate;
public boolean isValid(final String dateString) {
DateFormat dateFormat = getDateFormat();
Date parsedDate = null;
try {
parsedDate = mDateFormat.parse(dateString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
parsedDate = dateFormat.parse(dateString);
} catch (ParseException ignored) {}

Date now = new Date();
return parsedDate.before(now);
return parsedDate != null && parsedDate.before(now);
}

private DateFormat getDateFormat() {
Context context = mValidationContext.getContext();
String dateFormatString = mDateFormatResId != -1
? context.getString(mDateFormatResId) : mDateFormatString;
return new SimpleDateFormat(dateFormatString);
}
}

0 comments on commit f2dc8f3

Please sign in to comment.