Skip to content

Commit

Permalink
Merge pull request #484 from OpenSRP/483-fix-calculation-date-format
Browse files Browse the repository at this point in the history
Fix DatePicker calculations date format
  • Loading branch information
allan-on authored Sep 15, 2020
2 parents 8c62e5c + e779d52 commit 86299f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

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

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 86299f0

Please sign in to comment.