Skip to content

Commit

Permalink
Merge 09cc9c9 into 2acb479
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-karuri committed Aug 6, 2020
2 parents 2acb479 + 09cc9c9 commit 23a7120
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.vijay.jsonwizard.views.JsonFormFragmentView;
import com.vijay.jsonwizard.viewstates.JsonFormFragmentViewState;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.simprint.SimPrintsLibrary;
Expand Down Expand Up @@ -225,6 +227,11 @@ public void setmJsonApi(JsonApi mJsonApi) {
@Override
public void onResume() {
super.onResume();
if (!getJsonApi().isPreviousPressed()) {
skipStepsOnNextPressed();
} else {
skipStepOnPreviousPressed();
}
}

@Override
Expand Down Expand Up @@ -634,12 +641,14 @@ public void onClick(View view) {
if (view != null) {
if (view.getId() == R.id.next_button) {
Object isSubmit = view.getTag(R.id.submit);
getJsonApi().setPreviousPressed(false);
if (isSubmit != null && Boolean.valueOf(isSubmit.toString())) {
save(false);
} else {
next();
}
} else if (view.getId() == R.id.previous_button) {
getJsonApi().setPreviousPressed(true);
getFragmentManager().popBackStack();
}
}
Expand All @@ -660,4 +669,79 @@ public void setOnFieldsInvalid(OnFieldsInvalid onFieldsInvalid) {
public static NativeFormsProperties getNativeFormProperties() {
return nativeFormProperties;
}

/**
* Skips blank by relevance steps when next is clicked on the json wizard forms.
*/
public void skipStepsOnNextPressed() {
if (skipBlankSteps()) {
JSONObject formStep = getStep(getArguments().getString(JsonFormConstants.STEPNAME));
String next = formStep.optString(JsonFormConstants.NEXT, "");
if (StringUtils.isNotEmpty(next)) {
checkIfStepIsBlank(formStep);
if (shouldSkipStep()) {
next();
}
}
}
}

/**
* Skips blank by relevance steps when previous is clicked on the json wizard forms.
*/
public void skipStepOnPreviousPressed() {
if (skipBlankSteps()) {
JSONObject currentFormStep = getStep(getArguments().getString(JsonFormConstants.STEPNAME));
String next = currentFormStep.optString(JsonFormConstants.NEXT, "");
int currentFormStepNumber = getFormStepNumber(next);
for (int i = currentFormStepNumber; i >= 1; i--) {
JSONObject formStep = getJsonApi().getmJSONObject().optJSONObject(JsonFormConstants.STEP + i);
if (formStep != null) {
checkIfStepIsBlank(formStep);
if (shouldSkipStep()) {
getFragmentManager().popBackStack();
} else {
break;
}
}
}
}
}


/**
* Checks if a given step is blank due to relevance hidding all the widgets
*
* @param formStep {@link JSONObject}
*/
private void checkIfStepIsBlank(JSONObject formStep) {
try {
if (formStep.has(JsonFormConstants.FIELDS)) {
JSONArray fields = formStep.getJSONArray(JsonFormConstants.FIELDS);
for (int i = 0; i < fields.length(); i++) {
JSONObject field = fields.getJSONObject(i);
if (field.has(JsonFormConstants.TYPE) && !JsonFormConstants.HIDDEN.equals(field.getString(JsonFormConstants.TYPE))) {
boolean isVisible = field.optBoolean(JsonFormConstants.IS_VISIBLE, true);
if (isVisible) {
setShouldSkipStep(false);
break;
}
}
}
}
} catch (JSONException e) {
Timber.e(e, "%s --> checkIfStepIsBlank", this.getClass().getCanonicalName());
}
}

/**
* Returns the current form step number when given than steps next step number.
* This number is used to figure out which steps to pop when previous is clicked.
*
* @param nextFormNumber {@link String}
* @return formNumber {@link Integer}
*/
private int getFormStepNumber(String nextFormNumber) {
return Integer.parseInt(getArguments().getString(JsonFormConstants.STEPNAME).substring(4));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
import com.vijay.jsonwizard.task.NextProgressDialogTask;
import com.vijay.jsonwizard.viewstates.JsonFormFragmentViewState;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import timber.log.Timber;

/**
Expand Down Expand Up @@ -150,11 +145,6 @@ private Form getForm() {
public void onResume() {
super.onResume();
setJsonFormFragment(this);
if (!getJsonApi().isPreviousPressed()) {
skipStepsOnNextPressed();
} else {
skipStepOnPreviousPressed();
}
}

@Override
Expand Down Expand Up @@ -263,89 +253,6 @@ public void updateVisibilityOfNextAndSave(boolean next, boolean save) {
}
}

/**
* Skips blank by relevance steps when next is clicked on the json wizard forms.
*/
public void skipStepsOnNextPressed() {
if (skipBlankSteps()) {
JSONObject formStep = getStep(getArguments().getString(JsonFormConstants.STEPNAME));
String next = formStep.optString(JsonFormConstants.NEXT, "");
if (StringUtils.isNotEmpty(next)) {
checkIfStepIsBlank(formStep);
if (shouldSkipStep()) {
next();
}
}
}
}

/**
* Skips blank by relevance steps when previous is clicked on the json wizard forms.
*/
public void skipStepOnPreviousPressed() {
if (skipBlankSteps()) {
JSONObject currentFormStep = getStep(getArguments().getString(JsonFormConstants.STEPNAME));
String next = currentFormStep.optString(JsonFormConstants.NEXT, "");
int currentFormStepNumber = getFormStepNumber(next);
for (int i = currentFormStepNumber; i >= 1; i--) {
JSONObject formStep = getJsonApi().getmJSONObject().optJSONObject(JsonFormConstants.STEP + i);
if (formStep != null) {
checkIfStepIsBlank(formStep);
if (shouldSkipStep()) {
getFragmentManager().popBackStack();
} else {
break;
}
}
}
}
}

/**
* Checks if a given step is blank due to relevance hidding all the widgets
*
* @param formStep {@link JSONObject}
*/
private void checkIfStepIsBlank(JSONObject formStep) {
try {
if (formStep.has(JsonFormConstants.FIELDS)) {
JSONArray fields = formStep.getJSONArray(JsonFormConstants.FIELDS);
for (int i = 0; i < fields.length(); i++) {
JSONObject field = fields.getJSONObject(i);
if (field.has(JsonFormConstants.TYPE) && !JsonFormConstants.HIDDEN.equals(field.getString(JsonFormConstants.TYPE))) {
boolean isVisible = field.optBoolean(JsonFormConstants.IS_VISIBLE, true);
if (isVisible) {
setShouldSkipStep(false);
break;
}
}
}
}
} catch (JSONException e) {
Timber.e(e, "%s --> checkIfStepIsBlank", this.getClass().getCanonicalName());
}
}

/**
* Returns the current form step number when given than steps next step number.
* This number is used to figure out which steps to pop when previous is clicked.
*
* @param nextFormNumber {@link String}
* @return formNumber {@link Integer}
*/
private int getFormStepNumber(String nextFormNumber) {
int formNumber = 0;
if (StringUtils.isNotBlank(nextFormNumber)) {
int currentFormNumber = Integer.parseInt(nextFormNumber.substring(4, 5)) - 1;
if (currentFormNumber > 0) {
formNumber = currentFormNumber;
} else if (currentFormNumber == 0) {
formNumber = 1;
}
}
return formNumber;
}

protected void save() {
try {
Boolean skipValidation = ((JsonFormActivity) mMainView.getContext()).getIntent()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.13.5-SNAPSHOT
VERSION_NAME=1.14.0-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down

0 comments on commit 23a7120

Please sign in to comment.