Skip to content

Commit

Permalink
v2.1.13-SNAPSHOT (#610)
Browse files Browse the repository at this point in the history
* Update to client-utils v0.0.6-SNAPSHOT

* configurable visibility for form wizard prev/next icons

* configurable greying of next/save menu item when form fields are invalid

* configurable hiding of shown date selector dialog when a date form field is invalid
  • Loading branch information
LZRS authored Nov 23, 2021
1 parent ff55678 commit 64defd0
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 13 deletions.
4 changes: 2 additions & 2 deletions android-json-form-wizard/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ dependencies {
exclude group: 'com.android.support', module: 'recyclerview-v7'
}

implementation 'org.smartregister:opensrp-client-utils:0.0.4-SNAPSHOT'
implementation 'org.smartregister:opensrp-client-utils:0.0.6-SNAPSHOT'

implementation 'org.jeasy:easy-rules-core:3.3.0'
implementation 'org.jeasy:easy-rules-mvel:3.3.0'
Expand Down Expand Up @@ -146,7 +146,7 @@ dependencies {
testImplementation "org.powermock:powermock-module-junit4-rule:$powerMockVersion"
testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion"
testImplementation "org.powermock:powermock-classloading-xstream:$powerMockVersion"
implementation('com.github.raihan-mpower:FancyAlertDialog-Android:0.3') {
implementation('com.github.raihan-mpower:FancyAlertDialog-Android:0.3@aar') {
exclude group: 'com.android.support', module: 'appcompat-v7'

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ public boolean validateWith(@NonNull METValidator validator) {
return isValid;
}

public boolean isFilledValidly(){
boolean noValidators = validators == null || validators.isEmpty();
if (noValidators) return true;

boolean valid = true;
for (METValidator validator: validators){
valid = valid && validator.isValid(getText(), isEmpty());
}
return valid;
}

public boolean isEmpty(){
return getText().length() == 0;
}

/**
* Check all validators, sets the error text if not
* <p/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -31,6 +37,7 @@
import com.vijay.jsonwizard.activities.JsonFormActivity;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.customviews.RadioButton;
import com.vijay.jsonwizard.domain.Form;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.interfaces.JsonApi;
import com.vijay.jsonwizard.interfaces.OnFieldsInvalid;
Expand All @@ -42,6 +49,7 @@
import com.vijay.jsonwizard.viewstates.JsonFormFragmentViewState;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -59,8 +67,10 @@
* Created by vijay on 5/7/15.
*/
public class JsonFormFragment extends MvpFragment<JsonFormFragmentPresenter, JsonFormFragmentViewState>
implements CommonListener, JsonFormFragmentView<JsonFormFragmentViewState> {
implements CommonListener, JsonFormFragmentView<JsonFormFragmentViewState>, Handler.Callback {
private static final String TAG = "JsonFormFragment";
private static final int GRAY_OUT_ACTIVE_WHAT = 1212;

public OnFieldsInvalid onFieldsInvalid;
protected LinearLayout mMainView;
protected ScrollView mScrollView;
Expand All @@ -76,6 +86,8 @@ public class JsonFormFragment extends MvpFragment<JsonFormFragmentPresenter, Jso

private static NativeFormsProperties nativeFormProperties;

private final Handler handler = new Handler(this);

public static JsonFormFragment getFormFragment(String stepName) {
JsonFormFragment jsonFormFragment = new JsonFormFragment();
Bundle bundle = new Bundle();
Expand All @@ -85,6 +97,25 @@ public static JsonFormFragment getFormFragment(String stepName) {
return jsonFormFragment;
}

@Override
public boolean handleMessage(@NonNull @NotNull Message msg) {
//noinspection SwitchStatementWithTooFewBranches
switch (msg.what){
case GRAY_OUT_ACTIVE_WHAT:
requireActivity().invalidateOptionsMenu();
return true;
default:
return false;
}
}

Form getForm() {
if (getActivity() != null && getActivity() instanceof JsonFormActivity) {
return ((JsonFormActivity) getActivity()).getForm();
}
return null;
}

@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
Expand Down Expand Up @@ -119,6 +150,12 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
return rootView;
}

@Override
public void onDestroyView() {
handler.removeMessages(GRAY_OUT_ACTIVE_WHAT);
super.onDestroyView();
}

private void setupToolbarBackButton() {
if (getArguments() != null) {
String stepName = getArguments().getString(JsonFormConstants.STEPNAME);
Expand Down Expand Up @@ -247,6 +284,23 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_toolbar, menu);
presenter.setUpToolBar();

if (getForm() != null && getForm().isGreyOutSaveWhenFormInvalid()){
boolean isFormFilled = presenter.areFormViewsFilled();
if (isFormFilled) {
menu.findItem(R.id.action_next).setTitle(R.string.next);
menu.findItem(R.id.action_save).setTitle(R.string.save);
} else {
SpannableString nextString = new SpannableString(getString(R.string.next));
SpannableString saveString = new SpannableString(getString(R.string.save));
nextString.setSpan(new ForegroundColorSpan(Color.parseColor("#EECCCCCC")), 0, nextString.length(), 0);
saveString.setSpan(new ForegroundColorSpan(Color.parseColor("#EECCCCCC")), 0, saveString.length(), 0);
menu.findItem(R.id.action_next).setTitle(nextString);
menu.findItem(R.id.action_save).setTitle(saveString);
}

handler.sendEmptyMessageDelayed(GRAY_OUT_ACTIVE_WHAT, 100);
}
}

@Override
Expand Down Expand Up @@ -667,7 +721,6 @@ public void scrollToView(final View view) {
getJsonApi().getAppExecutors().mainThread().execute(new Runnable() {
@Override
public void run() {
view.requestFocus();
if (!(view instanceof MaterialEditText)) {
mScrollView.post(new Runnable() {
@Override
Expand All @@ -680,6 +733,12 @@ public void run() {
}
});
}

Form form = getForm();
if (!(view instanceof MaterialEditText)
|| (form != null && !form.isGreyOutSaveWhenFormInvalid())){
view.requestFocus();
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ protected void setupCustomUI() {

}

private Form getForm() {
if (getActivity() != null && getActivity() instanceof JsonFormActivity) {
return ((JsonFormActivity) getActivity()).getForm();
}
return null;
}

@Override
public void onResume() {
super.onResume();
Expand Down Expand Up @@ -194,7 +187,7 @@ public void setActionBarTitle(String title) {
public void updateVisibilityOfNextAndSave(boolean next, boolean save) {
Form form = getForm();
if (form != null && form.isWizard()) {
getMenu().findItem(com.vijay.jsonwizard.R.id.action_next).setVisible(false);
getMenu().findItem(com.vijay.jsonwizard.R.id.action_next).setVisible(next && form.isShowNextInToolbarWhenWizard());
getMenu().findItem(com.vijay.jsonwizard.R.id.action_save).setVisible(save);
} else {
getMenu().findItem(com.vijay.jsonwizard.R.id.action_next).setVisible(next);
Expand Down Expand Up @@ -245,6 +238,9 @@ public void updateVisibilityOfNextAndSave(boolean next, boolean save) {
if (form != null && form.isHidePreviousButton()) {
previousButton.setVisibility(View.GONE);
}
if (form != null && form.isHideNextIcon()) nextIcon.setVisibility(View.GONE);

if (form != null && form.isHidePreviousIcon()) previousIcon.setVisibility(View.GONE);
}

//hide wizard nav bar if stepcount is zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.rengwuxian.materialedittext.validation.METValidator;
import com.rey.material.widget.Button;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.activities.JsonFormActivity;
Expand Down Expand Up @@ -372,6 +373,69 @@ public boolean onNextClick(LinearLayout mainView) {
return false;
}

public boolean areFormViewsFilled() {
boolean filled = true;
for (View childView : formFragment.getJsonApi().getFormDataViews()) {
if (childView instanceof RadioGroup) {
filled = filled && NativeRadioButtonFactory.isValid((RadioGroup) childView);

} else if (childView instanceof NativeEditText) {
boolean filledValid = ((NativeEditText) childView).isFilledValidly();
filled = filled && filledValid;

} else if (childView instanceof MaterialEditText) {
MaterialEditText editView = (MaterialEditText) childView;
boolean noValidation = (!childView.isEnabled() || !editView.hasValidators());
boolean valid = true;
if (!noValidation && editView.getValidators() != null) {
for (METValidator validator : editView.getValidators()) {
valid = valid && validator.isValid(editView.getText(), editView.getText().length() == 0);
}
}
filled = filled && (noValidation || valid);

} else if (childView instanceof ImageView) {
boolean isRequired = (((childView.getTag(R.id.v_required) instanceof String) || (childView.getTag(R.id.error) instanceof String))
&& childView.isEnabled()
&& Boolean.parseBoolean((String) childView.getTag(R.id.v_required)));
boolean isEmpty = childView.getTag(R.id.imagePath) == null
|| TextUtils.isEmpty((String) childView.getTag(R.id.imagePath));

filled = filled && (!isRequired || !isEmpty);

} else if (childView instanceof Button) {
String type = (String) childView.getTag(R.id.type);
if (!TextUtils.isEmpty(type) && type.equals(JsonFormConstants.GPS)) {
boolean isRequired = ((childView.getTag(R.id.v_required) instanceof String) || (childView.getTag(R.id.error) instanceof String)
&& childView.isEnabled()
&& Boolean.parseBoolean((String) childView.getTag(R.id.v_required)));
filled = filled && !isRequired;
}

} else if (childView instanceof MaterialSpinner) {
boolean isRequired = (childView.getTag(R.id.v_required) != null
&& (boolean) childView.getTag(R.id.v_required)
&& childView.isEnabled());
boolean isEmpty = (((MaterialSpinner) childView).getSelectedItemPosition() == 0
|| ((MaterialSpinner) childView).getSelectedItemPosition() == AdapterView.INVALID_POSITION);

filled = filled && (!isRequired || !isEmpty);

} else if (childView instanceof ViewGroup
&& childView.getTag(R.id.is_checkbox_linear_layout) != null
&& Boolean.TRUE.equals(childView.getTag(R.id.is_checkbox_linear_layout))) {
filled = filled && CheckBoxFactory.isValid((LinearLayout) childView);

} else if (childView instanceof ViewGroup
&& childView.getTag(R.id.is_number_selector_linear_layout) != null
&& Boolean.TRUE.equals(childView.getTag(R.id.is_number_selector_linear_layout))) {
filled = filled && NumberSelectorFactory.isValid((ViewGroup) childView);
}
}

return filled;
}

public void validateAndWriteValues() {
for (View childView : formFragment.getJsonApi().getFormDataViews()) {
ValidationStatus validationStatus = validateView(childView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public static ValidationStatus validate(JsonFormFragmentView formFragmentView, L
return new ValidationStatus(true, null, formFragmentView, checkboxLinearLayout);
}

public static boolean isValid(final LinearLayout checkboxLinearLayout){
boolean isRequired = checkboxLinearLayout.isEnabled() && checkboxLinearLayout.getTag(R.id.error) != null;
return !isRequired || performValidation(checkboxLinearLayout);
}

private static boolean performValidation(LinearLayout checkboxLinearLayout) {
//Iterate through child layouts skipping first which is the label for the checkbox factory
boolean isChecked = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ private static void addSecondaryValue(JSONObject item, Calendar calendarDate) th
}
}

public static boolean isValid(final RadioGroup radioGroup) {
String error = (String) radioGroup.getTag(R.id.error);
return (error == null || !radioGroup.isEnabled()
|| radioGroup.isEnabled() && performValidation(radioGroup));
}

public static ValidationStatus validate(final JsonFormFragmentView formFragmentView, RadioGroup radioGroup) {
final String error = (String) radioGroup.getTag(R.id.error);
if (radioGroup.isEnabled() && error != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ public static void setSelectedTextViewText(String viewText) {
selectedTextView.setText(viewText);
}

public static boolean isValid(ViewGroup childAt){
boolean isRequired = Boolean.parseBoolean((String) childAt.getTag(R.id.v_required));
String selectedNumber = (String) childAt.getTag(R.id.selected_number_value);

return !(isRequired && TextUtils.isEmpty(selectedNumber) && (childAt.getChildCount() != 0 && childAt.getVisibility() == View.VISIBLE));
}

public static ValidationStatus validate(JsonFormFragmentView formFragmentView, ViewGroup childAt) {

boolean isRequired = Boolean.valueOf((String) childAt.getTag(R.id.v_required));
Expand Down
Loading

0 comments on commit 64defd0

Please sign in to comment.