Skip to content

Commit

Permalink
Merge pull request #10 from vijayrawatsan/EditTextAndImagePickerBasic…
Browse files Browse the repository at this point in the history
…Validation

Basic validation for image picker and edit text done. …
  • Loading branch information
vijayrawatsan committed Jul 21, 2015
2 parents b0a1883 + 8333945 commit b1e581d
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 21 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.github.rey5137:material:1.1.0'
compile 'com.rengwuxian.materialedittext:library:2.0.3'
compile 'com.rengwuxian.materialedittext:library:2.1.4'
compile('com.github.ganfra:material-spinner:1.1.0') {
exclude group: 'com.nineoldandroids', module: 'library'
exclude group: 'com.android.support', module: 'appcompat-v7'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

Expand All @@ -14,7 +15,7 @@
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.JsonApi;

public class JsonFormActivity extends ActionBarActivity implements JsonApi {
public class JsonFormActivity extends AppCompatActivity implements JsonApi {

private static final String TAG = "JsonFormActivity";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.json.JSONException;

import android.support.v7.internal.widget.TintContextWrapper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.activities.JsonFormActivity;
import com.vijay.jsonwizard.interfaces.JsonApi;

public class GenericTextWatcher implements TextWatcher {
Expand All @@ -27,7 +29,16 @@ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

public void afterTextChanged(Editable editable) {
String text = editable.toString();
JsonApi api = (JsonApi) mView.getContext();
JsonApi api = null;
if(mView.getContext() instanceof JsonApi) {
api = (JsonApi) mView.getContext();
} else if(mView.getContext() instanceof TintContextWrapper) {
TintContextWrapper tintContextWrapper = (TintContextWrapper) mView.getContext();
api = (JsonApi) tintContextWrapper.getBaseContext();
} else {
throw new RuntimeException("Could not fetch context");
}

String key = (String) mView.getTag(R.id.key);
try {
api.writeValue(mStepName, key, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.activities.JsonFormActivity;
Expand Down Expand Up @@ -98,7 +99,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
presenter.onNextClick(mMainView);
return true;
} else if (item.getItemId() == R.id.action_save) {
presenter.onSaveClick();
presenter.onSaveClick(mMainView);
return true;
}
return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -178,6 +179,12 @@ public Context getContext() {
return getActivity();
}

@Override
public void showToast(String message) {
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}


@Override
public CommonListener getCommonListener() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
Expand All @@ -16,6 +17,7 @@
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.vijay.jsonwizard.R;
Expand All @@ -25,8 +27,11 @@
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interactors.JsonFormInteractor;
import com.vijay.jsonwizard.mvp.MvpBasePresenter;
import com.vijay.jsonwizard.utils.ValidationStatus;
import com.vijay.jsonwizard.views.JsonFormFragmentView;
import com.vijay.jsonwizard.viewstates.JsonFormFragmentViewState;
import com.vijay.jsonwizard.widgets.EditTextFactory;
import com.vijay.jsonwizard.widgets.ImagePickerFactory;

/**
* Created by vijay on 5/14/15.
Expand All @@ -52,6 +57,7 @@ public void addFormElements() {
getView().addFormElements(views);
}

@SuppressLint("ResourceAsColor")
public void setUpToolBar() {
if (!mStepName.equals(JsonFormConstants.FIRST_STEP_NAME)) {
getView().setUpBackButton();
Expand All @@ -71,17 +77,36 @@ public void onBackClick() {
}

public void onNextClick(LinearLayout mainView) {
ValidationStatus validationStatus = writeValuesAndValidate(mainView);
if (validationStatus.isValid()) {
JsonFormFragment next = JsonFormFragment.getFormFragment(mStepDetails.optString("next"));
getView().hideKeyBoard();
getView().transactThis(next);
} else {
getView().showToast(validationStatus.getErrorMessage());
}
}

public ValidationStatus writeValuesAndValidate(LinearLayout mainView) {
int childCount = mainView.getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = mainView.getChildAt(i);
String key = (String) childAt.getTag(R.id.key);
if (childAt instanceof MaterialEditText) {
MaterialEditText editText = (MaterialEditText) childAt;
ValidationStatus validationStatus = EditTextFactory.validate(editText);
if(!validationStatus.isValid()) {
return validationStatus;
}
getView().writeValue(mStepName, key, editText.getText().toString());
} else if (childAt instanceof ImageView) {
Object imagePath = childAt.getTag(R.id.imagePath);
if (imagePath instanceof String) {
getView().writeValue(mStepName, key, (String) imagePath);
ValidationStatus validationStatus = ImagePickerFactory.validate((ImageView) childAt);
if(!validationStatus.isValid()) {
return validationStatus;
}
Object path = childAt.getTag(R.id.imagePath);
if (path instanceof String) {
getView().writeValue(mStepName, key, (String) path);
}
} else if (childAt instanceof CheckBox) {
String parentKey = (String) childAt.getTag(R.id.key);
Expand All @@ -96,15 +121,18 @@ public void onNextClick(LinearLayout mainView) {
}
}
}
JsonFormFragment next = JsonFormFragment.getFormFragment(mStepDetails.optString("next"));
getView().hideKeyBoard();
getView().transactThis(next);
return new ValidationStatus(true, null);
}

public void onSaveClick() {
Intent returnIntent = new Intent();
returnIntent.putExtra("json", getView().getCurrentJsonState());
getView().finishWithResult(returnIntent);
public void onSaveClick(LinearLayout mainView) {
ValidationStatus validationStatus = writeValuesAndValidate(mainView);
if(validationStatus.isValid()) {
Intent returnIntent = new Intent();
returnIntent.putExtra("json", getView().getCurrentJsonState());
getView().finishWithResult(returnIntent);
} else {
Toast.makeText(getView().getContext(), validationStatus.getErrorMessage(), Toast.LENGTH_LONG);
}
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vijay.jsonwizard.utils;

/**
* Created by vijay.rawat01 on 7/21/15.
*/
public class ValidationStatus {
boolean isValid;
String errorMessage;

public ValidationStatus(boolean isValid, String errorMessage) {
this.isValid = isValid;
this.errorMessage = errorMessage;
}

public boolean isValid() {
return isValid;
}

public void setIsValid(boolean isValid) {
this.isValid = isValid;
}

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.vijay.jsonwizard.validators.edittext;

import com.rengwuxian.materialedittext.validation.METValidator;

/**
* Created by vijay.rawat01 on 7/21/15.
*/
public class LengthValidator extends METValidator {

int minLength = 0;
int maxLength = Integer.MAX_VALUE;

public LengthValidator(String errorMessage, int minLength, int maxLength) {
super(errorMessage);
this.minLength = minLength;
this.maxLength = maxLength;
}

@Override
public boolean isValid(CharSequence charSequence, boolean isEmpty) {
if(!isEmpty) {
if(charSequence.length() >= minLength && charSequence.length() <= maxLength) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.vijay.jsonwizard.validators.edittext;

import com.vijay.jsonwizard.widgets.EditTextFactory;

/**
* Created by vijay.rawat01 on 7/21/15.
*/
public class MaxLengthValidator extends LengthValidator {

public MaxLengthValidator(String errorMessage, int maxLength) {
super(errorMessage, EditTextFactory.MIN_LENGTH, maxLength);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.vijay.jsonwizard.validators.edittext;

import com.vijay.jsonwizard.widgets.EditTextFactory;

/**
* Created by vijay.rawat01 on 7/21/15.
*/
public class MinLengthValidator extends LengthValidator {

public MinLengthValidator(String errorMessage, int minLength) {
super(errorMessage, minLength, EditTextFactory.MAX_LENGTH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.vijay.jsonwizard.validators.edittext;

import com.rengwuxian.materialedittext.validation.METValidator;

/**
* Created by vijay.rawat01 on 7/21/15.
*/
public class RequiredValidator extends METValidator {

public RequiredValidator(String errorMessage) {
super(errorMessage);
}

@Override
public boolean isValid(CharSequence charSequence, boolean isEmpty) {
return !isEmpty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public interface JsonFormFragmentView<VS extends ViewState> extends MvpView {

Context getContext();

void showToast(String message);

CommonListener getCommonListener();

void addFormElements(List<View> views);
Expand Down
Loading

0 comments on commit b1e581d

Please sign in to comment.