diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java index 8036cc62c..1a9b69aed 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java @@ -314,7 +314,7 @@ public static class BARCODE_CONSTANTS { public static final int RC_HANDLE_GMS = 9001; } - public static class OPTIBPCONSTANTS { + public static class OptibpConstants { public static final String OPTIBP_WIDGET = "optibp"; public static final int OPTIBP_REQUEST_CODE = 301; public static final int OPTIBP_REPEAT_REQUEST_CODE = 302; @@ -337,7 +337,6 @@ public static class OPTIBPCONSTANTS { public static final String MODEL = "model"; public static final String HEIGHT = "height"; public static final String WEIGHT = "weight"; - public static final String COMPERATIVES = "comperatives"; public static final String CURRENTWEIGHT = "current_weight"; } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java index df5bcf114..8fdb641be 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java @@ -149,7 +149,7 @@ protected void registerWidgets() { map.put(JsonFormConstants.EXTENDED_RADIO_BUTTON, new ExtendedRadioButtonWidgetFactory()); map.put(JsonFormConstants.EXPANSION_PANEL, new ExpansionPanelFactory()); map.put(JsonFormConstants.MULTI_SELECT_LIST, new MultiSelectListFactory()); - map.put(JsonFormConstants.OPTIBPCONSTANTS.OPTIBP_WIDGET, new OptiBPWidgetFactory()); + map.put(JsonFormConstants.OptibpConstants.OPTIBP_WIDGET, new OptiBPWidgetFactory()); } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java index ed7cde5aa..af7f94281 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java @@ -304,9 +304,9 @@ public static void updateEndProperties(PropertyManager propertyManager, JSONObje public static JSONObject createOptiBPDataObject(String clientId, String clientOpenSRPId, String calibration) throws JSONException { JSONObject jsonObject = new JSONObject(); - jsonObject.put(JsonFormConstants.OPTIBPCONSTANTS.OPTIBP_KEY_CLIENT_ID, clientId); - jsonObject.put(JsonFormConstants.OPTIBPCONSTANTS.OPTIBP_KEY_CLIENT_OPENSRP_ID, clientOpenSRPId); - jsonObject.put(JsonFormConstants.OPTIBPCONSTANTS.CALIBRATION, calibration); + jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_ID, clientId); + jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID, clientOpenSRPId); + jsonObject.put(JsonFormConstants.OptibpConstants.CALIBRATION, calibration); return jsonObject; } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java index 5346fb216..619e85e09 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java @@ -34,7 +34,7 @@ import com.rey.material.util.ViewUtil; import com.rey.material.widget.Button; import com.vijay.jsonwizard.R; -import com.vijay.jsonwizard.constants.JsonFormConstants.OPTIBPCONSTANTS; +import com.vijay.jsonwizard.constants.JsonFormConstants.OptibpConstants; import com.vijay.jsonwizard.domain.WidgetArgs; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; @@ -144,9 +144,8 @@ private Button initLaunchButton(LinearLayout rootLayout, final WidgetArgs widget launchButton.setOnClickListener(view -> { try { Timber.w(" ONCLICK WITH JSON %s", jsonObject); - Intent intent = new Intent(OPTIBPCONSTANTS.OPTIBP_LAUNCH_INTENT); + Intent intent = new Intent(OptibpConstants.OPTIBP_LAUNCH_INTENT); intent.setType("text/json"); - Timber.e("OptiBP Calibration Request: %s ", getInputJsonString(context, jsonObject, widgetArgs)); intent.putExtra(Intent.EXTRA_TEXT, getInputJsonString(context, jsonObject, widgetArgs)); ((Activity) context).startActivityForResult(Intent.createChooser(intent, ""), requestCode); } catch (Exception e) { @@ -166,16 +165,16 @@ private Button initLaunchButton(LinearLayout rootLayout, final WidgetArgs widget } private void formatButtonWidget(Button button, JSONObject jsonObject) throws JSONException { - if (jsonObject.has(OPTIBPCONSTANTS.OPTIBP_KEY_BUTTON_BG_COLOR)) { - String colorString = jsonObject.getString(OPTIBPCONSTANTS.OPTIBP_KEY_BUTTON_BG_COLOR); + if (jsonObject.has(OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR)) { + String colorString = jsonObject.getString(OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR); setButtonBgColor(button, colorString); } - if (jsonObject.has(OPTIBPCONSTANTS.OPTIBP_KEY_BUTTON_TEXT_COLOR)) { - String colorString = jsonObject.getString(OPTIBPCONSTANTS.OPTIBP_KEY_BUTTON_TEXT_COLOR); + if (jsonObject.has(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR)) { + String colorString = jsonObject.getString(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR); button.setTextColor(Color.parseColor(colorString)); } - if (jsonObject.has(OPTIBPCONSTANTS.OPTIBP_KEY_BUTTON_TEXT)) { - String buttonText = jsonObject.getString(OPTIBPCONSTANTS.OPTIBP_KEY_BUTTON_TEXT); + if (jsonObject.has(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT)) { + String buttonText = jsonObject.getString(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT); button.setText(buttonText); } @@ -187,14 +186,14 @@ public void setUpOptiBpActivityResultListener(final WidgetArgs widgetArgs, int r final JsonApi jsonApi = (JsonApi) context; jsonApi.addOnActivityResultListener(requestCode, (finalRequestCode, resultCode, data) -> { if (resultCode == Activity.RESULT_OK) { - if (finalRequestCode == OPTIBPCONSTANTS.OPTIBP_REQUEST_CODE || - finalRequestCode == OPTIBPCONSTANTS.OPTIBP_REPEAT_REQUEST_CODE) { + if (finalRequestCode == OptibpConstants.OPTIBP_REQUEST_CODE || + finalRequestCode == OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE) { try { if (data != null) { String resultJson = data.getStringExtra(Intent.EXTRA_TEXT); Timber.d("Resultant OptiBP JSON: %s ", resultJson); populateBPEditTextValues(resultJson, systolicEditText, diastolicEditText, widgetArgs); - String resultString = getComparatives(resultJson); + String resultString = getValueString(resultJson); if (StringUtils.isNotBlank(resultString)) { writeResult(jsonApi, rootLayout, resultString, widgetArgs); } else { @@ -271,29 +270,27 @@ private void toggleEditTextEnabled(EditText editText, boolean enabled) { protected String getBPValue(String resultJsonString, BPFieldType field) throws JSONException { if (resultJsonString != null) { JSONObject jsonObject = new JSONObject(resultJsonString); - JSONArray result = jsonObject.getJSONArray(OPTIBPCONSTANTS.OPTIBP_REPORT_RESULT); + JSONArray result = jsonObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_RESULT); JSONObject resultObject = result.getJSONObject(0); - JSONArray component = resultObject.getJSONArray(OPTIBPCONSTANTS.OPTIBP_REPORT_COMPONENT); + JSONArray component = resultObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_COMPONENT); JSONObject bpComponent = ((JSONObject) component.get(BPFieldType.SYSTOLIC_BP.equals(field) ? 1 : 0)); - JSONObject valueQuantity = bpComponent.getJSONObject(OPTIBPCONSTANTS.OPTIBP_REPORT_VALUE_QUANTITY); + JSONObject valueQuantity = bpComponent.getJSONObject(OptibpConstants.OPTIBP_REPORT_VALUE_QUANTITY); int value = valueQuantity.getInt(VALUE); return String.valueOf(value); } return null; } - protected String getComparatives(String resultJsonString) throws JSONException { + protected String getValueString(String resultJsonString) throws JSONException { if (resultJsonString != null) { JSONObject jsonObject = new JSONObject(resultJsonString); - JSONArray result = jsonObject.getJSONArray(OPTIBPCONSTANTS.OPTIBP_REPORT_RESULT); + JSONArray result = jsonObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_RESULT); JSONObject resultObject = result.getJSONObject(0); - JSONArray component = resultObject.getJSONArray(OPTIBPCONSTANTS.OPTIBP_REPORT_COMPONENT); + JSONArray component = resultObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_COMPONENT); JSONObject secondIndex = component.optJSONObject(2); - String valueString = secondIndex.optString(OPTIBPCONSTANTS.OPTIBP_VALUE_STRING); - JSONObject valueObject = new JSONObject(valueString); - JSONArray returnArray = valueObject.optJSONArray(OPTIBPCONSTANTS.COMPERATIVES); - Timber.d("Comparative Array from OPtibp: %s", returnArray.toString()); - return returnArray.toString(); + String valueString = secondIndex.optString(OptibpConstants.OPTIBP_VALUE_STRING); + Timber.d("Comparative Array from OPtibp: %s", valueString); + return valueString; } return null; @@ -311,7 +308,7 @@ protected EditText getBPEditTextField(WidgetArgs widgetArgs, BPFieldType field) } private int getRequestCode(boolean isRepeat) { - return isRepeat ? OPTIBPCONSTANTS.OPTIBP_REPEAT_REQUEST_CODE : OPTIBPCONSTANTS.OPTIBP_REQUEST_CODE; + return isRepeat ? OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE : OptibpConstants.OPTIBP_REQUEST_CODE; } @SuppressWarnings("SameParameterValue") @@ -320,60 +317,46 @@ private boolean isRepeatMeasurement(BPFieldType systolicField, BPFieldType diast } protected String getInputJsonString(Context context, JSONObject jsonObject, WidgetArgs widgetArgs) throws JSONException { - if (!jsonObject.has(OPTIBPCONSTANTS.OPTIBP_KEY_DATA)) { + if (!jsonObject.has(OptibpConstants.OPTIBP_KEY_DATA)) { throw new JSONException(context.getString(R.string.missing_client_info)); } - JSONObject optiBPData = jsonObject.getJSONObject(OPTIBPCONSTANTS.OPTIBP_KEY_DATA); - if (!optiBPData.has(OPTIBPCONSTANTS.OPTIBP_KEY_CLIENT_ID) - || !optiBPData.has(OPTIBPCONSTANTS.OPTIBP_KEY_CLIENT_OPENSRP_ID)) { + JSONObject optiBPData = jsonObject.getJSONObject(OptibpConstants.OPTIBP_KEY_DATA); + if (!optiBPData.has(OptibpConstants.OPTIBP_KEY_CLIENT_ID) + || !optiBPData.has(OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID)) { throw new JSONException(context.getString(R.string.missing_client_info)); } - if (!optiBPData.has(OPTIBPCONSTANTS.CALIBRATION)) { - throw new JSONException(context.getString(R.string.calibration_data)); - } - if (TextUtils.isEmpty(optiBPData.getString(OPTIBPCONSTANTS.OPTIBP_KEY_CLIENT_ID)) - || TextUtils.isEmpty(optiBPData.getString(OPTIBPCONSTANTS.OPTIBP_KEY_CLIENT_OPENSRP_ID))) { + if (TextUtils.isEmpty(optiBPData.getString(OptibpConstants.OPTIBP_KEY_CLIENT_ID)) + || TextUtils.isEmpty(optiBPData.getString(OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID))) { throw new JSONException(context.getString(R.string.missing_client_info)); } - - /*** - * Removing the key and value to add add extra data - */ - optiBPData.remove(OPTIBPCONSTANTS.CALIBRATION); /*** * Adding new calibration data here */ appendHealthData(optiBPData, widgetArgs); - optiBPData.put(OPTIBPCONSTANTS.CALIBRATION, getCalibrationData(optiBPData.optString(OPTIBPCONSTANTS.CALIBRATION))); + optiBPData.put(OptibpConstants.CALIBRATION, getCalibrationData(optiBPData.optString(OptibpConstants.CALIBRATION))); return optiBPData.toString(); } private JSONArray getCalibrationData(String calibration) { try { - if (StringUtils.isBlank(calibration)) { - return null; - } - JSONArray calibrationArray = new JSONArray(); - JSONObject calibrationJson = new JSONObject(); if (Utils.checkIfValidJsonArray(calibration)) { - calibrationJson.put(OPTIBPCONSTANTS.COMPERATIVES, new JSONArray(calibration)); - } else { - calibrationJson.put(OPTIBPCONSTANTS.COMPERATIVES, new JSONArray()); + return new JSONArray(calibration); } - return calibrationArray.put(calibrationJson); - } catch (JSONException e) { + return StringUtils.isBlank(calibration) ? null : new JSONArray().put(new JSONObject(calibration)); + } catch (Exception e) { Timber.e(e); return null; } + } private void appendHealthData(JSONObject returnObject, WidgetArgs widgetArgs) { try { - JSONObject currentHeight = getSingleStepJsonObject(widgetArgs, STEP1, OPTIBPCONSTANTS.HEIGHT); - JSONObject currentWeight = getSingleStepJsonObject(widgetArgs, STEP1, OPTIBPCONSTANTS.CURRENTWEIGHT); + JSONObject currentHeight = getSingleStepJsonObject(widgetArgs, STEP1, OptibpConstants.HEIGHT); + JSONObject currentWeight = getSingleStepJsonObject(widgetArgs, STEP1, OptibpConstants.CURRENTWEIGHT); if (currentHeight != null && currentWeight != null) { - returnObject.put(OPTIBPCONSTANTS.HEIGHT, Integer.parseInt(currentHeight.optString(VALUE))); - returnObject.put(OPTIBPCONSTANTS.WEIGHT, Integer.parseInt(currentWeight.optString(VALUE))); + returnObject.put(OptibpConstants.HEIGHT, Integer.parseInt(currentHeight.optString(VALUE))); + returnObject.put(OptibpConstants.WEIGHT, Integer.parseInt(currentWeight.optString(VALUE))); } } catch (JSONException e) { Timber.e(e); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java index 899dab969..6d93663c8 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java @@ -10,6 +10,7 @@ import com.rey.material.widget.Button; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; +import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.domain.WidgetArgs; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; @@ -21,6 +22,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; import org.robolectric.RuntimeEnvironment; @@ -230,7 +232,7 @@ public class OptiBpWidgetFactoryTest extends FactoryTest { " ]," + " \"optibp_data\": {\n" + " \"clientId\": \"sampleClientId\",\n" + - " \"clientOpenSRPId\": \"sampleClientOpenSRPId\"\n" + + " \"clientOpenSRPId\": \"sampleClientOpenSRPId\",\"calibration\":\"\"\n" + " }\n" + " },\n" + " {\n" + @@ -376,9 +378,7 @@ public void testInputJson() throws JSONException { Mockito.doReturn(formFragment).when(widgetArgs).getFormFragment(); Mockito.doReturn(jsonApi).when(formFragment).getJsonApi(); Mockito.doReturn(new JSONObject(formString)).when(jsonApi).getmJSONObject(); - - String inputJson = factorySpy.getInputJsonString(RuntimeEnvironment.application.getApplicationContext(), new JSONObject(optiBPWidgetString), widgetArgs); - + String inputJson = factorySpy.getInputJsonString(jsonFormActivity, new JSONObject(optiBPWidgetString), widgetArgs); Assert.assertEquals(inputJson, "{\"clientId\":\"sampleClientId\",\"clientOpenSRPId\":\"sampleClientOpenSRPId\",\"calibration\":[{\"date\":\"2019-03-26T11:20:33+0800\",\"model\":\"device model\",\"height\":70,\"weight\":180,\"comperatives\":[{\"systolic\":120,\"diastolic\":80,\"cuffSystolic\":120,\"cuffDiastolic\":80,\"features\":{\"$key\":\"0.2f\"}}]}]}"); } @@ -402,6 +402,8 @@ public void testPopulateETValues() throws JSONException { Assert.assertNotNull(factorySpy); EditText sbp = Mockito.mock(EditText.class); EditText dbp = Mockito.mock(EditText.class); + widgetArgs=Mockito.mock(WidgetArgs.class); + Mockito.doReturn(new JSONObject(formString)).when(widgetArgs).getFormFragment().getJsonApi().getStep(ArgumentMatchers.anyString()).optString(JsonFormConstants.FIELDS); factorySpy.populateBPEditTextValues(resultJson, sbp, dbp,widgetArgs); Mockito.verify(sbp).setEnabled(false); Mockito.verify(dbp).setEnabled(false); diff --git a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java index 5b94022b9..02284dcde 100644 --- a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java +++ b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java @@ -210,12 +210,12 @@ public void startForm(int jsonFormActivityRequestCode, String formName, String e for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); if (jsonObject.getString(KEY).equalsIgnoreCase("optipb_widget1")) { - if (jsonObject.has(JsonFormConstants.OPTIBPCONSTANTS.OPTIBP_KEY_DATA)) { - jsonObject.remove(JsonFormConstants.OPTIBPCONSTANTS.OPTIBP_KEY_DATA); + if (jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA)) { + jsonObject.remove(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA); } JSONObject optiBPData = FormUtils.createOptiBPDataObject("46ccd2e0-bbec-4e4a-8f73-972a2f1f95ea", "1272326657",""); - jsonObject.put(JsonFormConstants.OPTIBPCONSTANTS.OPTIBP_KEY_DATA, optiBPData); + jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA, optiBPData); break; } }