Skip to content

Commit

Permalink
#7 Add another predefined Date format: "Default with Yesterday, Today…
Browse files Browse the repository at this point in the history
… and Tomorrow". No additional numbers for farther days are being added in this format.

Add "BBB" and "BBBB" custom patterns.
"BBB" stands for adding "Yesterday, Today and Tomorrow" only. "BBBB", in addition to it, adds "N days ago" and "in N days" for farther days.
  • Loading branch information
yvolk committed Mar 13, 2020
1 parent ae51488 commit 9222fc7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.andstatus.todoagenda.prefs;

import org.andstatus.todoagenda.BaseWidgetTest;
import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatValue;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatter;
Expand Down Expand Up @@ -64,6 +65,14 @@ public void customPatterns() {
assertPattern(now.plusDays(1), "", "");
assertPattern(now.plusDays(1), "b", "1");
assertPattern(now.plusDays(1), "bbb", "001");
CharSequence tomorrowText = provider.getContext().getText(R.string.tomorrow);
assertPattern(now.plusDays(1), "bbbb", tomorrowText.toString());
assertPattern(now.plusDays(1), "BBB", tomorrowText.toString());
assertPattern(now.plusDays(1), "BBBB", tomorrowText.toString());
assertPattern(now.plusDays(-2), "BBB", "");
assertPattern(now.plusDays(-2), "BBBB", String.format(provider.getContext().getText(R.string.N_days_ago).toString(), 2));
assertPattern(now.plusDays(2), "BBB", "");
assertPattern(now.plusDays(2), "BBBB", String.format(provider.getContext().getText(R.string.in_N_days).toString(), 2));
assertPattern(now.plusDays(5), "b", "5");
assertPattern(now.plusDays(5), "bbb", "005");
assertPattern(now.plusDays(5), "bbbb", "5");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum DateFormatType {
HIDDEN("hidden", R.string.hidden, ""),
DEVICE_DEFAULT("deviceDefault", R.string.device_default, ""),
DEFAULT_WEEKDAY("defaultWeekday", R.string.date_format_default_weekday, ""),
DEFAULT_YTT("defaultYtt", R.string.date_format_default_ytt, ""),
DEFAULT_DAYS("defaultDays", R.string.date_format_default_days, ""),
ABBREVIATED("abbrev", R.string.appearance_abbreviate_dates_title, ""),
NUMBER_OF_DAYS("days", R.string.date_format_number_of_days_to_event, "bbbb"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.util.Locale;

public class DateFormatter {
private static final char NUMBER_OF_DAYS_LETTER = 'b';
private static final char NUMBER_OF_DAYS_LOWER_LETTER = 'b';
private static final char NUMBER_OF_DAYS_UPPER_LETTER = 'B';

private final Context context;
private final DateFormatValue dateFormatValue;
private final DateTime now;
Expand Down Expand Up @@ -59,10 +61,13 @@ public CharSequence formatDate(DateTime date) {
return formatDateTime(date, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY);
case DEFAULT_DAYS:
return getNumberOfDaysToEventString(context, 5, getNumberOfDaysToEvent(date)) + ", " +
return formatNumberOfDaysToEvent(context, 5, getNumberOfDaysToEvent(date)) + ", " +
formatDateTime(date, DateUtils.FORMAT_SHOW_DATE);
case DEFAULT_YTT:
CharSequence str1 = formatNumberOfDaysToEventText(context, 3, getNumberOfDaysToEvent(date));
return (str1.length() == 0 ? "" : str1 + ", ") + formatDateTime(date, DateUtils.FORMAT_SHOW_DATE);
case NUMBER_OF_DAYS:
return getNumberOfDaysToEventString(context, 5, getNumberOfDaysToEvent(date));
return formatNumberOfDaysToEvent(context, 5, getNumberOfDaysToEvent(date));
default:
return "(not implemented)";
}
Expand All @@ -86,18 +91,10 @@ public static Date toJavaDate(DateTime date) {
return new Date(date.getYearOfEra() - 1900, date.getMonthOfYear() - 1, date.getDayOfMonth());
}

public static CharSequence getNumberOfDaysToEventString(Context context, int formatLength, int daysToEvent) {
public static CharSequence formatNumberOfDaysToEvent(Context context, int formatLength, int daysToEvent) {
if (formatLength >= 4) {
switch (daysToEvent) {
case -1:
return context.getText(R.string.yesterday);
case 0:
return context.getText(R.string.today);
case 1:
return context.getText(R.string.tomorrow);
default:
break;
}
CharSequence ytt = getYtt(context, daysToEvent);
if (ytt.length() > 0) return ytt;
}
if (Math.abs(daysToEvent) > 9999) return "...";

Expand All @@ -107,7 +104,30 @@ public static CharSequence getNumberOfDaysToEventString(Context context, int for
return String.format("%0" + formatLength + "d", daysToEvent);
}

public int getNumberOfDaysToEvent(DateTime date) {
public static CharSequence formatNumberOfDaysToEventText(Context context, int formatLength, int daysToEvent) {
CharSequence ytt = getYtt(context, daysToEvent);
if (ytt.length() > 0) return ytt;

if (formatLength < 4) return "";

return String.format(context.getText(daysToEvent < 0 ? R.string.N_days_ago : R.string.in_N_days).toString(),
Math.abs(daysToEvent));
}

public static CharSequence getYtt(Context context, int daysToEvent) {
switch (daysToEvent) {
case -1:
return context.getText(R.string.yesterday);
case 0:
return context.getText(R.string.today);
case 1:
return context.getText(R.string.tomorrow);
default:
return "";
}
}

private int getNumberOfDaysToEvent(DateTime date) {
return Days.daysBetween(
now.withZone(date.getZone()).withTimeAtStartOfDay(),
date.withTimeAtStartOfDay())
Expand All @@ -130,20 +150,24 @@ private String preProcessNumberOfDaysToEvent(DateTime date, String pattern) {
int ind1 = getIndexOfNumberOfDaysLetter(pattern);
if (ind1 < 0) return pattern;

char patternLetter = pattern.charAt(ind1);
int ind2 = ind1;
while (ind2 < pattern.length() && pattern.charAt(ind2) == NUMBER_OF_DAYS_LETTER) {
while (ind2 < pattern.length() && pattern.charAt(ind2) == patternLetter) {
ind2++;
}
CharSequence result = getNumberOfDaysToEventString(context, ind2 - ind1, getNumberOfDaysToEvent(date));
CharSequence result = patternLetter == NUMBER_OF_DAYS_LOWER_LETTER
? formatNumberOfDaysToEvent(context, ind2 - ind1, getNumberOfDaysToEvent(date))
: formatNumberOfDaysToEventText(context, ind2 - ind1, getNumberOfDaysToEvent(date));
return (ind1 > 0 ? pattern.substring(0, ind1) : "") +
"'" + result + "'" +
(ind2 < pattern.length() ? pattern.substring(ind2) : "");
(result.length() == 0 ? "" : "'" + result + "'") +
(ind2 < pattern.length() ? pattern.substring(ind2) : "");
}

private int getIndexOfNumberOfDaysLetter(String pattern) {
boolean inQuotes = false;
for (int ind = 0; ind < pattern.length(); ind++) {
if ((pattern.charAt(ind) == NUMBER_OF_DAYS_LETTER) && !inQuotes) return ind;
if ((pattern.charAt(ind) == NUMBER_OF_DAYS_LOWER_LETTER || pattern.charAt(ind) == NUMBER_OF_DAYS_UPPER_LETTER)
&& !inQuotes) return ind;

if (pattern.charAt(ind) == '\'') inQuotes = !inQuotes;
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<!-- Date format preference -->
<string name="simple_options">Simple options</string>
<string name="date_format_default_weekday">Default with Weekday</string>
<string name="date_format_default_ytt">Default with Yesterday, Today and Tomorrow</string>
<string name="date_format_default_days">Default with Number of days to event</string>
<string name="appearance_abbreviate_dates_title">Abbreviate dates with Weekday</string>
<string name="appearance_abbreviate_dates_desc">Use three-letter format for dates</string>
Expand All @@ -112,6 +113,8 @@
<string name="custom_pattern">Custom pattern</string>
<string name="sample_date">Sample date (in yyyy-MM-dd format)</string>
<string name="result_formatted_date">Result, formatted date</string>
<string name="N_days_ago">%d days ago</string>
<string name="in_N_days">in %d days</string>

<!-- Preference frame: Colors -->
<string name="colors_prefs_desc">Colors and opacity of texts and backgrounds</string>
Expand Down

0 comments on commit 9222fc7

Please sign in to comment.