From b9c2f68ffa4dabac29d9db82ec84f5158caeaf18 Mon Sep 17 00:00:00 2001 From: zainulabidin Date: Fri, 3 Jul 2020 14:26:06 +0500 Subject: [PATCH 1/2] format visual date for date picker widget --- .../java/com/vijay/jsonwizard/domain/Form.java | 10 ++++++++++ .../java/com/vijay/jsonwizard/utils/Utils.java | 16 ++++++++++++++++ .../jsonwizard/widgets/DatePickerFactory.java | 12 +++++++++--- .../com/vijay/jsonwizard/utils/UtilsTest.java | 12 ++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/domain/Form.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/domain/Form.java index de768dd37..563ae743d 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/domain/Form.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/domain/Form.java @@ -28,6 +28,8 @@ public class Form implements Serializable { private Set disabledFields; + private static String datePickerDisplayFormat; + public String getName() { return name; } @@ -139,4 +141,12 @@ public Set getDisabledFields() { public void setDisabledFields(Set disabledFields) { this.disabledFields = disabledFields; } + + public static String getDatePickerDisplayFormat() { + return datePickerDisplayFormat; + } + + public void setDatePickerDisplayFormat(String datePickerDisplayFormat) { + Form.datePickerDisplayFormat = datePickerDisplayFormat; + } } \ No newline at end of file diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 92010334d..20240f8ac 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -45,6 +45,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -775,6 +777,20 @@ public static void removeGeneratedDynamicRules(JsonFormFragment formFragment) { } } } + + + public static String formatDateToPattern(String date, String inputFormat, String outputFormat) { + if (StringUtils.isEmpty(date)) return ""; + SimpleDateFormat format = new SimpleDateFormat(inputFormat); + Date newDate = null; + try { + newDate = format.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + format = new SimpleDateFormat(outputFormat); + return format.format(newDate); + } } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java index d3e6f45c4..93b513141 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java @@ -21,6 +21,7 @@ import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.customviews.DatePickerDialog; import com.vijay.jsonwizard.customviews.GenericTextWatcher; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.interfaces.FormWidgetFactory; @@ -29,6 +30,7 @@ import com.vijay.jsonwizard.utils.FormUtils; import com.vijay.jsonwizard.utils.NativeFormLangUtils; import com.vijay.jsonwizard.utils.NativeFormsProperties; +import com.vijay.jsonwizard.utils.Utils; import com.vijay.jsonwizard.validators.edittext.RequiredValidator; import org.apache.commons.lang3.StringUtils; @@ -85,7 +87,9 @@ private static void showDatePickerDialog(Activity context, DatePickerDialog date } private void updateDateText(Context context, MaterialEditText editText, TextView duration, String date) { - editText.setText(date); + editText.setText(StringUtils.isNoneBlank(Form.getDatePickerDisplayFormat()) ? + Utils.formatDateToPattern(date, DATE_FORMAT.toPattern(), Form.getDatePickerDisplayFormat()) + : date); String durationLabel = (String) duration.getTag(R.id.label); if (StringUtils.isNotBlank(durationLabel)) { Locale locale = getSetLanguage(context); @@ -95,7 +99,6 @@ private void updateDateText(Context context, MaterialEditText editText, TextView } duration.setText(durationText); } - } @NotNull @@ -277,7 +280,7 @@ private void addRefreshLogicView(Context context, MaterialEditText editText, Str private void updateEditText(MaterialEditText editText, JSONObject jsonObject, String stepName, Context context, TextView duration) throws JSONException { Locale locale = getCurrentLocale(context); - SimpleDateFormat DATE_FORMAT_LOCALE = new SimpleDateFormat("dd-MM-yyyy", locale); + final SimpleDateFormat DATE_FORMAT_LOCALE = new SimpleDateFormat("dd-MM-yyyy", locale); String openMrsEntityParent = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY_PARENT); String openMrsEntity = jsonObject.getString(JsonFormConstants.OPENMRS_ENTITY); @@ -293,6 +296,7 @@ private void updateEditText(MaterialEditText editText, JSONObject jsonObject, St editText.setTag(R.id.openmrs_entity_id, openMrsEntityId); editText.setTag(R.id.address, stepName + ":" + jsonObject.getString(KEY.KEY)); editText.setTag(R.id.locale_independent_value, jsonObject.optString(KEY.VALUE)); + if (jsonObject.has(JsonFormConstants.V_REQUIRED)) { JSONObject requiredObject = jsonObject.optJSONObject(JsonFormConstants.V_REQUIRED); boolean requiredValue = requiredObject.getBoolean(KEY.VALUE); @@ -313,6 +317,7 @@ private void updateEditText(MaterialEditText editText, JSONObject jsonObject, St } } + @VisibleForTesting protected Locale getCurrentLocale(Context context) { return context.getResources().getConfiguration().locale.getLanguage().equals("ar") ? Locale.ENGLISH : context.getResources().getConfiguration().locale;//Arabic should render normal numbers/numeric digits @@ -368,6 +373,7 @@ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth return datePickerDialog; } + protected int getLayout() { return R.layout.native_form_item_date_picker; } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index c94e8893d..dab1b9f7b 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -43,6 +43,9 @@ import java.util.Map; import java.util.Set; +import static com.vijay.jsonwizard.utils.Utils.formatDateToPattern; +import static org.junit.Assert.assertEquals; + public class UtilsTest extends BaseTest { @Mock @@ -344,4 +347,13 @@ public void testRemoveGeneratedDynamicRulesShouldRemoveRules() throws JSONExcept Utils.removeGeneratedDynamicRules(jsonFormFragment); Assert.assertFalse(form.optJSONObject(JsonFormConstants.STEP1).optJSONArray(JsonFormConstants.FIELDS).optJSONObject(0).has(JsonFormConstants.RELEVANCE)); } + + @Test + public void testFormatDateToPattern() { + String date = "5/29/2020"; + String inputFormat = "dd/MM/yyyy"; + String outputFormat = "dd MMM yyyy"; + String formattedDate = formatDateToPattern(date, inputFormat, outputFormat); + assertEquals("05 May 2022", formattedDate); + } } \ No newline at end of file From 01e76ff74e2507c34e47ef639469d296f21b175f Mon Sep 17 00:00:00 2001 From: zainulabidin Date: Sun, 5 Jul 2020 19:20:48 +0500 Subject: [PATCH 2/2] Fix bug for age calculation --- .../java/com/vijay/jsonwizard/widgets/DatePickerFactory.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java index 93b513141..79ccabade 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/DatePickerFactory.java @@ -73,7 +73,9 @@ private static void showDatePickerDialog(Activity context, DatePickerDialog date context.getFragmentManager().executePendingTransactions(); String text = editText.getText().toString(); - Calendar date = FormUtils.getDate(text); + Calendar date = FormUtils.getDate(StringUtils.isNoneBlank(Form.getDatePickerDisplayFormat()) ? + Utils.formatDateToPattern(text, Form.getDatePickerDisplayFormat(), DATE_FORMAT.toPattern()) + : text); if (text.isEmpty()) { Object defaultValue = datePickerDialog.getArguments().get(JsonFormConstants.DEFAULT); if (defaultValue != null)