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 622335a2c..e9638f06d 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 @@ -232,6 +232,7 @@ public class JsonFormConstants { public static final String DEFAULT_FORMS_DIRECTORY = "opensrpNativeFormTester"; public static final String JSON_FORM_DIRECTORY = "json.form"; public static final String MEDIA_LOGIC = "media_logic"; + public static final String FIELDS_TO_USE_VALUE = "fields_to_use_value"; public static final String PROPERTIES_FILE_EXTENSION = ".properties"; public static final String JSON_FILE_EXTENSION = ".json"; @@ -312,6 +313,21 @@ public static class BARCODE_CONSTANTS { public static final int RC_HANDLE_GMS = 9001; } + 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; + public static final String OPTIBP_REPORT_RESULT = "result"; + public static final String OPTIBP_REPORT_COMPONENT = "component"; + public static final String OPTIBP_REPORT_VALUE_QUANTITY = "valueQuantity"; + public static final String OPTIBP_LAUNCH_INTENT = "android.intent.action.BLOOD_MEASURE"; + public static final String OPTIBP_KEY_DATA = "optibp_data"; + public static final String OPTIBP_KEY_CLIENT_ID = "clientId"; + public static final String OPTIBP_KEY_CLIENT_OPENSRP_ID = "clientOpenSRPId"; + public static final String OPTIBP_KEY_BUTTON_BG_COLOR = "optibp_button_bg_color"; + public static final String OPTIBP_KEY_BUTTON_TEXT_COLOR = "optibp_button_text_color"; + } + public static class EDIT_TEXT_TYPE { public static final String NUMBER = "number"; } 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 0e9f5eefb..f6d41cab3 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 @@ -33,6 +33,7 @@ import com.vijay.jsonwizard.widgets.NativeEditTextFactory; import com.vijay.jsonwizard.widgets.NativeRadioButtonFactory; import com.vijay.jsonwizard.widgets.NumberSelectorFactory; +import com.vijay.jsonwizard.widgets.OptiBPWidgetFactory; import com.vijay.jsonwizard.widgets.RadioButtonFactory; import com.vijay.jsonwizard.widgets.RepeatingGroupFactory; import com.vijay.jsonwizard.widgets.SectionFactory; @@ -147,6 +148,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()); } 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 714a67cc8..16f868580 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 @@ -299,6 +299,13 @@ public static void updateEndProperties(PropertyManager propertyManager, JSONObje } } + public static JSONObject createOptiBPDataObject(String clientId, String clientOpenSRPId) 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); + return jsonObject; + } + public Map createRadioButtonAndCheckBoxLabel(String stepName, LinearLayout linearLayout, JSONObject jsonObject, Context context, JSONArray canvasIds, final Boolean readOnly, diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/JsonFormMLSAssetGenerator.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/JsonFormMLSAssetGenerator.java index 26d0df1d8..1617d7cdb 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/JsonFormMLSAssetGenerator.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/JsonFormMLSAssetGenerator.java @@ -351,7 +351,7 @@ private static JsonFormInteractor getJsonFormInteractor() throws Exception { ? "com.vijay.jsonwizard.interactors.JsonFormInteractor" : jsonFormInteractorName; Class clazz = Class.forName(jsonFormInteractorName); Method factoryMethod = clazz.getDeclaredMethod("getInstance"); - return (JsonFormInteractor) factoryMethod.invoke(null, null); + return (JsonFormInteractor) factoryMethod.invoke(null, (Object[]) null); } public static void main(String[] args) throws Exception { diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/CheckBoxFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/CheckBoxFactory.java index 4cdf0d355..10b08f4ef 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/CheckBoxFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/CheckBoxFactory.java @@ -1,5 +1,6 @@ package com.vijay.jsonwizard.widgets; +import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.support.annotation.NonNull; @@ -32,6 +33,8 @@ import java.util.Map; import java.util.Set; +import timber.log.Timber; + import static com.vijay.jsonwizard.utils.FormUtils.MATCH_PARENT; import static com.vijay.jsonwizard.utils.FormUtils.WRAP_CONTENT; import static com.vijay.jsonwizard.utils.FormUtils.getCurrentCheckboxValues; @@ -84,8 +87,8 @@ public List getViewsFromJson(String stepName, Context context, JsonFormFra return attachJson(stepName, context, jsonObject, listener, formFragment, false); } - private List attachJson(String stepName, Context context, JSONObject jsonObject, CommonListener listener, JsonFormFragment formFragment, - boolean popup) throws JSONException { + private List attachJson(final String stepName, final Context context, final JSONObject jsonObject, final CommonListener listener, final JsonFormFragment formFragment, + final boolean popup) throws JSONException { String openMrsEntityParent = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_PARENT); String openMrsEntity = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY); String openMrsEntityId = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_ID); @@ -119,7 +122,6 @@ private List attachJson(String stepName, Context context, JSONObject jsonO Map labelViews = formUtils.createRadioButtonAndCheckBoxLabel(stepName, rootLayout, jsonObject, context, canvasIds, readOnly, listener, popup); - ArrayList editableCheckBoxes = addCheckBoxOptionsElements(jsonObject, context, readOnly, canvasIds, stepName, rootLayout, listener, popup); @@ -177,7 +179,7 @@ private ArrayList addCheckBoxOptionsElements(JSONObject jsonObject, Contex ArrayList checkBoxes = new ArrayList<>(); ArrayList checkboxLayouts = new ArrayList<>(); for (int i = 0; i < options.length(); i++) { - JSONObject item = options.getJSONObject(i); + final JSONObject item = options.getJSONObject(i); String openMrsEntityParent = item.optString(JsonFormConstants.OPENMRS_ENTITY_PARENT); String openMrsEntity = item.optString(JsonFormConstants.OPENMRS_ENTITY); @@ -205,14 +207,23 @@ private ArrayList addCheckBoxOptionsElements(JSONObject jsonObject, Contex checkboxLayout.setTag(R.id.type, jsonObject.getString(JsonFormConstants.TYPE) + JsonFormConstants.SUFFIX.PARENT); canvasIds.put(checkboxLayout.getId()); - if (StringUtils.isNotEmpty(item.optString(JsonFormConstants.VALUE))) { - checkBox.setChecked(Boolean.valueOf(item.optString(JsonFormConstants.VALUE))); - } - - //Preselect values if they exist - if (checkBoxValues != null && getCurrentCheckboxValues(checkBoxValues).contains(item.getString(JsonFormConstants.KEY))) { - checkBox.setChecked(true); - } + final JSONArray finalCheckBoxValues = checkBoxValues; + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run(){ + if (StringUtils.isNotEmpty(item.optString(JsonFormConstants.VALUE))) { + checkBox.setChecked(Boolean.parseBoolean(item.optString(JsonFormConstants.VALUE))); + } + //Preselect values if they exist + try { + if (finalCheckBoxValues != null && getCurrentCheckboxValues(finalCheckBoxValues).contains(item.getString(JsonFormConstants.KEY))) { + checkBox.setChecked(true); + } + } catch (JSONException e) { + Timber.e(e); + } + } + }); checkBox.setEnabled(!readOnly); if (i == options.length() - 1) { diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactory.java index 192e25eec..483d4c3d5 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/NativeRadioButtonFactory.java @@ -563,7 +563,12 @@ private void createRadioButton(RelativeLayout rootLayout, JSONObject jsonObject, if (!TextUtils.isEmpty(jsonObject.optString(JsonFormConstants.VALUE)) && jsonObject.optString(JsonFormConstants.VALUE).equals(item.getString(JsonFormConstants.KEY))) { - radioButton.setChecked(true); + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + radioButton.setChecked(true); + } + }); } String optionTextColor = JsonFormConstants.DEFAULT_TEXT_COLOR; if (item.has(JsonFormConstants.TEXT_COLOR)) { 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 new file mode 100644 index 000000000..db0d6760b --- /dev/null +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java @@ -0,0 +1,365 @@ +package com.vijay.jsonwizard.widgets; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.GradientDrawable; +import android.graphics.drawable.ShapeDrawable; +import android.support.v4.content.ContextCompat; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewTreeObserver; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +import com.rey.material.util.ViewUtil; +import com.rey.material.widget.Button; +import com.vijay.jsonwizard.R; +import com.vijay.jsonwizard.constants.JsonFormConstants; +import com.vijay.jsonwizard.domain.WidgetArgs; +import com.vijay.jsonwizard.fragments.JsonFormFragment; +import com.vijay.jsonwizard.interfaces.CommonListener; +import com.vijay.jsonwizard.interfaces.FormWidgetFactory; +import com.vijay.jsonwizard.interfaces.JsonApi; +import com.vijay.jsonwizard.interfaces.OnActivityResultListener; +import com.vijay.jsonwizard.utils.FormUtils; + +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import timber.log.Timber; + +public class OptiBPWidgetFactory implements FormWidgetFactory { + + @Override + public List getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception { + return getViewsFromJson(stepName, context, formFragment, jsonObject, listener, false); + } + + @Override + public List getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener, boolean popup) throws Exception { + WidgetArgs widgetArgs = new WidgetArgs(); + widgetArgs.withStepName(stepName).withContext(context).withFormFragment(formFragment) + .withJsonObject(jsonObject).withListener(listener).withPopup(popup); + + JSONArray canvasIds = new JSONArray(); + + boolean readOnly = false; + if (jsonObject.has(JsonFormConstants.READ_ONLY)) { + readOnly = jsonObject.getBoolean(JsonFormConstants.READ_ONLY); + } + + LinearLayout rootLayout = getRootLayout(context); + setWidgetTags(rootLayout, canvasIds, widgetArgs); + attachRefreshLogic(context, jsonObject, rootLayout); + + initBPFieldsKeys(jsonObject); + EditText systolicBPEditText = getBPEditTextField(widgetArgs, BPFieldType.SYSTOLIC_BP); + EditText diastolicBPEditText = getBPEditTextField(widgetArgs, BPFieldType.DIASTOLIC_BP); + + TextView labelTextView = initLabel(rootLayout, widgetArgs, readOnly); + setWidgetTags(labelTextView, canvasIds, widgetArgs); + + boolean isRepeat = isRepeatMeasurement(BPFieldType.SYSTOLIC_BP, BPFieldType.DIASTOLIC_BP); + Button launchButton = initLaunchButton(rootLayout, widgetArgs, readOnly, getRequestCode(isRepeat)); + setWidgetTags(launchButton, canvasIds, widgetArgs); + + setGlobalLayoutListener(rootLayout, systolicBPEditText, diastolicBPEditText); + setUpOptiBpActivityResultListener(widgetArgs, getRequestCode(isRepeat), rootLayout, systolicBPEditText, diastolicBPEditText); + + ((JsonApi) context).addFormDataView(rootLayout); + + rootLayout.setTag(R.id.canvas_ids, canvasIds.toString()); + + List views = new ArrayList<>(1); + views.add(rootLayout); + return views; + } + + private void setWidgetTags(View view, JSONArray canvasIds, WidgetArgs widgetArgs) throws JSONException { + JSONObject jsonObject = widgetArgs.getJsonObject(); + FormUtils.setViewOpenMRSEntityAttributes(jsonObject, view); + + view.setId(ViewUtil.generateViewId()); + view.setTag(R.id.key, jsonObject.getString(JsonFormConstants.KEY)); + view.setTag(R.id.type, widgetArgs.getJsonObject().getString(JsonFormConstants.TYPE)); + view.setTag(R.id.extraPopup, widgetArgs.isPopup()); + view.setTag(R.id.address, widgetArgs.getStepName() + ":" + jsonObject.getString(JsonFormConstants.KEY)); + canvasIds.put(view.getId()); + } + + private void initBPFieldsKeys(JSONObject jsonObject) throws JSONException { + if (jsonObject.has(JsonFormConstants.FIELDS_TO_USE_VALUE) + && jsonObject.getJSONArray(JsonFormConstants.FIELDS_TO_USE_VALUE).length() == 2) { + JSONArray fields = jsonObject.getJSONArray(JsonFormConstants.FIELDS_TO_USE_VALUE); + BPFieldType.SYSTOLIC_BP.setKey(fields.get(0).toString()); + BPFieldType.DIASTOLIC_BP.setKey(fields.get(1).toString()); + } else { + Timber.e("No field values defined to populate BP values"); + } + } + + private TextView initLabel(LinearLayout rootLayout, WidgetArgs widgetArgs, boolean readOnly) throws JSONException { + Context context = widgetArgs.getContext(); + TextView labelTextView = rootLayout.findViewById(R.id.optibp_label); + labelTextView.setText(getLabelText(context, widgetArgs.getJsonObject())); + if (readOnly) { + labelTextView.setTextColor(ContextCompat.getColor(context, android.R.color.darker_gray)); + } + return labelTextView; + } + + private Button initLaunchButton(LinearLayout rootLayout, final WidgetArgs widgetArgs, boolean readOnly, final int requestCode) throws JSONException { + final Context context = widgetArgs.getContext(); + final JSONObject jsonObject = widgetArgs.getJsonObject(); + Button launchButton = rootLayout.findViewById(R.id.optibp_launch_button); + formatButtonWidget(launchButton, widgetArgs.getJsonObject()); + launchButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + try { + Timber.w(" ONCLICK WITH JSON %s", jsonObject); + Intent intent = new Intent(JsonFormConstants.OptibpConstants.OPTIBP_LAUNCH_INTENT); + intent.setType("text/json"); + intent.putExtra(Intent.EXTRA_TEXT, getInputJsonString(context, jsonObject)); + ((Activity) context).startActivityForResult(Intent.createChooser(intent, ""), requestCode); + } catch (Exception e) { + Timber.e(e); + } + } + }); + + if (readOnly) { + launchButton.setBackgroundDrawable(new ColorDrawable(widgetArgs.getContext().getResources() + .getColor(android.R.color.darker_gray))); + launchButton.setClickable(false); + launchButton.setEnabled(false); + launchButton.setFocusable(false); + } + + return launchButton; + } + + private void formatButtonWidget(Button button, JSONObject jsonObject) throws JSONException { + if (jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR)) { + String colorString = jsonObject.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR); + setButtonBgColor(button, colorString); + } + if (jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR)) { + String colorString = jsonObject.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR); + button.setTextColor(Color.parseColor(colorString)); + } + } + + public void setUpOptiBpActivityResultListener(final WidgetArgs widgetArgs, int requestCode, final LinearLayout rootLayout, final EditText systolicEditText, final EditText diastolicEditText) { + final Context context = widgetArgs.getContext(); + if (context instanceof JsonApi) { + final JsonApi jsonApi = (JsonApi) context; + jsonApi.addOnActivityResultListener(requestCode, new OnActivityResultListener() { + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode == Activity.RESULT_OK) { + if (requestCode == JsonFormConstants.OptibpConstants.OPTIBP_REQUEST_CODE || + requestCode == JsonFormConstants.OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE) { + try { + if (data != null) { + try { + String resultJson = data.getStringExtra(Intent.EXTRA_TEXT); + Timber.d("Resultant OptiBP JSON: %s ", resultJson); + populateBPEditTextValues(resultJson, systolicEditText, diastolicEditText); + writeResult(jsonApi, rootLayout, resultJson, widgetArgs); + } catch (JSONException e) { + Timber.e(e); + } + + } else + Timber.i("NO RESULT FROM OPTIBP APP"); + } catch (Exception e) { + Timber.e(e); + } + } + } else { + Toast.makeText(context, context.getString(R.string.optibp_unable_to_receive), Toast.LENGTH_SHORT).show(); + } + } + }); + } + } + + private void setGlobalLayoutListener(final LinearLayout rootLayout, final EditText systolicBPEditText, final EditText diastolicBPEditText) { + if (systolicBPEditText != null && diastolicBPEditText != null) { + rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + if (rootLayout.getVisibility() != View.VISIBLE + && TextUtils.isEmpty(systolicBPEditText.getText()) + && TextUtils.isEmpty(diastolicBPEditText.getText())) { + Timber.i("OptiBP widget not visible"); + toggleEditTextEnabled(systolicBPEditText, true); + toggleEditTextEnabled(diastolicBPEditText, true); + } + } + }); + } + } + + private void setButtonBgColor(Button button, String colorString) { + Drawable background = button.getBackground(); + if (background instanceof ShapeDrawable) { + ((ShapeDrawable) background).getPaint().setColor(Color.parseColor(colorString)); + } else if (background instanceof GradientDrawable) { + ((GradientDrawable) background).setColor(Color.parseColor(colorString)); + } else if (background instanceof ColorDrawable) { + ((ColorDrawable) background).setColor(Color.parseColor(colorString)); + } + } + + private void writeResult(JsonApi jsonApi, LinearLayout rootLayout, String resultJson, WidgetArgs widgetArgs) throws JSONException { + // Write result JSON to widget value + String key = (String) rootLayout.getTag(R.id.key); + String openMrsEntityParent = (String) rootLayout.getTag(R.id.openmrs_entity_parent); + String openMrsEntity = (String) rootLayout.getTag(R.id.openmrs_entity); + String openMrsEntityId = (String) rootLayout.getTag(R.id.openmrs_entity_id); + jsonApi.writeValue(widgetArgs.getStepName(), key, resultJson, openMrsEntityParent, + openMrsEntity, openMrsEntityId, widgetArgs.isPopup()); + } + + protected void populateBPEditTextValues(String resultJsonJsonString, EditText systolicBPEditText, EditText diastolicBPEditText) throws JSONException { + if (systolicBPEditText != null) { + systolicBPEditText.setText(getBPValue(resultJsonJsonString, BPFieldType.SYSTOLIC_BP)); + toggleEditTextEnabled(systolicBPEditText, false); + } + if (diastolicBPEditText != null) { + diastolicBPEditText.setText(getBPValue(resultJsonJsonString, BPFieldType.DIASTOLIC_BP)); + toggleEditTextEnabled(diastolicBPEditText, false); + } + } + + private void toggleEditTextEnabled(EditText editText, boolean enabled) { + editText.setEnabled(enabled); + editText.setClickable(enabled); + editText.setFocusable(enabled); + editText.setFocusableInTouchMode(enabled); + } + + protected String getBPValue(String resultJsonString, BPFieldType field) throws JSONException { + JSONObject jsonObject = new JSONObject(resultJsonString); + JSONArray result = jsonObject.getJSONArray(JsonFormConstants.OptibpConstants.OPTIBP_REPORT_RESULT); + JSONObject resultObject = result.getJSONObject(0); + JSONArray component = resultObject.getJSONArray(JsonFormConstants.OptibpConstants.OPTIBP_REPORT_COMPONENT); + JSONObject bpComponent = ((JSONObject) component.get(BPFieldType.SYSTOLIC_BP.equals(field) ? 1 : 0)); + JSONObject valueQuantity = bpComponent.getJSONObject(JsonFormConstants.OptibpConstants.OPTIBP_REPORT_VALUE_QUANTITY); + int value = valueQuantity.getInt(JsonFormConstants.VALUE); + return String.valueOf(value); + } + + protected EditText getBPEditTextField(WidgetArgs widgetArgs, BPFieldType field) { + Context context = widgetArgs.getContext(); + EditText bpEditText = (EditText) widgetArgs.getFormFragment().getJsonApi().getFormDataView(widgetArgs.getStepName() + ":" + field.getKey()); + if (bpEditText == null) { + Toast.makeText(context, context.getString(R.string.optibp_values_error), Toast.LENGTH_SHORT).show(); + Timber.e(context.getString(R.string.optibp_values_error)); + return null; + } + return bpEditText; + } + + private int getRequestCode(boolean isRepeat) { + return isRepeat ? JsonFormConstants.OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE : JsonFormConstants.OptibpConstants.OPTIBP_REQUEST_CODE; + } + + @SuppressWarnings("SameParameterValue") + private boolean isRepeatMeasurement(BPFieldType systolicField, BPFieldType diastolicField) { + return systolicField.getKey().contains("repeat") && diastolicField.getKey().contains("repeat"); + } + + protected String getInputJsonString(Context context, JSONObject jsonObject) throws JSONException { + if (!jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA)) { + throw new JSONException(context.getString(R.string.missing_client_info)); + } + JSONObject optiBPData = jsonObject.getJSONObject(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA); + if (!optiBPData.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_ID) + || !optiBPData.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID)) { + throw new JSONException(context.getString(R.string.missing_client_info)); + } + if (TextUtils.isEmpty(optiBPData.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_ID)) + || TextUtils.isEmpty(optiBPData.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID))) { + throw new JSONException(context.getString(R.string.missing_client_info)); + } + return optiBPData.toString(); + } + + private String getLabelText(Context context, JSONObject jsonObject) throws JSONException { + if (!jsonObject.has(JsonFormConstants.LABEL)) { + return context.getString(R.string.optibp_label); + } + return jsonObject.getString(JsonFormConstants.LABEL); + } + + @SuppressLint("InflateParams") + public LinearLayout getRootLayout(Context context) { + return (LinearLayout) LayoutInflater.from(context).inflate(R.layout.native_form_item_optibp_widget, null); + } + + @Override + public @NotNull Set getCustomTranslatableWidgetFields() { + Set customTranslatableWidgetFields = new HashSet<>(); + customTranslatableWidgetFields.add(JsonFormConstants.LABEL); + return customTranslatableWidgetFields; + } + + private void attachRefreshLogic(Context context, JSONObject jsonObject, View view) { + String relevance = jsonObject.optString(JsonFormConstants.RELEVANCE); + String constraints = jsonObject.optString(JsonFormConstants.CONSTRAINTS); + String calculation = jsonObject.optString(JsonFormConstants.CALCULATION); + + if (StringUtils.isNotBlank(relevance) && context instanceof JsonApi) { + view.setTag(R.id.relevance, relevance); + ((JsonApi) context).addSkipLogicView(view); + } + + if (StringUtils.isNotBlank(constraints) && context instanceof JsonApi) { + view.setTag(R.id.constraints, constraints); + ((JsonApi) context).addConstrainedView(view); + } + + if (StringUtils.isNotBlank(calculation) && context instanceof JsonApi) { + view.setTag(R.id.calculation, calculation); + ((JsonApi) context).addCalculationLogicView(view); + } + } + + protected enum BPFieldType { + DIASTOLIC_BP("bp_diastolic"), SYSTOLIC_BP("bp_systolic"); // TODO -> Add these KEYS to explicit documentation + + private String key; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + BPFieldType(String key) { + this.key = key; + } + } +} + diff --git a/android-json-form-wizard/src/main/res/drawable/optbp_btn_bg.xml b/android-json-form-wizard/src/main/res/drawable/optbp_btn_bg.xml new file mode 100644 index 000000000..bf3b7b12b --- /dev/null +++ b/android-json-form-wizard/src/main/res/drawable/optbp_btn_bg.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/android-json-form-wizard/src/main/res/layout/native_form_item_optibp_widget.xml b/android-json-form-wizard/src/main/res/layout/native_form_item_optibp_widget.xml new file mode 100644 index 000000000..19dd91399 --- /dev/null +++ b/android-json-form-wizard/src/main/res/layout/native_form_item_optibp_widget.xml @@ -0,0 +1,32 @@ + + + + + + + \ No newline at end of file diff --git a/android-json-form-wizard/src/main/res/values/strings.xml b/android-json-form-wizard/src/main/res/values/strings.xml index 54509d95e..f1fd5b4ca 100644 --- a/android-json-form-wizard/src/main/res/values/strings.xml +++ b/android-json-form-wizard/src/main/res/values/strings.xml @@ -115,5 +115,10 @@ Sorry, a fatal error occurred when loading the form. Please contact a developer for further assistance. Please select one option Please make sure you have set the radio button options + Get Started + Unable to receive any result + Missing client info + Measure the blood pressure using OptiBP + No fields defined to populate BP values \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java index cbbc22be8..4ec146a1a 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java @@ -725,4 +725,11 @@ public void testGetDynamicLabelInfoList() throws JSONException { Assert.assertEquals(expectedList.get(0).getDynamicLabelTitle(), actualList.get(0).getDynamicLabelTitle()); Assert.assertEquals(expectedList.get(0).getDynamicLabelImageSrc(), actualList.get(0).getDynamicLabelImageSrc()); } + + @Test + public void testCreateOptiBPDataObject() throws JSONException { + JSONObject inputJson = FormUtils.createOptiBPDataObject("clientId", "clientOpenSRPId"); + + Assert.assertEquals(inputJson.toString(), "{\"clientId\":\"clientId\",\"clientOpenSRPId\":\"clientOpenSRPId\"}"); + } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java index f436a79f0..41f371fcb 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/CheckBoxFactoryTest.java @@ -109,4 +109,25 @@ public void testGetCustomTranslatableWidgetFields() { Assert.assertEquals(1, editableProperties.size()); Assert.assertEquals("options.text", editableProperties.iterator().next()); } + + + @Test + public void testPerformValidation() throws Exception { + Assert.assertNotNull(factory); + CheckBoxFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(rootLayout); + LinearLayout rootLayoutSpy = Mockito.spy(rootLayout); + + LinearLayout checkboxOptionLayout = Mockito.mock(LinearLayout.class); + CheckBox currentCheckbox = Mockito.mock(CheckBox.class); + + Mockito.doReturn(2).when(rootLayoutSpy).getChildCount(); + Mockito.doReturn(checkboxOptionLayout).when(rootLayoutSpy).getChildAt(Mockito.anyInt()); + Mockito.doReturn(currentCheckbox).when(checkboxOptionLayout).getChildAt(0); + Mockito.doReturn(true).when(currentCheckbox).isChecked(); + + Boolean checked = Whitebox.invokeMethod(factorySpy, "performValidation", rootLayoutSpy); + + Assert.assertTrue(checked); + } } 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 new file mode 100644 index 000000000..852a286e0 --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java @@ -0,0 +1,390 @@ +package com.vijay.jsonwizard.widgets; + +import android.content.res.Resources; +import android.view.View; +import android.view.ViewTreeObserver; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.rey.material.widget.Button; +import com.vijay.jsonwizard.R; +import com.vijay.jsonwizard.activities.JsonFormActivity; +import com.vijay.jsonwizard.fragments.JsonFormFragment; +import com.vijay.jsonwizard.interfaces.CommonListener; +import com.vijay.jsonwizard.utils.FormUtils; + +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import java.util.List; + +import timber.log.Timber; + + +public class OptiBpWidgetFactoryTest extends FactoryTest { + private OptiBPWidgetFactory factory; + + @Mock + private JsonFormActivity context; + + @Mock + private JsonFormFragment formFragment; + + @Mock + private Resources resources; + + @Mock + private CommonListener listener; + + @Mock + private LinearLayout rootLayout; + + @Mock + private TextView label; + + @Mock + private Button launchButton; + + private final String optiBPWidgetString = "{\n" + + " \"key\": \"optipb_widget1\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"\",\n" + + " \"type\": \"optibp\",\n" + + " \"label\": \"Measure the blood pressure using OptiBP\",\n" + + " \"optibp_button_bg_color\": \"#d32f2f\",\n" + + " \"optibp_button_text_color\": \"#FFFFFF\",\n" + + " \"read_only\": false,\n" + + " \"optibp_data\": {\n" + + " \"clientId\": \"sampleClientId\",\n" + + " \"clientOpenSRPId\": \"sampleClientOpenSRPId\"\n" + + " }," + + " \"fields_to_use_value\": [\n" + + " \"bp_systolic\",\n" + + " \"bp_diastolic\"\n" + + " ]" + + " }"; + + private final String formString = "{\n" + + " \"show_errors_on_submit\": true,\n" + + " \"encounter_type\": \"OptiBP Demo\",\n" + + " \"count\": \"1\",\n" + + " \"entity_id\": \"\",\n" + + " \"relational_id\": \"\",\n" + + " \"metadata\": {\n" + + " \"start\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"concept\",\n" + + " \"openmrs_data_type\": \"start\",\n" + + " \"openmrs_entity_id\": \"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + + " },\n" + + " \"end\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"concept\",\n" + + " \"openmrs_data_type\": \"end\",\n" + + " \"openmrs_entity_id\": \"163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + + " },\n" + + " \"today\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"encounter\",\n" + + " \"openmrs_entity_id\": \"encounter_date\"\n" + + " },\n" + + " \"deviceid\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"concept\",\n" + + " \"openmrs_data_type\": \"deviceid\",\n" + + " \"openmrs_entity_id\": \"163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + + " },\n" + + " \"subscriberid\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"concept\",\n" + + " \"openmrs_data_type\": \"subscriberid\",\n" + + " \"openmrs_entity_id\": \"163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + + " },\n" + + " \"simserial\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"concept\",\n" + + " \"openmrs_data_type\": \"simserial\",\n" + + " \"openmrs_entity_id\": \"163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + + " },\n" + + " \"phonenumber\": {\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"concept\",\n" + + " \"openmrs_data_type\": \"phonenumber\",\n" + + " \"openmrs_entity_id\": \"163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"\n" + + " },\n" + + " \"encounter_location\": \"\",\n" + + " \"look_up\": {\n" + + " \"entity_id\": \"\",\n" + + " \"value\": \"\"\n" + + " }\n" + + " },\n" + + " \"step1\": {\n" + + " \"title\": \"OptiBp Widget Demo\",\n" + + " \"fields\": [\n" + + " {\n" + + " \"key\": \"enabled_label\",\n" + + " \"type\": \"label\",\n" + + " \"text\": \"OptiBP Scan\",\n" + + " \"hint_on_text\": false,\n" + + " \"text_color\": \"#FFC100\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"\",\n" + + " \"label_info_text\": \"Checking out the functionality for OptiBP widget\"\n" + + " },\n" + + " {\n" + + " \"key\": \"bp_systolic_label\",\n" + + " \"type\": \"label\",\n" + + " \"label_text_style\": \"bold\",\n" + + " \"text\": \"Systolic blood pressure (SBP) (mmHg)\",\n" + + " \"text_color\": \"#000000\",\n" + + " \"v_required\": {\n" + + " \"value\": true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\": \"bp_systolic\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"5090\",\n" + + " \"type\": \"normal_edit_text\",\n" + + " \"edit_text_style\": \"bordered\",\n" + + " \"edit_type\": \"number\",\n" + + " \"v_required\": {\n" + + " \"value\": \"true\",\n" + + " \"err\": \"Please enter BP systolic value\"\n" + + " },\n" + + " \"v_numeric\": {\n" + + " \"value\": \"true\",\n" + + " \"err\": \"\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\": \"spacer\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"spacer\",\n" + + " \"type\": \"spacer\",\n" + + " \"spacer_height\": \"10sp\"\n" + + " },\n" + + " {\n" + + " \"key\": \"bp_diastolic_label\",\n" + + " \"type\": \"label\",\n" + + " \"label_text_style\": \"bold\",\n" + + " \"text\": \"Diastolic blood pressure (DBP) (mmHg)\",\n" + + " \"text_color\": \"#000000\",\n" + + " \"v_required\": {\n" + + " \"value\": true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\": \"bp_diastolic\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"5089\",\n" + + " \"type\": \"normal_edit_text\",\n" + + " \"edit_text_style\": \"bordered\",\n" + + " \"edit_type\": \"number\",\n" + + " \"v_numeric\": {\n" + + " \"value\": \"true\",\n" + + " \"err\": \"\"\n" + + " },\n" + + " \"v_required\": {\n" + + " \"value\": \"true\",\n" + + " \"err\": \"Please enter the BP diastolic value\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\": \"spacer\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"spacer\",\n" + + " \"type\": \"spacer\",\n" + + " \"spacer_height\": \"10sp\"\n" + + " },\n" + + " {\n" + + " \"key\": \"optipb_widget1\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"\",\n" + + " \"type\": \"optibp\",\n" + + " \"label\": \"Measure the blood pressure using OptiBP\",\n" + + " \"optibp_button_bg_color\": \"#d32f2f\",\n" + + " \"optibp_button_text_color\": \"#FFFFFF\",\n" + + " \"read_only\": false,\n" + " " + + " \"fields_to_use_value\": [\n" + + " \"bp_systolic\",\n" + + " \"bp_diastolic\"\n" + + " ]," + + " \"optibp_data\": {\n" + + " \"clientId\": \"sampleClientId\",\n" + + " \"clientOpenSRPId\": \"sampleClientOpenSRPId\"\n" + + " }" + + " }\n" + + " ]\n" + + " }\n" + + "}"; + + private final String resultJson = "{\n" + + " \"resourceType\": \"DiagnosticReport\",\n" + + " \"identifier\": [\n" + + " {\n" + + " \"use\": \"official\",\n" + + " \"value\": \"uuid\" \n" + + " }\n" + + " ],\n" + + " \"subject\": \"patient-uuid\", \n" + + " \"result\": [\n" + + " {\n" + + " \"status\": \"final\",\n" + + " \"category\": [\n" + + " {\n" + + " \"coding\": [\n" + + " {\n" + + " \"system\": \"http://terminology.hl7.org/CodeSystem/observation-category\",\n" + + " \"code\": \"vital-signs\",\n" + + " \"display\": \"vital-signs\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ],\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"system\": \"http://loinc.org\",\n" + + " \"code\": \"55284-4\",\n" + + " \"display\": \"Blood Pressure\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Blood Pressure\"\n" + + " },\n" + + " \"component\": [\n" + + " {\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"system\": \"http://loinc.org\",\n" + + " \"code\": \"8462-4\",\n" + + " \"display\": \"Diastolic Blood Pressure\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Diastolic Blood Pressure\"\n" + + " },\n" + + " \"valueQuantity\": {\n" + + " \"value\": 70\n" + + " }\n" + + " },\n" + + " {\n" + + " \"code\": {\n" + + " \"coding\": [\n" + + " {\n" + + " \"system\": \"http://loinc.org\",\n" + + " \"code\": \"8480-6\",\n" + + " \"display\": \"Systolic Blood Pressure\"\n" + + " }\n" + + " ],\n" + + " \"text\": \"Systolic Blood Pressure\"\n" + + " },\n" + + " \"valueQuantity\": {\n" + + " \"value\": 110\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}"; + + @Before + public void setUp() { + super.setUp(); + try { + jsonFormActivity.setmJSONObject(new JSONObject(formString)); + } catch (JSONException e) { + Timber.e(e); + } + EditText sbp = new EditText(jsonFormActivity); + sbp.setTag(R.id.address, "step1:bp_systolic"); + jsonFormActivity.addFormDataView(sbp); + EditText dbp = new EditText(jsonFormActivity); + dbp.setTag(R.id.address, "step1:bp_diastolic"); + jsonFormActivity.addFormDataView(dbp); + factory = new OptiBPWidgetFactory(); + } + + @Test + public void testFactoryInstantiatesViewsCorrectly() throws Exception { + Assert.assertNotNull(factory); + OptiBPWidgetFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(factorySpy); + + FormUtils formUtils = new FormUtils(); + FormUtils formUtilsSpy = Mockito.spy(formUtils); + Assert.assertNotNull(formUtilsSpy); + + Mockito.doReturn(rootLayout).when(factorySpy).getRootLayout(context); + Assert.assertNotNull(rootLayout); + + Mockito.doReturn(Mockito.mock(ViewTreeObserver.class)).when(rootLayout).getViewTreeObserver(); + + Mockito.doReturn(resources).when(context).getResources(); + Assert.assertNotNull(resources); + + Mockito.doReturn(label).when(rootLayout).findViewById(R.id.optibp_label); + Assert.assertNotNull(label); + + Mockito.doReturn(launchButton).when(rootLayout).findViewById(R.id.optibp_launch_button); + Assert.assertNotNull(launchButton); + + //noinspection ResultOfMethodCallIgnored + Mockito.doReturn(jsonFormActivity).when(formFragment).getJsonApi(); + + List viewList = factorySpy.getViewsFromJson("step1", context, formFragment, new JSONObject(optiBPWidgetString), listener); + Assert.assertNotNull(viewList); + Assert.assertTrue(viewList.size() > 0); + } + + @Test + public void testInputJson() throws JSONException { + Assert.assertNotNull(factory); + OptiBPWidgetFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(factorySpy); + + String inputJson = factorySpy.getInputJsonString(jsonFormActivity, new JSONObject(optiBPWidgetString)); + + Assert.assertEquals(inputJson, "{\"clientId\":\"sampleClientId\",\"clientOpenSRPId\":\"sampleClientOpenSRPId\"}"); + } + + @Test + public void testResultJson() throws JSONException { + Assert.assertNotNull(factory); + OptiBPWidgetFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(factorySpy); + + String systolic = factorySpy.getBPValue(resultJson, OptiBPWidgetFactory.BPFieldType.SYSTOLIC_BP); + String diastolic = factorySpy.getBPValue(resultJson, OptiBPWidgetFactory.BPFieldType.DIASTOLIC_BP); + + Assert.assertEquals(systolic, "110"); + Assert.assertEquals(diastolic, "70"); + } + + @Test + public void testPopulateETValues() throws JSONException { + Assert.assertNotNull(factory); + OptiBPWidgetFactory factorySpy = Mockito.spy(factory); + Assert.assertNotNull(factorySpy); + + EditText sbp = Mockito.mock(EditText.class); + EditText dbp = Mockito.mock(EditText.class); + factorySpy.populateBPEditTextValues(resultJson, sbp, dbp); + + Mockito.verify(sbp).setEnabled(false); + Mockito.verify(dbp).setEnabled(false); + } +} diff --git a/build.gradle b/build.gradle index 4ca8da437..ac7592ee7 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,6 @@ buildscript { repositories { google() mavenCentral() - jcenter() maven{ url 'https://plugins.gradle.org/m2/' } } @@ -19,11 +18,11 @@ configure(allprojects) { project -> group = GROUP repositories { - jcenter() mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } maven { url 'https://plugins.gradle.org/m2/'} mavenLocal() + jcenter() } } } @@ -42,7 +41,6 @@ subprojects { ext.androidAnnotationsAPIVersion = '3.0.1' repositories { - jcenter() mavenCentral() maven { url 'https://jitpack.io' } maven { url "http://acra.googlecode.com/svn/repository/releases" } @@ -50,6 +48,7 @@ subprojects { maven { url "http://cloudant.github.io/cloudant-sync-eap/repository" } maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } mavenLocal() + jcenter() } // Improve build server performance by allowing disabling of pre-dexing diff --git a/form_tester/build.gradle b/form_tester/build.gradle index 2674898ff..04b343f76 100644 --- a/form_tester/build.gradle +++ b/form_tester/build.gradle @@ -1,7 +1,7 @@ buildscript { repositories { - jcenter() google() + jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.3' diff --git a/gradle.properties b/gradle.properties index 3a1ce066c..0de4d45a2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=2.0.11-SNAPSHOT +VERSION_NAME=2.1.0-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard diff --git a/sample/build.gradle b/sample/build.gradle index f8beb5f8d..5df5cf5ac 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,7 +1,7 @@ buildscript { repositories { - jcenter() google() + jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.3' diff --git a/sample/src/main/assets/json.form/optibp_demo_form.json b/sample/src/main/assets/json.form/optibp_demo_form.json new file mode 100644 index 000000000..d44827d13 --- /dev/null +++ b/sample/src/main/assets/json.form/optibp_demo_form.json @@ -0,0 +1,160 @@ +{ + "show_errors_on_submit": true, + "encounter_type": "OptiBP Demo", + "count": "1", + "entity_id": "", + "relational_id": "", + "metadata": { + "start": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "start", + "openmrs_entity_id": "163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "end": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "end", + "openmrs_entity_id": "163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "today": { + "openmrs_entity_parent": "", + "openmrs_entity": "encounter", + "openmrs_entity_id": "encounter_date" + }, + "deviceid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "deviceid", + "openmrs_entity_id": "163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "subscriberid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "subscriberid", + "openmrs_entity_id": "163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "simserial": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "simserial", + "openmrs_entity_id": "163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "phonenumber": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "phonenumber", + "openmrs_entity_id": "163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "encounter_location": "", + "look_up": { + "entity_id": "", + "value": "" + } + }, + "step1": { + "title": "OptiBp Widget Demo", + "fields": [ + { + "key": "enabled_label", + "type": "label", + "text": "OptiBP Scan", + "hint_on_text": false, + "text_color": "#FFC100", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "label_info_text": "Checking out the functionality for OptiBP widget" + }, + { + "key": "bp_systolic_label", + "type": "label", + "label_text_style": "bold", + "text": "Systolic blood pressure (SBP) (mmHg)", + "text_color": "#000000", + "v_required": { + "value": true + } + }, + { + "key": "bp_systolic", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "5090", + "type": "normal_edit_text", + "edit_text_style": "bordered", + "edit_type": "number", + "v_required": { + "value": "true", + "err": "Please enter BP systolic value" + }, + "v_numeric": { + "value": "true", + "err": "" + } + }, + { + "key": "spacer", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "spacer", + "type": "spacer", + "spacer_height": "10sp" + }, + { + "key": "bp_diastolic_label", + "type": "label", + "label_text_style": "bold", + "text": "Diastolic blood pressure (DBP) (mmHg)", + "text_color": "#000000", + "v_required": { + "value": true + } + }, + { + "key": "bp_diastolic", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "5089", + "type": "normal_edit_text", + "edit_text_style": "bordered", + "edit_type": "number", + "v_numeric": { + "value": "true", + "err": "" + }, + "v_required": { + "value": "true", + "err": "Please enter the BP diastolic value" + } + }, + { + "key": "spacer", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "spacer", + "type": "spacer", + "spacer_height": "10sp" + }, + { + "key": "optipb_widget1", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "optibp", + "label": "Measure the blood pressure using OptiBP", + "optibp_button_bg_color": "#d32f2f", + "optibp_button_text_color": "#FFFFFF", + "read_only": false, + "fields_to_use_value": [ + "bp_systolic", + "bp_diastolic" + ], + "optibp_data": { + "clientId": "sampleClientId", + "clientOpenSRPId": "sampleClientOpenSRPId" + } + } + ] + } +} \ No newline at end of file diff --git a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java index 2dc918cd3..e66ce82ff 100644 --- a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java +++ b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java @@ -14,6 +14,7 @@ import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.factory.FileSourceFactoryHelper; +import com.vijay.jsonwizard.utils.FormUtils; import org.json.JSONArray; import org.json.JSONObject; @@ -39,6 +40,7 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.expansion_panel_button).setOnClickListener(this); findViewById(R.id.repeating_group_button).setOnClickListener(this); findViewById(R.id.multiselect_list).setOnClickListener(this); + findViewById(R.id.optibp_widget).setOnClickListener(this); } @Override @@ -181,6 +183,29 @@ public void startForm(int jsonFormActivityRequestCode, String formName, String e startActivityForResult(intent, jsonFormActivityRequestCode); break; } + case "optibp_demo_form": { + JSONObject stepOne = jsonForm.getJSONObject(STEP1); + JSONArray jsonArray = stepOne.getJSONArray(FIELDS); + 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); + } + JSONObject optiBPData = FormUtils.createOptiBPDataObject("46ccd2e0-bbec-4e4a-8f73-972a2f1f95ea", + "1272326657"); + jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA, optiBPData); + break; + } + } + + Intent intent = new Intent(this, JsonFormActivity.class); + intent.putExtra("json", jsonForm.toString()); + intent.putExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, translate); + Log.d(getClass().getName(), "form is " + jsonForm.toString()); + startActivityForResult(intent, jsonFormActivityRequestCode); + break; + } default: { @@ -250,6 +275,9 @@ public void onClick(View view) { case R.id.multiselect_list: startForm(REQUEST_CODE_GET_JSON, "multi_select_list_form", null, false); break; + case R.id.optibp_widget: + startForm(REQUEST_CODE_GET_JSON, "optibp_demo_form", null, false); + break; default: break; } diff --git a/sample/src/main/res/layout/content_main.xml b/sample/src/main/res/layout/content_main.xml index f972fa702..00f182300 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -111,5 +111,14 @@ android:textColor="@android:color/white" android:theme="@style/NativeFormsAppTheme" /> +