Skip to content

Commit

Permalink
Merge pull request #581 from opensrp/feature/optibp-widget
Browse files Browse the repository at this point in the history
OptiBP Widget
  • Loading branch information
hamza-vd authored May 31, 2021
2 parents 1231902 + a991728 commit c9c6c7e
Show file tree
Hide file tree
Showing 20 changed files with 1,089 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, View> createRadioButtonAndCheckBoxLabel(String stepName, LinearLayout linearLayout,
JSONObject jsonObject, Context context,
JSONArray canvasIds, final Boolean readOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -84,8 +87,8 @@ public List<View> getViewsFromJson(String stepName, Context context, JsonFormFra
return attachJson(stepName, context, jsonObject, listener, formFragment, false);
}

private List<View> attachJson(String stepName, Context context, JSONObject jsonObject, CommonListener listener, JsonFormFragment formFragment,
boolean popup) throws JSONException {
private List<View> 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);
Expand Down Expand Up @@ -119,7 +122,6 @@ private List<View> attachJson(String stepName, Context context, JSONObject jsonO

Map<String, View> labelViews = formUtils.createRadioButtonAndCheckBoxLabel(stepName, rootLayout, jsonObject, context, canvasIds, readOnly, listener, popup);


ArrayList<View> editableCheckBoxes = addCheckBoxOptionsElements(jsonObject, context, readOnly, canvasIds, stepName,
rootLayout, listener, popup);

Expand Down Expand Up @@ -177,7 +179,7 @@ private ArrayList<View> addCheckBoxOptionsElements(JSONObject jsonObject, Contex
ArrayList<CheckBox> checkBoxes = new ArrayList<>();
ArrayList<View> 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);
Expand Down Expand Up @@ -205,14 +207,23 @@ private ArrayList<View> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Loading

0 comments on commit c9c6c7e

Please sign in to comment.