Skip to content

Commit 348336a

Browse files
Merge pull request #476 from OpenSRP/save-repeating-group-count
Save repeating group count to obs
2 parents 3fee953 + 463292f commit 348336a

File tree

3 files changed

+174
-66
lines changed

3 files changed

+174
-66
lines changed

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/RepeatingGroupFactory.java

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.vijay.jsonwizard.presenters.JsonFormFragmentPresenter;
3232
import com.vijay.jsonwizard.task.AttachRepeatingGroupTask;
3333
import com.vijay.jsonwizard.utils.FormUtils;
34+
import com.vijay.jsonwizard.utils.ValidationStatus;
3435
import com.vijay.jsonwizard.validators.edittext.MaxNumericValidator;
3536
import com.vijay.jsonwizard.validators.edittext.MinNumericValidator;
3637
import com.vijay.jsonwizard.validators.edittext.RequiredValidator;
@@ -51,6 +52,11 @@
5152

5253
import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS;
5354
import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY;
55+
import static com.vijay.jsonwizard.constants.JsonFormConstants.OPENMRS_ENTITY;
56+
import static com.vijay.jsonwizard.constants.JsonFormConstants.OPENMRS_ENTITY_ID;
57+
import static com.vijay.jsonwizard.constants.JsonFormConstants.OPENMRS_ENTITY_PARENT;
58+
import static com.vijay.jsonwizard.constants.JsonFormConstants.TEXT;
59+
import static com.vijay.jsonwizard.constants.JsonFormConstants.TYPE;
5460
import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE;
5561

5662
/**
@@ -59,31 +65,29 @@
5965
public class RepeatingGroupFactory implements FormWidgetFactory {
6066

6167
private static Map<Integer, String> repeatingGroupLayouts = new HashMap<>();
62-
private final String REFERENCE_EDIT_TEXT_HINT = "reference_edit_text_hint";
68+
public static final String REFERENCE_EDIT_TEXT_HINT = "reference_edit_text_hint";
6369
private final String REPEATING_GROUP_LABEL = "repeating_group_label";
6470
private final String REFERENCE_EDIT_TEXT = "reference_edit_text";
6571
private final String REPEATING_GROUP_MAX = "repeating_group_max";
6672
private final String REPEATING_GROUP_MIN = "repeating_group_min";
6773
protected int MAX_NUM_REPEATING_GROUPS = 35;
6874
protected int MIN_NUM_REPEATING_GROUPS = 0;
69-
private ImageButton doneButton;
70-
private WidgetArgs widgetArgs;
75+
7176

7277
@Override
7378
public List<View> getViewsFromJson(final String stepName, final Context context, final JsonFormFragment formFragment, final JSONObject jsonObject, final CommonListener listener, final boolean popup) throws Exception {
7479
List<View> views = new ArrayList<>(1);
7580
LinearLayout rootLayout = getRootLayout(context);
7681

7782
final int rootLayoutId = View.generateViewId();
78-
doneButton = rootLayout.findViewById(R.id.btn_repeating_group_done);
7983

8084
rootLayout.setId(rootLayoutId);
8185
views.add(rootLayout);
8286

8387
JSONArray repeatingGroupLayout = jsonObject.getJSONArray(VALUE);
8488
repeatingGroupLayouts.put(rootLayoutId, repeatingGroupLayout.toString());
8589

86-
this.widgetArgs = new WidgetArgs()
90+
final WidgetArgs widgetArgs = new WidgetArgs()
8791
.withStepName(stepName)
8892
.withContext(context)
8993
.withFormFragment(formFragment)
@@ -92,15 +96,19 @@ public List<View> getViewsFromJson(final String stepName, final Context context,
9296
.withPopup(popup);
9397

9498
final MaterialEditText referenceEditText = rootLayout.findViewById(R.id.reference_edit_text);
99+
final ImageButton doneButton = rootLayout.findViewById(R.id.btn_repeating_group_done);
100+
95101
final String referenceEditTextHint = jsonObject.optString(REFERENCE_EDIT_TEXT_HINT, context.getString(R.string.enter_number_of_repeating_group_items));
96102
final String repeatingGroupLabel = jsonObject.optString(REPEATING_GROUP_LABEL, context.getString(R.string.repeating_group_item));
97103
String remoteReferenceEditText = jsonObject.optString(REFERENCE_EDIT_TEXT);
98104

99-
setRepeatingGroupNumLimits();
105+
setRepeatingGroupNumLimits(widgetArgs);
100106

101107
// Enables us to fetch this value from a previous edit_text & disable this one
102-
retrieveRepeatingGroupCountFromRemoteReferenceEditText(rootLayout, (JsonApi) context, referenceEditText, remoteReferenceEditText);
103-
setUpReferenceEditText(referenceEditText, referenceEditTextHint, repeatingGroupLabel);
108+
retrieveRepeatingGroupCountFromRemoteReferenceEditText(rootLayout, getJsonApi(widgetArgs),
109+
referenceEditText, remoteReferenceEditText, doneButton, widgetArgs);
110+
setUpReferenceEditText(doneButton, referenceEditText, referenceEditTextHint,
111+
repeatingGroupLabel, getRepeatingGroupCountObj(widgetArgs), widgetArgs);
104112

105113
// Disable the done button if the reference edit text being used is remote & has a valid value
106114
if (isRemoteReferenceValueUsed(referenceEditText)) {
@@ -109,18 +117,46 @@ public List<View> getViewsFromJson(final String stepName, final Context context,
109117
doneButton.setOnClickListener(new View.OnClickListener() {
110118
@Override
111119
public void onClick(View v) {
112-
addOnDoneAction(referenceEditText);
120+
addOnDoneAction(referenceEditText, doneButton, widgetArgs);
113121
}
114122
});
115123
}
116124

117125
((JsonApi) context).addFormDataView(referenceEditText);
118-
setViewTags(rootLayout);
119-
prepareViewChecks(rootLayout, context);
126+
setViewTags(rootLayout, widgetArgs);
127+
prepareViewChecks(rootLayout, context, widgetArgs);
120128

121129
return views;
122130
}
123131

132+
/**
133+
* Returns the object that holds the repeating group count
134+
*
135+
* @return
136+
* @throws JSONException
137+
*/
138+
private JSONObject getRepeatingGroupCountObj(WidgetArgs widgetArgs) throws JSONException {
139+
140+
String repeatingGroupCountObjKey = widgetArgs.getJsonObject().get(KEY) + "_count";
141+
JSONArray stepFields = getStepFields(getJsonApi(widgetArgs).getStep(widgetArgs.getStepName()));
142+
JSONObject repeatingGroupCountObj = FormUtils.getFieldJSONObject(stepFields, repeatingGroupCountObjKey);
143+
// prevents re-adding the count object during form traversals
144+
if (repeatingGroupCountObj != null) {
145+
return repeatingGroupCountObj;
146+
}
147+
148+
repeatingGroupCountObj = new JSONObject();
149+
repeatingGroupCountObj.put(KEY, repeatingGroupCountObjKey);
150+
repeatingGroupCountObj.put(OPENMRS_ENTITY_PARENT, "");
151+
repeatingGroupCountObj.put(OPENMRS_ENTITY, "");
152+
repeatingGroupCountObj.put(OPENMRS_ENTITY_ID, "");
153+
repeatingGroupCountObj.put(TYPE, "");
154+
repeatingGroupCountObj.put(TEXT, widgetArgs.getJsonObject().get(REFERENCE_EDIT_TEXT_HINT));
155+
stepFields.put(repeatingGroupCountObj);
156+
157+
return repeatingGroupCountObj;
158+
}
159+
124160
@Override
125161
public List<View> getViewsFromJson(final String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception {
126162
return getViewsFromJson(stepName, context, formFragment, jsonObject, listener, false);
@@ -139,7 +175,7 @@ protected LinearLayout getRootLayout(Context context) {
139175
/**
140176
* Sets min and max number of repeating groups
141177
*/
142-
private void setRepeatingGroupNumLimits() {
178+
private void setRepeatingGroupNumLimits(WidgetArgs widgetArgs) {
143179
try {
144180
MIN_NUM_REPEATING_GROUPS = widgetArgs.getJsonObject().optInt(REPEATING_GROUP_MIN, MIN_NUM_REPEATING_GROUPS);
145181
MAX_NUM_REPEATING_GROUPS = widgetArgs.getJsonObject().optInt(REPEATING_GROUP_MAX, MAX_NUM_REPEATING_GROUPS);
@@ -148,7 +184,7 @@ private void setRepeatingGroupNumLimits() {
148184
}
149185
}
150186

151-
private void setViewTags(@NonNull LinearLayout rootLayout) {
187+
private void setViewTags(@NonNull LinearLayout rootLayout, WidgetArgs widgetArgs) {
152188
JSONArray canvasIds = new JSONArray();
153189
canvasIds.put(rootLayout.getId());
154190
rootLayout.setTag(R.id.canvas_ids, canvasIds.toString());
@@ -158,7 +194,7 @@ private void setViewTags(@NonNull LinearLayout rootLayout) {
158194
rootLayout.setTag(R.id.address, widgetArgs.getStepName() + ":" + widgetArgs.getJsonObject().optString(KEY));
159195
}
160196

161-
private void prepareViewChecks(@NonNull LinearLayout view, @NonNull Context context) {
197+
private void prepareViewChecks(@NonNull LinearLayout view, @NonNull Context context, WidgetArgs widgetArgs) {
162198
String relevance = widgetArgs.getJsonObject().optString(JsonFormConstants.RELEVANCE);
163199
String constraints = widgetArgs.getJsonObject().optString(JsonFormConstants.CONSTRAINTS);
164200
String calculation = widgetArgs.getJsonObject().optString(JsonFormConstants.CALCULATION);
@@ -179,7 +215,13 @@ private void prepareViewChecks(@NonNull LinearLayout view, @NonNull Context cont
179215
}
180216
}
181217

182-
private void retrieveRepeatingGroupCountFromRemoteReferenceEditText(final @NonNull View rootLayout, @NonNull JsonApi context, @NonNull final MaterialEditText referenceEditText, @Nullable String remoteReferenceEditTextAddress) throws JSONException {
218+
private void retrieveRepeatingGroupCountFromRemoteReferenceEditText(final @NonNull View rootLayout,
219+
@NonNull JsonApi context,
220+
@NonNull final MaterialEditText referenceEditText,
221+
@Nullable String remoteReferenceEditTextAddress,
222+
@NonNull final ImageButton doneButton,
223+
@NonNull final WidgetArgs widgetArgs) throws JSONException {
224+
183225
if (!TextUtils.isEmpty(remoteReferenceEditTextAddress) && remoteReferenceEditTextAddress.contains(":")) {
184226
String finalRemoteReferenceEditTextAddress = remoteReferenceEditTextAddress.trim();
185227
String[] addressSections = finalRemoteReferenceEditTextAddress.split(":");
@@ -190,7 +232,7 @@ private void retrieveRepeatingGroupCountFromRemoteReferenceEditText(final @NonNu
190232
JSONObject stepJsonObject = context.getmJSONObject().optJSONObject(remoteReferenceEditTextStep);
191233

192234
if (stepJsonObject != null) {
193-
JSONArray fields = stepJsonObject.optJSONArray(FIELDS);
235+
JSONArray fields = getStepFields(stepJsonObject);
194236

195237
for (int i = 0; i < fields.length(); i++) {
196238
JSONObject stepField = fields.optJSONObject(i);
@@ -205,9 +247,11 @@ private void retrieveRepeatingGroupCountFromRemoteReferenceEditText(final @NonNu
205247
// Start the repeating groups
206248
Object visibilityTag = rootLayout.getTag(R.id.relevance_decided);
207249
if (visibilityTag != null && (boolean) visibilityTag) {
208-
attachRepeatingGroup(referenceEditText.getParent().getParent(), remoteReferenceValue);
250+
attachRepeatingGroup(referenceEditText.getParent().getParent(),
251+
remoteReferenceValue, doneButton, widgetArgs);
209252
} else {
210-
setGlobalLayoutListener(rootLayout, referenceEditText, remoteReferenceValue);
253+
setGlobalLayoutListener(rootLayout, referenceEditText,
254+
remoteReferenceValue, doneButton, widgetArgs);
211255
}
212256
} catch (NumberFormatException ex) {
213257
Timber.e(ex);
@@ -221,22 +265,33 @@ private void retrieveRepeatingGroupCountFromRemoteReferenceEditText(final @NonNu
221265
}
222266

223267
@VisibleForTesting
224-
protected void setGlobalLayoutListener(@NonNull final View rootLayout, @NonNull final MaterialEditText referenceEditText, final int remoteReferenceValue) {
268+
protected void setGlobalLayoutListener(@NonNull final View rootLayout,
269+
@NonNull final MaterialEditText referenceEditText,
270+
@NonNull final int remoteReferenceValue,
271+
@NonNull final ImageButton doneButton,
272+
@NonNull final WidgetArgs widgetArgs) {
273+
225274
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
226275

227276
@Override
228277
public void onGlobalLayout() {
229278
Object visibilityTag = rootLayout.getTag(R.id.relevance_decided);
230279
if (visibilityTag != null && (boolean) visibilityTag) {
231-
attachRepeatingGroup(referenceEditText.getParent().getParent(), remoteReferenceValue);
280+
attachRepeatingGroup(referenceEditText.getParent().getParent(),
281+
remoteReferenceValue, doneButton, widgetArgs);
232282

233283
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
234284
}
235285
}
236286
});
237287
}
238288

239-
private void setUpReferenceEditText(final MaterialEditText referenceEditText, String referenceEditTextHint, String repeatingGroupLabel) throws JSONException {
289+
private void setUpReferenceEditText(final ImageButton doneButton,
290+
final MaterialEditText referenceEditText,
291+
final String referenceEditTextHint,
292+
final String repeatingGroupLabel,
293+
final JSONObject repeatingGroupCount,
294+
final WidgetArgs widgetArgs) throws JSONException {
240295
// We should disable this edit_text if another referenced edit text is being used
241296
Context context = widgetArgs.getContext();
242297
if (isRemoteReferenceValueUsed(referenceEditText)) {
@@ -246,7 +301,7 @@ private void setUpReferenceEditText(final MaterialEditText referenceEditText, St
246301
@Override
247302
public boolean onEditorAction(TextView focusTextView, int actionId, KeyEvent event) {
248303
if (actionId == EditorInfo.IME_ACTION_DONE) {
249-
addOnDoneAction(focusTextView);
304+
addOnDoneAction(focusTextView, doneButton, widgetArgs);
250305
return true;
251306
}
252307
return false;
@@ -255,7 +310,7 @@ public boolean onEditorAction(TextView focusTextView, int actionId, KeyEvent eve
255310
}
256311

257312
referenceEditText.setTag(R.id.address, widgetArgs.getStepName() + ":" + widgetArgs.getJsonObject().getString(KEY));
258-
attachTextChangedListener(referenceEditText);
313+
attachTextChangedListener(referenceEditText, doneButton, repeatingGroupCount, widgetArgs);
259314
referenceEditText.setHint(referenceEditTextHint);
260315
referenceEditText.setTag(R.id.repeating_group_label, repeatingGroupLabel);
261316
referenceEditText.setTag(R.id.extraPopup, false);
@@ -274,7 +329,11 @@ public boolean onEditorAction(TextView focusTextView, int actionId, KeyEvent eve
274329
}
275330
}
276331

277-
private void attachTextChangedListener(final MaterialEditText referenceEditText) {
332+
private void attachTextChangedListener(final MaterialEditText referenceEditText,
333+
final ImageButton doneButton,
334+
final JSONObject repeatingGroupCount,
335+
final WidgetArgs widgetArgs) {
336+
278337
referenceEditText.addTextChangedListener(new TextWatcher() {
279338
@Override
280339
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@@ -289,11 +348,23 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
289348
@Override
290349
public void afterTextChanged(Editable s) {
291350
doneButton.setImageResource(R.drawable.ic_done_grey);
292-
JsonFormFragmentPresenter.validate(widgetArgs.getFormFragment(), referenceEditText, false);
351+
ValidationStatus validationStatus = JsonFormFragmentPresenter
352+
.validate(widgetArgs.getFormFragment(), referenceEditText, false);
353+
if (validationStatus.isValid()) {
354+
writeJsonObjectValue(repeatingGroupCount, s.toString());
355+
}
293356
}
294357
});
295358
}
296359

360+
private void writeJsonObjectValue(JSONObject jsonObject, String value) {
361+
try {
362+
jsonObject.put(VALUE, value);
363+
} catch (JSONException e) {
364+
Timber.e(e);
365+
}
366+
}
367+
297368
private void addRequiredValidator(JSONObject jsonObject, MaterialEditText editText) throws JSONException {
298369
JSONObject requiredObject = jsonObject.optJSONObject(JsonFormConstants.V_REQUIRED);
299370
if (requiredObject != null) {
@@ -305,23 +376,26 @@ private void addRequiredValidator(JSONObject jsonObject, MaterialEditText editTe
305376
}
306377
}
307378

308-
protected void addOnDoneAction(TextView textView) {
379+
protected void addOnDoneAction(final TextView textView, final ImageButton doneButton, final WidgetArgs widgetArgs) {
309380
try {
310381
InputMethodManager inputMethodManager = (InputMethodManager) widgetArgs.getFormFragment().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
311382
inputMethodManager.hideSoftInputFromWindow(textView.getWindowToken(), 0);
312383
textView.clearFocus();
313-
attachRepeatingGroup(textView.getParent().getParent(), Integer.parseInt(textView.getText().toString()));
384+
attachRepeatingGroup(textView.getParent().getParent(),
385+
Integer.parseInt(textView.getText().toString()), doneButton, widgetArgs);
314386
} catch (Exception e) {
315387
Timber.e(e);
316388
}
317389
}
318390

319-
private void attachRepeatingGroup(final ViewParent parent, final int numRepeatingGroups) {
391+
private void attachRepeatingGroup(final ViewParent parent, final int numRepeatingGroups,
392+
final ImageButton doneButton, final WidgetArgs widgetArgs) {
320393
if (numRepeatingGroups > MAX_NUM_REPEATING_GROUPS) {
321394
return;
322395
}
323396

324-
new AttachRepeatingGroupTask(parent, numRepeatingGroups, repeatingGroupLayouts, widgetArgs, doneButton).execute();
397+
new AttachRepeatingGroupTask(parent, numRepeatingGroups, repeatingGroupLayouts,
398+
widgetArgs, doneButton).execute();
325399
}
326400

327401
protected int getLayout() {
@@ -337,4 +411,13 @@ public int remoteReferenceValue(@NonNull View referenceEditText) {
337411
Object tagValue = referenceEditText.getTag(R.id.repeating_group_remote_reference_value);
338412
return tagValue instanceof Integer ? (int) tagValue : 0;
339413
}
414+
415+
private JsonApi getJsonApi(WidgetArgs widgetArgs) {
416+
Context context = widgetArgs.getContext();
417+
return context instanceof JsonApi ? (JsonApi) context : null;
418+
}
419+
420+
private JSONArray getStepFields (JSONObject step) {
421+
return step.optJSONArray(FIELDS);
422+
}
340423
}

0 commit comments

Comments
 (0)