Skip to content

Commit

Permalink
Merge pull request #443 from OpenSRP/format-visual-date-for-date-pick…
Browse files Browse the repository at this point in the history
…er-widget

format visual date for date picker widget
  • Loading branch information
zzainulabidin authored Jul 6, 2020
2 parents 29d4acd + e59a164 commit ab6aa0e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Form implements Serializable {

private Set<String> disabledFields;

private static String datePickerDisplayFormat;

public String getName() {
return name;
}
Expand Down Expand Up @@ -139,4 +141,12 @@ public Set<String> getDisabledFields() {
public void setDisabledFields(Set<String> disabledFields) {
this.disabledFields = disabledFields;
}

public static String getDatePickerDisplayFormat() {
return datePickerDisplayFormat;
}

public void setDatePickerDisplayFormat(String datePickerDisplayFormat) {
Form.datePickerDisplayFormat = datePickerDisplayFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -71,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)
Expand All @@ -85,7 +89,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);
Expand All @@ -95,7 +101,6 @@ private void updateDateText(Context context, MaterialEditText editText, TextView
}
duration.setText(durationText);
}

}

@NotNull
Expand Down Expand Up @@ -277,7 +282,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);
Expand All @@ -293,6 +298,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);
Expand All @@ -313,6 +319,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
Expand Down Expand Up @@ -368,6 +375,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ab6aa0e

Please sign in to comment.