-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds daterange picker for case search #2508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/> | ||
</vector> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.commcare.utils; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import androidx.core.util.Pair; | ||
|
||
/** | ||
* Utility functions for DateRangePicker widget | ||
*/ | ||
public class DateRangeUtils { | ||
|
||
// Changing this will require changing this format on ES end as well | ||
public static final String DATE_RANGE_ANSWER_PREFIX = "__range__"; | ||
public static final String DATE_RANGE_ANSWER_DELIMITER = "__"; | ||
public static final String DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER = " to "; | ||
private static final String DATE_FORMAT = "yyyy-MM-dd"; | ||
|
||
/** | ||
* @param humanReadableDateRange human readable fomat for date range as 'startDate to endDate' | ||
* @return a Pair of start time and end time that can be supplied to MaterialDatePicker to set a date range, | ||
* @throws ParseException if the given humanReadableDateRange is not in 'yyyy-mm-dd to yyyy-mm-dd' format | ||
*/ | ||
@Nullable | ||
public static Pair<Long, Long> parseHumanReadableDate(String humanReadableDateRange) throws ParseException { | ||
if (humanReadableDateRange.contains(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER)) { | ||
String[] humanReadableDateRangeSplit = humanReadableDateRange.split(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER); | ||
if (humanReadableDateRangeSplit.length == 2) { | ||
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US); | ||
Date startDate = sdf.parse(humanReadableDateRangeSplit[0]); | ||
Date endDate = sdf.parse(humanReadableDateRangeSplit[1]); | ||
return new Pair<>(getTimeFromDateOffsettingTz(startDate), getTimeFromDateOffsettingTz(endDate)); | ||
} | ||
} | ||
throw new ParseException("Argument " + humanReadableDateRange + " should be formatted as 'yyyy-mm-dd to yyyy-mm-dd'", 0); | ||
} | ||
|
||
private static Long getTimeFromDateOffsettingTz(Date date) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what it returns? Can you see whether a similar utility exists in DataUtils? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't able to find it anywhere else. It removes the timezone offset from the date in millis and return the date. This is because when we set the date to the daterange widget we are setting time in milliseconds without any indication of timezone and the date widget itself doesn't offset the timezone from it to arrive at the offsetted date. |
||
return date.getTime() - date.getTimezoneOffset() * 60 * 1000; | ||
} | ||
|
||
/** | ||
* Formats __range__startDate__endDate as 'startDate to EndDate' | ||
* | ||
* @param dateRangeAnswer A date range value in form of '__range__startDate__endDate' | ||
* @return human readable format 'startDate to EndDate' for given dateRangeAnswer | ||
*/ | ||
public static String getHumanReadableDateRange(String dateRangeAnswer) { | ||
if (dateRangeAnswer != null && dateRangeAnswer.startsWith(DATE_RANGE_ANSWER_PREFIX)) { | ||
String[] dateRangeSplit = dateRangeAnswer.split(DATE_RANGE_ANSWER_DELIMITER); | ||
if (dateRangeSplit.length == 3) { | ||
return getHumanReadableDateRange(dateRangeSplit[2], dateRangeSplit[3]); | ||
} | ||
} | ||
return dateRangeAnswer; | ||
} | ||
|
||
|
||
// Formats as 'startDate to endDate' | ||
public static String getHumanReadableDateRange(String startDate, String endDate) { | ||
return startDate + DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER + endDate; | ||
} | ||
|
||
// Formats as '__range__startDate__endDate' | ||
public static String formatDateRangeAnswer(String startDate, String endDate) { | ||
return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate; | ||
} | ||
|
||
// Convers given time as yyyy-mm-dd | ||
public static String getDateFromTime(Long time) { | ||
return new SimpleDateFormat(DATE_FORMAT, Locale.US).format(new Date(time)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.commcare.utils; | ||
|
||
import org.junit.Test; | ||
|
||
import java.text.ParseException; | ||
|
||
import androidx.core.util.Pair; | ||
|
||
public class DateRangeUtilsTest { | ||
|
||
@Test | ||
public void testDateConversion() throws ParseException { | ||
String dateRange = "2020-02-15 to 2021-03-18"; | ||
Pair<Long, Long> selection = DateRangeUtils.parseHumanReadableDate(dateRange); | ||
String startDate = DateRangeUtils.getDateFromTime(selection.first); | ||
String endDate = DateRangeUtils.getDateFromTime(selection.second); | ||
String humanReadableDateRange = DateRangeUtils.getHumanReadableDateRange(startDate, endDate); | ||
assert dateRange.contentEquals(humanReadableDateRange); | ||
assert DateRangeUtils.formatDateRangeAnswer(startDate, endDate).contentEquals("__range__2020-02-15__2021-03-18"); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice,
Bridge
is the reason why it's working on pre-lollipop right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No not really, Bridge theme is just a workaround for apps not comfortable with changing their theme to a MaterialComponent theme. Bridge theme inherits from the AppCompat Theme and adds any properties defined by the MaterialComponent theme in it. So it has nothing to do with device compatibility. The library support the 14+ devices by default.