Skip to content

Commit

Permalink
Merge pull request #546 from OpenSRP/images-and-text-label-view
Browse files Browse the repository at this point in the history
Implemented dynamic label view for showing list of images and texts
  • Loading branch information
qaziabubakar-vd authored Feb 4, 2021
2 parents dff5d57 + b5cc3b6 commit 5768c97
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<RecyclerView.ViewHolder> {
private final Context context;
private final ArrayList<DynamicLabelInfo> dynamicLabelInfoList;

public DynamicLabelAdapter(Context context, ArrayList<DynamicLabelInfo> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,13 +42,15 @@
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;
import com.vijay.jsonwizard.customviews.RadioButton;
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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<DynamicLabelInfo> 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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -495,13 +503,29 @@ public static HashMap<String, String> 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;
}

public static Drawable readImageFromAsset(Context context, String fileName) throws IOException {
return Drawable.createFromStream(context.getAssets().open(fileName), null);
}

public static ArrayList<DynamicLabelInfo> getDynamicLabelInfoList(JSONArray jsonArray) {
ArrayList<DynamicLabelInfo> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/customDialogRowLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/custom_dialog_margin"
android:orientation="vertical">

<com.vijay.jsonwizard.views.CustomTextView
android:id="@+id/labelTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:gravity="start"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"
tools:text="Here goes the Title!" />

<TextView
android:id="@+id/descriptionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:textSize="18sp"
android:visibility="gone"
tools:text="This is where the description must go!" />

<ImageView
android:id="@+id/imageViewLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/shows_image_on_dialog"
android:scaleType="fitXY"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
android:id="@+id/customDialogMainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/custom_dialog_margin"
android:orientation="vertical">

<com.vijay.jsonwizard.views.CustomTextView
android:id="@+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"
tools:text="Title" />

<android.support.v7.widget.RecyclerView
android:id="@+id/dialogRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/dialogButton"
android:layout_below="@+id/dialogTitle"
android:layout_gravity="center"
android:layout_margin="6dp"
android:overScrollMode="never"
android:visibility="gone" />

<android.support.v7.widget.AppCompatButton
android:id="@+id/dialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/okay_button"
android:padding="6dp"
android:text="@string/ok"
android:visibility="gone"
android:textAlignment="center"
android:textColor="@color/toaster_note_blue_icon"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
1 change: 1 addition & 0 deletions android-json-form-wizard/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<item name="skip_validation" type="id" />
<item name="label_dialog_info" type="id" />
<item name="label_dialog_title" type="id" />
<item name="dynamic_label_info" type="id" />
<item name="original_text" type="id" />
<item name="specify_textview" type="id" />
<item name="specify_reasons_textview" type="id" />
Expand Down
Loading

0 comments on commit 5768c97

Please sign in to comment.