Skip to content

Commit

Permalink
#7 Use "Entry date format" setting for both "Event entry layouts". I.…
Browse files Browse the repository at this point in the history
…e. Date may be shown before time in the "Time below title" layout
  • Loading branch information
yvolk committed Feb 13, 2020
1 parent be39309 commit 45110fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.andstatus.todoagenda.TextSizeScale;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatValue;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatter;
import org.andstatus.todoagenda.provider.EventProviderType;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.InstanceId;
Expand Down Expand Up @@ -240,6 +241,9 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_SHOW_EVENT_ICON)) {
showEventIcon = json.getBoolean(PREF_SHOW_EVENT_ICON);
}
if (json.has(PREF_EVENT_ENTRY_LAYOUT)) {
setEventEntryLayout(EventEntryLayout.fromValue(json.getString(PREF_EVENT_ENTRY_LAYOUT)));
}
if (json.has(PREF_ENTRY_DATE_FORMAT)) {
entryDateFormat = DateFormatValue.load(
json.getString(PREF_ENTRY_DATE_FORMAT), PREF_ENTRY_DATE_FORMAT_DEFAULT);
Expand Down Expand Up @@ -271,9 +275,6 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_REFRESH_PERIOD_MINUTES)) {
setRefreshPeriodMinutes(json.getInt(PREF_REFRESH_PERIOD_MINUTES));
}
if (json.has(PREF_EVENT_ENTRY_LAYOUT)) {
setEventEntryLayout(EventEntryLayout.fromValue(json.getString(PREF_EVENT_ENTRY_LAYOUT)));
}
if (json.has(PREF_MULTILINE_TITLE)) {
multilineTitle = json.getBoolean(PREF_MULTILINE_TITLE);
}
Expand Down Expand Up @@ -788,4 +789,8 @@ private int maxSourceId() {
.max(Comparator.comparingInt(id -> id)).orElse(1);
return Math.max(id1, id2);
}

public DateFormatter newDateformatter() {
return new DateFormatter(context, getEntryDateFormat(), clock().now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public CharSequence formatMillis(long millis) {
return formatDateTime(millis, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY);
case DEFAULT_DAYS:
return getDaysFromTodayString(context, 5, getDaysFromToday(millis)) + ", " +
return getNumberOfDaysToEventString(context, 5, getNumberOfDaysToEvent(millis)) + ", " +
formatDateTime(millis, DateUtils.FORMAT_SHOW_DATE);
case NUMBER_OF_DAYS:
return getDaysFromTodayString(context, 5, getDaysFromToday(millis));
return getNumberOfDaysToEventString(context, 5, getNumberOfDaysToEvent(millis));
default:
return "(not implemented)";
}
Expand All @@ -81,9 +81,9 @@ private String formatDateTime(long millis, int flags) {
.toString();
}

public static CharSequence getDaysFromTodayString(Context context, int formatLength, int daysFromToday) {
public static CharSequence getNumberOfDaysToEventString(Context context, int formatLength, int daysToEvent) {
if (formatLength > 4) {
switch (daysFromToday) {
switch (daysToEvent) {
case -1:
return context.getText(R.string.yesterday);
case 0:
Expand All @@ -94,10 +94,10 @@ public static CharSequence getDaysFromTodayString(Context context, int formatLen
break;
}
}
return Math.abs(daysFromToday) > 9999 ? "..." : Integer.toString(daysFromToday);
return Math.abs(daysToEvent) > 9999 ? "..." : Integer.toString(daysToEvent);
}

public int getDaysFromToday(long millis) {
public int getNumberOfDaysToEvent(long millis) {
return Days.daysBetween(now.withTimeAtStartOfDay(),
Instant.ofEpochMilli(millis)).getDays();
}
Expand All @@ -120,7 +120,7 @@ private String preProcessNumberOfDaysToEvent(long millis, String pattern) {
while (ind2 < pattern.length() && pattern.charAt(ind2) == NUMBER_OF_DAYS_LETTER) {
ind2++;
}
CharSequence result = getDaysFromTodayString(context, ind2 - ind1, getDaysFromToday(millis));
CharSequence result = getNumberOfDaysToEventString(context, ind2 - ind1, getNumberOfDaysToEvent(millis));
return (ind1 > 0 ? pattern.substring(0, ind1) : "") +
"'" + result + "'" +
(ind2 < pattern.length() ? pattern.substring(ind2) : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.andstatus.todoagenda.prefs.TextShadingPref;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.provider.EventProvider;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.EventEntryLayout;
import org.andstatus.todoagenda.widget.TaskEntry;
import org.andstatus.todoagenda.widget.WidgetEntry;
Expand Down Expand Up @@ -59,23 +58,24 @@ private void setDaysToEvent(TaskEntry entry, RemoteViews rv) {
rv.setViewVisibility(R.id.event_entry_days_right, View.GONE);
rv.setViewVisibility(R.id.event_entry_time, View.GONE);
} else {
if (getSettings().getEntryDateFormat().type == DateFormatType.NUMBER_OF_DAYS) {
int days = entry.getDaysFromToday();
boolean daysAsText = days > -2 && days < 2;
if (getSettings().getEntryDateFormat().type == DateFormatType.HIDDEN) {
rv.setViewVisibility(R.id.event_entry_days, View.GONE);
rv.setViewVisibility(R.id.event_entry_days_right, View.GONE);
} else {
int days = entry.getDaysToEvent();
boolean daysAsText = getSettings().getEntryDateFormat().type != DateFormatType.NUMBER_OF_DAYS ||
days > -2 && days < 2;
int viewToShow = daysAsText ? R.id.event_entry_days : R.id.event_entry_days_right;
int viewToHide = daysAsText ? R.id.event_entry_days_right : R.id.event_entry_days;
rv.setViewVisibility(viewToHide, View.GONE);
rv.setViewVisibility(viewToShow, View.VISIBLE);
setViewWidth(getSettings(), rv, viewToShow, daysAsText
? R.dimen.days_to_event_width
: R.dimen.days_to_event_right_width);
rv.setTextViewText(viewToShow, DateUtil.getDaysFromTodayString(getSettings().getContext(), days));
rv.setTextViewText(viewToShow, entry.formatEntryDate());
setTextSize(getSettings(), rv, viewToShow, R.dimen.event_entry_details);
setTextColorFromAttr(getSettings().getShadingContext(TextShadingPref.forDetails(entry)),
rv, viewToShow, R.attr.dayHeaderTitle);
} else {
rv.setViewVisibility(R.id.event_entry_days, View.GONE);
rv.setViewVisibility(R.id.event_entry_days_right, View.GONE);
}
setViewWidth(getSettings(), rv, R.id.event_entry_time, R.dimen.event_time_width);
rv.setViewVisibility(R.id.event_entry_time, View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.TextShadingPref;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatter;
import org.andstatus.todoagenda.util.RemoteViewsUtil;
import org.andstatus.todoagenda.util.StringUtil;

import static org.andstatus.todoagenda.util.RemoteViewsUtil.setMultiline;
import static org.andstatus.todoagenda.util.RemoteViewsUtil.setTextColorFromAttr;
Expand All @@ -26,8 +26,10 @@ public enum EventEntryLayout {
DEFAULT(R.layout.event_entry, "DEFAULT", R.string.default_multiline_layout) {
@Override
protected void setEventDetails(CalendarEntry entry, RemoteViews rv) {
String eventDetails = appendWithSeparator(
entry.getEventTimeString(), SPACE_PIPE_SPACE, entry.getLocationString());
String eventDetails = appendWithSeparator(entry.formatEntryDate().toString(), " ",
appendWithSeparator(
entry.getEventTimeString(), SPACE_PIPE_SPACE, entry.getLocationString())
);
int viewId = R.id.event_entry_details;
if (TextUtils.isEmpty(eventDetails)) {
rv.setViewVisibility(viewId, View.GONE);
Expand All @@ -53,10 +55,7 @@ protected void setDaysToEvent(CalendarEntry entry, RemoteViews rv) {
rv.setViewVisibility(R.id.event_entry_days, View.GONE);
rv.setViewVisibility(R.id.event_entry_days_right, View.GONE);
} else {
DateFormatter formatter = new DateFormatter(entry.getContext(), entry.getSettings().getEntryDateFormat(),
entry.getSettings().clock().now());

int days = entry.getDaysFromToday();
int days = entry.getDaysToEvent();
boolean daysAsText = entry.getSettings().getEntryDateFormat().type != DateFormatType.NUMBER_OF_DAYS ||
(days > -2 && days < 2);

Expand All @@ -65,7 +64,7 @@ protected void setDaysToEvent(CalendarEntry entry, RemoteViews rv) {
rv.setViewVisibility(viewToHide, View.GONE);
rv.setViewVisibility(viewToShow, View.VISIBLE);

rv.setTextViewText(viewToShow, formatter.formatMillis(entry.entryDate.getMillis()));
rv.setTextViewText(viewToShow, entry.formatEntryDate());
InstanceSettings settings = entry.getSettings();
setViewWidth(settings, rv, viewToShow, daysAsText
? R.dimen.days_to_event_width
Expand Down Expand Up @@ -147,7 +146,7 @@ protected void setEventDetails(CalendarEntry entry, RemoteViews rv) {
}

public static String appendWithSeparator(String input, String separator, String toAppend) {
return input == null || input.length() == 0
return StringUtil.isEmpty(input)
? toAppend
: (toAppend == null || toAppend.length() == 0
? input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.OrderedEventSource;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.util.MyClock;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -134,7 +135,7 @@ public String getLocation() {
return "";
}

public int getDaysFromToday() {
public int getDaysToEvent() {
return Days.daysBetween(settings.clock().now(entryDate.getZone()).withTimeAtStartOfDay(),
entryDate.withTimeAtStartOfDay()).getDays();
}
Expand Down Expand Up @@ -175,6 +176,12 @@ public boolean duplicates(WidgetEntry other) {
getLocation().equals(other.getLocation());
}

public CharSequence formatEntryDate() {
return settings.getEntryDateFormat().type == DateFormatType.HIDDEN
? ""
: settings.newDateformatter().formatMillis(entryDate.getMillis());
}

@Override
public String toString() {
return entryPosition.value + " [" +
Expand Down

0 comments on commit 45110fc

Please sign in to comment.