Skip to content

Commit

Permalink
Merge pull request #210 from OpenSRP/209-text-with-image-widget
Browse files Browse the repository at this point in the history
209 text with image widget
  • Loading branch information
allan-on authored Jul 9, 2019
2 parents b26ef24 + 783d527 commit e463a1b
Show file tree
Hide file tree
Showing 8 changed files with 1,114 additions and 925 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class JsonFormConstants {
public static final String LABEL_INFO_TITLE = "label_info_title";
public static final String LABEL_NUMBER = "label_number";
public static final String CHOOSE_IMAGE = "choose_image";
public static final String IMAGE_FILE = "image_file";
public static final String IMAGE_FOLDER = "image_folder";
public static final String IMAGE_VIEW = "image_view";
public static final String OPTIONS_FIELD_NAME = "options";
public static final String SPINNER = "spinner";
public static final String DATE_PICKER = "date_picker";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.vijay.jsonwizard.widgets.HiddenTextFactory;
import com.vijay.jsonwizard.widgets.HorizontalLineFactory;
import com.vijay.jsonwizard.widgets.ImagePickerFactory;
import com.vijay.jsonwizard.widgets.ImageViewFactory;
import com.vijay.jsonwizard.widgets.LabelFactory;
import com.vijay.jsonwizard.widgets.NativeEditTextFactory;
import com.vijay.jsonwizard.widgets.NativeRadioButtonFactory;
Expand Down Expand Up @@ -80,6 +81,7 @@ protected void registerWidgets() {
map.put(JsonFormConstants.TIME_PICKER, new TimePickerFactory());
map.put(JsonFormConstants.REPEATING_GROUP, new RepeatingGroupFactory());
map.put(JsonFormConstants.RDT_CAPTURE, new RDTCaptureFactory());
map.put(JsonFormConstants.IMAGE_VIEW, new ImageViewFactory());
}

public List<View> fetchFormElements(String stepName, JsonFormFragment formFragment,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.vijay.jsonwizard.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.rey.material.util.ViewUtil;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.interfaces.FormWidgetFactory;
import com.vijay.jsonwizard.utils.FormUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ImageViewFactory implements FormWidgetFactory {

private View rootLayout;
private TextView descriptionTextView;

@Override
public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener, boolean popup) throws Exception {
List<View> views = new ArrayList<>(1);
rootLayout = LayoutInflater.from(context).inflate(getLayout(), null);
descriptionTextView = rootLayout.findViewById(R.id.imageViewLabel);
setWidgetTags(jsonObject, stepName);
setViewConfigs(jsonObject, context);
views.add(rootLayout);
return views;
}

@Override
public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception {
return getViewsFromJson(stepName, context, formFragment, jsonObject, listener, false);
}

private int getLayout() {
return R.layout.native_form_image_view;
}

private void setWidgetTags(JSONObject jsonObject, String stepName) {
setBasicTags(jsonObject, rootLayout);
setBasicTags(jsonObject, descriptionTextView);

JSONArray canvasIds = new JSONArray();
rootLayout.setId(ViewUtil.generateViewId());
canvasIds.put(rootLayout.getId());
rootLayout.setTag(R.id.canvas_ids, canvasIds);
rootLayout.setTag(R.id.type, jsonObject.optString(JsonFormConstants.TYPE));
rootLayout.setTag(R.id.address, stepName + ":" + jsonObject.optString(JsonFormConstants.KEY));
rootLayout.setTag(R.id.extraPopup, false);
}

private void setBasicTags(JSONObject jsonObject, View view) {
String key = jsonObject.optString(JsonFormConstants.KEY, "");
String openMrsEntityParent = jsonObject.optString(JsonFormConstants.OPENMRS_ENTITY_PARENT, "");
String openMrsEntity = jsonObject.optString(JsonFormConstants.OPENMRS_ENTITY, "");
String openMrsEntityId = jsonObject.optString(JsonFormConstants.OPENMRS_ENTITY_ID, "");

view.setTag(R.id.key, key);
view.setTag(R.id.openmrs_entity_parent, openMrsEntityParent);
view.setTag(R.id.openmrs_entity, openMrsEntity);
view.setTag(R.id.openmrs_entity_id, openMrsEntityId);
}

private void setViewConfigs(JSONObject jsonObject, Context context) {
String descriptionText = jsonObject.optString(JsonFormConstants.TEXT, "");
String imageFile = jsonObject.optString(JsonFormConstants.IMAGE_FILE, "");

if (!TextUtils.isEmpty(descriptionText)) {
descriptionTextView.setText(descriptionText);
String textColor = jsonObject.optString(JsonFormConstants.TEXT_COLOR, "#000000");
descriptionTextView.setTextColor(Color.parseColor(textColor));
String textSize = jsonObject.optString(JsonFormConstants.TEXT_SIZE, String.valueOf(context.getResources().getDimension(R.dimen.label_text_size)));
descriptionTextView.setTextSize(FormUtils.getValueFromSpOrDpOrPx(textSize, context));
}

if (!TextUtils.isEmpty(imageFile)) {
String folderName = jsonObject.optString(JsonFormConstants.IMAGE_FOLDER, "");
Bitmap bitmap = FormUtils.getBitmap(context, folderName, imageFile);
if (bitmap != null) {
ImageView imageView = rootLayout.findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/native_form_text_and_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:paddingBottom="@dimen/label_bottom_padding">

<TextView
android:id="@+id/imageViewLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:paddingBottom="20dp"
android:text="Sample text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageViewLabel"
app:layout_constraintVertical_bias="0.394" />

</android.support.constraint.ConstraintLayout>
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.6.22-SNAPSHOT
VERSION_NAME=1.6.23-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard
Expand Down
Binary file added sample/src/main/assets/image/process.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion sample/src/main/assets/json.form/wizard_form.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"count": "3",
"count": "4",
"encounter_type": "Test",
"entity_id": "",
"relational_id": "",
Expand Down Expand Up @@ -241,6 +241,7 @@
},
"step3": {
"title": "Maternal Exam",
"next": "step4",
"fields": [
{
"key": "spacer",
Expand Down Expand Up @@ -535,5 +536,21 @@
}
}
]
},
"step4": {
"title": "How to instructions",
"fields": [
{
"key": "illustration_text_description",
"openmrs_entity_parent": "",
"openmrs_entity": "",
"openmrs_entity_id": "",
"type": "image_view",
"text": "Take the test by doing it as shown in the image below",
"label_text_size": "18sp",
"image_file": "process.jpeg",
"text_color": "#000000"
}
]
}
}

0 comments on commit e463a1b

Please sign in to comment.