diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java index ed24fc0bc..18530581b 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java @@ -3,6 +3,7 @@ import android.support.annotation.NonNull; import android.text.TextUtils; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.utils.FormUtils; import com.vijay.jsonwizard.utils.Utils; @@ -78,7 +79,7 @@ public String getWeeksAndDaysFromDays(Integer days) { } public String formatDate(String dateString, String duration) { - LocalDate date = new LocalDate(Utils.reverseDateString(dateString, "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); int result = 0; String cleanDuration = duration.trim().toLowerCase(); @@ -128,7 +129,7 @@ public String addDuration(String durationString) { * @return String with date */ public String addDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(dateString, "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); String[] durationArr = getDurationArray(durationString); for (String duration : durationArr) { @@ -181,7 +182,7 @@ public String subtractDuration(String durationString) { */ public String subtractDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(dateString, "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); String[] durationArr = getDurationArray(durationString); 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 e6fe5f9fc..9ddae8dd1 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 @@ -107,6 +107,13 @@ public static String reverseDateString(String str, String delimiter) { return strr[2] + "-" + strr[1] + "-" + strr[0]; } + public static String getDateFormattedForCalculation(String date, String datePickerDisplayFormat) { + if (StringUtils.isNotBlank(datePickerDisplayFormat)) { + return formatDateToPattern(date, datePickerDisplayFormat, DatePickerFactory.DATE_FORMAT.toPattern()); + } else + return date; + } + public static String getDuration(String date) { return getDuration(date, null); } @@ -818,7 +825,7 @@ public static int getResourceId(Context context, String name, ResourceType resou return -1; } } - + public static boolean isEmptyJsonArray(JSONArray jsonArray) { return jsonArray == null || jsonArray.length() == 0; } 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 bbb60327a..4d46b8df5 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 @@ -13,6 +13,7 @@ import com.vijay.jsonwizard.BaseTest; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.JsonApi; import com.vijay.jsonwizard.rules.RuleConstant; @@ -45,9 +46,6 @@ import static com.vijay.jsonwizard.utils.Utils.formatDateToPattern; import static com.vijay.jsonwizard.utils.Utils.isEmptyJsonArray; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; public class UtilsTest extends BaseTest { @@ -330,6 +328,21 @@ public void testReverseDateStringShouldReturnReverseDateString() { Assert.assertEquals("1997-12-20", Utils.reverseDateString(date, "-")); } + @Test + public void getDateForCalculationShouldReturnCorrectDateString() { + String date = "20-12-2021"; + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date, null)); + } + + @Test + public void getDateForCalculationReturnsDateStringWhenDisplayFormatIsSet() { + Form form = new Form(); + form.setDatePickerDisplayFormat("dd MMM yyyy"); + String date = "20 DEC 2021"; + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date, Form.getDatePickerDisplayFormat())); + form.setDatePickerDisplayFormat(null); // To not pollute other tests + } + @Test public void testGetStringValueShouldReturnStringValue() throws Exception { JSONObject jsonObject = new JSONObject(); @@ -357,15 +370,15 @@ public void testFormatDateToPattern() { String inputFormat = "dd/MM/yyyy"; String outputFormat = "dd MMM yyyy"; String formattedDate = formatDateToPattern(date, inputFormat, outputFormat); - assertEquals("05 May 2022", formattedDate); + Assert.assertEquals("05 May 2022", formattedDate); } @Test public void testIsEmptyJsonArrayShouldReturnCorrectStatus() { - assertTrue(isEmptyJsonArray(null)); + Assert.assertTrue(isEmptyJsonArray(null)); JSONArray jsonArray = new JSONArray(); - assertTrue(isEmptyJsonArray(jsonArray)); + Assert.assertTrue(isEmptyJsonArray(jsonArray)); jsonArray.put("value"); - assertFalse(isEmptyJsonArray(jsonArray)); + Assert.assertFalse(isEmptyJsonArray(jsonArray)); } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 1d2ee72b0..0d1a873e0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.14.3.0-SNAPSHOT +VERSION_NAME=1.14.3.1-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard