diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/adapter/DynamicLabelAdapter.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/adapter/DynamicLabelAdapter.java new file mode 100644 index 000000000..ccc4b61eb --- /dev/null +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/adapter/DynamicLabelAdapter.java @@ -0,0 +1,80 @@ +package com.vijay.jsonwizard.adapter; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.vijay.jsonwizard.R; +import com.vijay.jsonwizard.model.DynamicLabelInfo; +import com.vijay.jsonwizard.utils.FormUtils; + +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; +import java.util.ArrayList; + +public class DynamicLabelAdapter extends RecyclerView.Adapter { + private final Context context; + private final ArrayList dynamicLabelInfoList; + + public DynamicLabelAdapter(Context context, ArrayList dynamicLabelInfoList) { + this.context = context; + this.dynamicLabelInfoList = dynamicLabelInfoList; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dynamic_dialog_row_layout, parent, false); + return new RecyclerViewHolder(itemView); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder; + String dynamicLabelTitle = dynamicLabelInfoList.get(position).getDynamicLabelTitle(); + if (StringUtils.isNotBlank(dynamicLabelTitle)) { + recyclerViewHolder.tileTextView.setText(dynamicLabelTitle); + recyclerViewHolder.tileTextView.setVisibility(View.VISIBLE); + } + + String dynamicLabelText = dynamicLabelInfoList.get(position).getDynamicLabelText(); + if (StringUtils.isNotBlank(dynamicLabelText)) { + recyclerViewHolder.descriptionTextView.setText(dynamicLabelText); + recyclerViewHolder.descriptionTextView.setVisibility(View.VISIBLE); + } + + String dynamicLabelImageSrc = dynamicLabelInfoList.get(position).getDynamicLabelImageSrc(); + if (StringUtils.isNotBlank(dynamicLabelImageSrc)) { + try { + recyclerViewHolder.imageViewLabel.setImageDrawable(FormUtils.readImageFromAsset(context, dynamicLabelImageSrc)); + recyclerViewHolder.imageViewLabel.setVisibility(View.VISIBLE); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public int getItemCount() { + return dynamicLabelInfoList.size(); + } + + public static class RecyclerViewHolder extends RecyclerView.ViewHolder { + private final TextView descriptionTextView; + private final TextView tileTextView; + private final ImageView imageViewLabel; + + private RecyclerViewHolder(View view) { + super(view); + descriptionTextView = view.findViewById(R.id.descriptionText); + tileTextView = view.findViewById(R.id.labelTitle); + imageViewLabel = view.findViewById(R.id.imageViewLabel); + } + } +} 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 bc4738684..622335a2c 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 @@ -24,7 +24,10 @@ public class JsonFormConstants { public static final String LABEL_TEXT_SIZE = "label_text_size"; public static final String LABEL_TEXT_COLOR = "label_text_color"; public static final String LABEL_INFO_TEXT = "label_info_text"; + public static final String DYNAMIC_LABEL_INFO = "dynamic_label_info"; + public static final String DYNAMIC_LABEL_TITLE = "dynamic_label_title"; public static final String LABEL_INFO_TITLE = "label_info_title"; + public static final String DYNAMIC_LABEL_TEXT = "dynamic_label_text"; public static final String LABEL_NUMBER = "label_number"; public static final String CHOOSE_IMAGE = "choose_image"; public static final String FINGER_PRINT = "finger_print"; @@ -198,6 +201,8 @@ public class JsonFormConstants { public static final String INVISIBLE_REQUIRED_FIELDS = "invisible_required_fields"; public static final String LABEL_INFO_HAS_IMAGE = "label_info_has_image"; public static final String LABEL_INFO_IMAGE_SRC = "label_info_image_src"; + public static final String DYNAMIC_LABEL_IMAGE_SRC = "dynamic_label_image_src"; + public static final String LABEL_IS_DYNAMIC = "label_is_dynamic"; public static final String ACCORDION_INFO_TEXT = "accordion_info_text"; public static final String ACCORDION_INFO_TITLE = "accordion_info_title"; public static final String DISPLAY_BOTTOM_SECTION = "display_bottom_section"; diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/model/DynamicLabelInfo.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/model/DynamicLabelInfo.java new file mode 100644 index 000000000..9fa38ca5f --- /dev/null +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/model/DynamicLabelInfo.java @@ -0,0 +1,28 @@ +package com.vijay.jsonwizard.model; + +/** + * Created by Qazi Abubakar + */ +public class DynamicLabelInfo { + private final String dynamicLabelTitle; + private final String dynamicLabelText; + private final String dynamicLabelImageSrc; + + public DynamicLabelInfo(String dynamicLabelTitle, String dynamicLabelText, String dynamicLabelImageSrc) { + this.dynamicLabelTitle = dynamicLabelTitle; + this.dynamicLabelText = dynamicLabelText; + this.dynamicLabelImageSrc = dynamicLabelImageSrc; + } + + public String getDynamicLabelTitle() { + return dynamicLabelTitle; + } + + public String getDynamicLabelText() { + return dynamicLabelText; + } + + public String getDynamicLabelImageSrc() { + return dynamicLabelImageSrc; + } +} diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java index 442bad18d..33de64598 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenter.java @@ -20,6 +20,8 @@ import android.support.v4.content.FileProvider; import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatRadioButton; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.util.Log; import android.view.MenuItem; @@ -40,6 +42,7 @@ import com.rey.material.widget.Button; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; +import com.vijay.jsonwizard.adapter.DynamicLabelAdapter; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.customviews.MaterialSpinner; import com.vijay.jsonwizard.customviews.NativeEditText; @@ -47,6 +50,7 @@ import com.vijay.jsonwizard.fragments.JsonFormErrorFragment; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interactors.JsonFormInteractor; +import com.vijay.jsonwizard.model.DynamicLabelInfo; import com.vijay.jsonwizard.mvp.MvpBasePresenter; import com.vijay.jsonwizard.rules.RuleConstant; import com.vijay.jsonwizard.task.ExpansionPanelGenericPopupDialogTask; @@ -88,6 +92,7 @@ import timber.log.Timber; import static com.vijay.jsonwizard.utils.FormUtils.dpToPixels; +import static com.vijay.jsonwizard.utils.FormUtils.getDynamicLabelInfoList; /** * Created by vijay on 5/14/15. @@ -840,6 +845,8 @@ private void setViewGroupEditable(View childElement) { protected void showInformationDialog(View view) { if (view.getTag(R.id.label_dialog_image_src) != null) { showCustomDialog(view); + } else if (view.getTag(R.id.dynamic_label_info) != null) { + showDynamicDialog(view); } else { showAlertDialog(view); } @@ -920,6 +927,46 @@ public void onClick(View v) { dialog.show(); } + private void showDynamicDialog(@NonNull View view) { + final Dialog dialog = getCustomDialog(view); + dialog.setContentView(R.layout.native_form_dynamic_dialog); + if (dialog.getWindow() != null) { + WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); + params.width = (int) (view.getContext().getResources().getDisplayMetrics().widthPixels + * 0.90); + params.height = ViewGroup.LayoutParams.WRAP_CONTENT; + } + + String titleString = (String) view.getTag(R.id.label_dialog_title); + if (StringUtils.isNotBlank(titleString)) { + TextView dialogTitle = dialog.findViewById(R.id.dialogTitle); + dialogTitle.setText(titleString); + dialogTitle.setVisibility(View.VISIBLE); + } + + RecyclerView dialogRecyclerView = dialog.findViewById(R.id.dialogRecyclerView); + JSONArray dynamicLabelInfoArray = (JSONArray) view.getTag(R.id.dynamic_label_info); + ArrayList dynamicLabelInfoList = getDynamicLabelInfoList(dynamicLabelInfoArray); + + if (dynamicLabelInfoList.size() > 0) { + DynamicLabelAdapter dynamicLabelAdapter = new DynamicLabelAdapter(view.getContext(), dynamicLabelInfoList); + dialogRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); + dialogRecyclerView.setAdapter(dynamicLabelAdapter); + dialogRecyclerView.setVisibility(View.VISIBLE); + } + + AppCompatButton dialogButton = dialog.findViewById(R.id.dialogButton); + dialogButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dialog.dismiss(); + } + }); + dialogButton.setVisibility(View.VISIBLE); + + dialog.show(); + } + //household_photo,JsonFormConstants.CHOOSE_IMAGE public void onClickCameraIcon(String key, String type) { 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 3daaea0d3..0a82c738e 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 @@ -44,6 +44,7 @@ import com.vijay.jsonwizard.interfaces.JsonApi; import com.vijay.jsonwizard.interfaces.OnFormFetchedCallback; import com.vijay.jsonwizard.interfaces.RollbackDialogCallback; +import com.vijay.jsonwizard.model.DynamicLabelInfo; import com.vijay.jsonwizard.rules.RuleConstant; import com.vijay.jsonwizard.views.CustomTextView; @@ -64,6 +65,7 @@ import java.io.StringReader; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.HashSet; @@ -477,6 +479,12 @@ public void showInfoIcon(String stepName, JSONObject jsonObject, CommonListener } + if (imageAttributes.get(JsonFormConstants.LABEL_IS_DYNAMIC) != null) { + imageView.setTag(R.id.dynamic_label_info, jsonObject.getJSONArray(JsonFormConstants.DYNAMIC_LABEL_INFO)); + imageView.setTag(R.id.label_dialog_title, imageAttributes.get(JsonFormConstants.LABEL_INFO_TITLE)); + imageView.setVisibility(View.VISIBLE); + } + imageView.setTag(R.id.key, jsonObject.getString(JsonFormConstants.KEY)); imageView.setTag(R.id.type, jsonObject.getString(JsonFormConstants.TYPE)); imageView.setTag(R.id.address, stepName + ":" + jsonObject.getString(JsonFormConstants.KEY)); @@ -495,6 +503,8 @@ public static HashMap getInfoDialogAttributes(JSONObject jsonObj jsonObject.optString(JsonFormConstants.LABEL_INFO_HAS_IMAGE, null)); imageAttributes.put(JsonFormConstants.LABEL_INFO_IMAGE_SRC, jsonObject.optString(JsonFormConstants.LABEL_INFO_IMAGE_SRC, null)); + imageAttributes.put(JsonFormConstants.LABEL_IS_DYNAMIC, + jsonObject.optString(JsonFormConstants.LABEL_IS_DYNAMIC, null)); return imageAttributes; } @@ -502,6 +512,20 @@ public static Drawable readImageFromAsset(Context context, String fileName) thro return Drawable.createFromStream(context.getAssets().open(fileName), null); } + public static ArrayList getDynamicLabelInfoList(JSONArray jsonArray) { + ArrayList dynamicLabelInfos = new ArrayList<>(); + for (int i = 0; i < jsonArray.length(); i++) { + try { + JSONObject dynamicLabelJsonObject = jsonArray.getJSONObject(i); + dynamicLabelInfos.add(new DynamicLabelInfo(dynamicLabelJsonObject.getString(JsonFormConstants.DYNAMIC_LABEL_TITLE), + dynamicLabelJsonObject.getString(JsonFormConstants.DYNAMIC_LABEL_TEXT), dynamicLabelJsonObject.getString(JsonFormConstants.DYNAMIC_LABEL_IMAGE_SRC))); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return dynamicLabelInfos; + } + public static void setEditButtonAttributes(JSONObject jsonObject, View editableView, ImageView editButton, CommonListener listener) throws JSONException { diff --git a/android-json-form-wizard/src/main/res/layout/dynamic_dialog_row_layout.xml b/android-json-form-wizard/src/main/res/layout/dynamic_dialog_row_layout.xml new file mode 100644 index 000000000..a3831f1fa --- /dev/null +++ b/android-json-form-wizard/src/main/res/layout/dynamic_dialog_row_layout.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android-json-form-wizard/src/main/res/layout/native_form_dynamic_dialog.xml b/android-json-form-wizard/src/main/res/layout/native_form_dynamic_dialog.xml new file mode 100644 index 000000000..aa0aa111a --- /dev/null +++ b/android-json-form-wizard/src/main/res/layout/native_form_dynamic_dialog.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android-json-form-wizard/src/main/res/values/ids.xml b/android-json-form-wizard/src/main/res/values/ids.xml index 0aa21c290..54d496574 100644 --- a/android-json-form-wizard/src/main/res/values/ids.xml +++ b/android-json-form-wizard/src/main/res/values/ids.xml @@ -33,6 +33,7 @@ + diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java index dfef5af45..f068248bb 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonFormFragmentPresenterRoboElectricTest.java @@ -569,6 +569,33 @@ public void testShowInformationDialogShouldShowCustomDialog() { verify(dialogSpy, times(1)).dismiss(); } + @Test + public void testShowInformationDialogShouldShowDynamicDialog() { + View view = new View(RuntimeEnvironment.application); + try { + view.setTag(R.id.dynamic_label_info, new JSONArray("[{\"dynamic_label_title\": \"1\",\"dynamic_label_text\": \"1- A maximum of up to 3 weekly doses may be required.\",\"dynamic_label_image_src\":\"img/first_img.png\"}]")); + } catch (JSONException e) { + e.printStackTrace(); + } + view.setTag(R.id.label_dialog_title, "title"); + view.setTag(R.id.label_dialog_info, "info"); + + JsonFormFragmentPresenter spyPresenter = spy(presenter); + Dialog dialogSpy = spy(new Dialog(view.getContext())); + doReturn(dialogSpy).when(spyPresenter).getCustomDialog(view); + spyPresenter.showInformationDialog(view); + + verify(dialogSpy, times(1)).show(); + + assertTrue(dialogSpy.findViewById(R.id.dialogRecyclerView).isShown()); + + assertTrue(dialogSpy.findViewById(R.id.dialogTitle).isShown()); + + dialogSpy.findViewById(R.id.dialogButton).performClick(); + + verify(dialogSpy, times(1)).dismiss(); + } + @Test public void testShowInformationDialogShouldShowAlertDialog() { View view = new View(RuntimeEnvironment.application); 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 f4b908a68..d64cd8090 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 @@ -9,6 +9,7 @@ import com.vijay.jsonwizard.domain.ExpansionPanelItemModel; import com.vijay.jsonwizard.domain.ExpansionPanelValuesModel; import com.vijay.jsonwizard.interfaces.OnFormFetchedCallback; +import com.vijay.jsonwizard.model.DynamicLabelInfo; import org.apache.commons.lang3.StringUtils; import org.jeasy.rules.api.Facts; @@ -25,6 +26,7 @@ import org.smartregister.client.utils.contract.ClientFormContract; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.Locale; @@ -657,4 +659,15 @@ public void testGetCheckBoxResultsWithNoIsRuleCheck() throws JSONException { Assert.assertNotNull(facts); Assert.assertEquals(4, facts.asMap().size()); } + + @Test + public void testGetDynamicLabelInfoList() throws JSONException { + JSONArray jsonArray = new JSONArray("[{\"dynamic_label_title\": \"sample title\",\"dynamic_label_text\": \"sample text\",\"dynamic_label_image_src\": \"img/img.png\"}]"); + ArrayList expectedList = new ArrayList<>(); + expectedList.add(new DynamicLabelInfo("sample title", "sample text", "img/img.png")); + ArrayList actualList = FormUtils.getDynamicLabelInfoList(jsonArray); + Assert.assertEquals(expectedList.get(0).getDynamicLabelText(), actualList.get(0).getDynamicLabelText()); + Assert.assertEquals(expectedList.get(0).getDynamicLabelTitle(), actualList.get(0).getDynamicLabelTitle()); + Assert.assertEquals(expectedList.get(0).getDynamicLabelImageSrc(), actualList.get(0).getDynamicLabelImageSrc()); + } } diff --git a/gradle.properties b/gradle.properties index 3a4676169..ca772a411 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=2.0.2-SNAPSHOT +VERSION_NAME=2.0.3-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard diff --git a/sample/src/main/assets/img/first_img.png b/sample/src/main/assets/img/first_img.png new file mode 100644 index 000000000..b75be2ff5 Binary files /dev/null and b/sample/src/main/assets/img/first_img.png differ diff --git a/sample/src/main/assets/json.form/single_form.json b/sample/src/main/assets/json.form/single_form.json index a8f14a811..e110dd11e 100644 --- a/sample/src/main/assets/json.form/single_form.json +++ b/sample/src/main/assets/json.form/single_form.json @@ -100,10 +100,30 @@ "label": "Does the woman have any of the danger signs in the image?", "label_text_style": "bold", "text_color": "#000000", - "label_info_text": "A maximum of up to 3 weekly doses may be required.", - "label_info_title": "Syphilis Compliance", - "label_info_image_src": "img/chw.png", - "label_info_has_image": true, + "label_info_title": "Main Title", + "dynamic_label_info": [ + { + "dynamic_label_title": "1", + "dynamic_label_text": "1- A maximum of up to 3 weekly doses may be required.", + "dynamic_label_image_src": "img/first_img.png" + }, + { + "dynamic_label_title": "2", + "dynamic_label_text": "", + "dynamic_label_image_src": "" + }, + { + "dynamic_label_title": "3rd Title", + "dynamic_label_text": "Third description text", + "dynamic_label_image_src": "img/first_img.png" + }, + { + "dynamic_label_title": "", + "dynamic_label_text": "", + "dynamic_label_image_src": "img/first_img.png" + } + ], + "label_is_dynamic": true, "options": [ { "key": "1",