Skip to content

Commit

Permalink
#7 Add "Day header date format" setting to Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Feb 15, 2020
1 parent 8f10274 commit d0a45b9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.andstatus.todoagenda;

import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.DayHeader;
import org.json.JSONException;
import org.junit.Test;
Expand All @@ -28,7 +27,7 @@ public void testDayHeadersShift() throws IOException, JSONException {

assertEquals("First day header should be Jan 8\n" + getFactory().getWidgetEntries(), 8,
dayHeader0.entryDate.dayOfMonth().get());
String dayHeaderTitle = DateUtil.createDayHeaderTitle(getSettings(), dayHeader0.entryDate);
CharSequence dayHeaderTitle = getSettings().dayHeaderDateFormatter().formatDate(dayHeader0.entryDate);
assertEquals("First day header should show Jan 8\n" + getFactory().getWidgetEntries() + "\n",
"Today, January 8", dayHeaderTitle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ private List<WidgetEntry> addDayHeaders(List<WidgetEntry> listIn) {
List<WidgetEntry> listOut = new ArrayList<>();
if (!listIn.isEmpty()) {
InstanceSettings settings = getSettings();
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
DayHeader curDayBucket = new DayHeader(settings, DAY_HEADER, MyClock.DATETIME_MIN);
boolean pastEventsHeaderAdded = false;
boolean endOfListHeaderAdded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ABBREVIATE_DATES_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ACTIVE_SOURCES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_ALIGNMENT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_DATE_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_DATE_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_DATE_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_DATE_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_BACKGROUND_COLOR;
Expand Down Expand Up @@ -94,6 +96,7 @@ public static void fromInstanceSettings(Context context, Integer widgetId) {
setInt(context, PREF_EVENTS_BACKGROUND_COLOR, settings.getEventsBackgroundColor());
setShowDaysWithoutEvents(context, settings.getShowDaysWithoutEvents());
setShowDayHeaders(context, settings.getShowDayHeaders());
setDateFormat(context, PREF_DAY_HEADER_DATE_FORMAT, settings.getDayHeaderDateFormat());
setHorizontalLineBelowDayHeader(context, settings.getHorizontalLineBelowDayHeader());
setShowPastEventsUnderOneHeader(context, settings.getShowPastEventsUnderOneHeader());
setShowPastEventsWithDefaultColor(context, settings.getShowPastEventsWithDefaultColor());
Expand Down Expand Up @@ -266,6 +269,10 @@ public static boolean getShowLocation(Context context) {
return getBoolean(context, PREF_SHOW_LOCATION, PREF_SHOW_LOCATION_DEFAULT);
}

public static DateFormatValue getDayHeaderDateFormat(Context context) {
return getDateFormat(context, PREF_DAY_HEADER_DATE_FORMAT, PREF_DAY_HEADER_DATE_FORMAT_DEFAULT);
}

public static DateFormatValue getWidgetHeaderDateFormat(Context context) {
return getDateFormat(context, PREF_WIDGET_HEADER_DATE_FORMAT, PREF_WIDGET_HEADER_DATE_FORMAT_DEFAULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class InstanceSettings {
private DateFormatValue widgetHeaderDateFormat = PREF_WIDGET_HEADER_DATE_FORMAT_DEFAULT;
static final String PREF_SHOW_DAY_HEADERS = "showDayHeaders";
private boolean showDayHeaders = true;
static final String PREF_DAY_HEADER_DATE_FORMAT = "dayHeaderDateFormat";
static final DateFormatValue PREF_DAY_HEADER_DATE_FORMAT_DEFAULT = DateFormatType.DEFAULT_DAYS.defaultValue();
private DateFormatValue dayHeaderDateFormat = PREF_DAY_HEADER_DATE_FORMAT_DEFAULT;
static final String PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER = "showPastEventsUnderOneHeader";
private boolean showPastEventsUnderOneHeader = false;
static final String PREF_DAY_HEADER_ALIGNMENT = "dayHeaderAlignment";
Expand Down Expand Up @@ -236,6 +239,10 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_SHOW_DAY_HEADERS)) {
showDayHeaders = json.getBoolean(PREF_SHOW_DAY_HEADERS);
}
if (json.has(PREF_DAY_HEADER_DATE_FORMAT)) {
dayHeaderDateFormat = DateFormatValue.load(
json.getString(PREF_DAY_HEADER_DATE_FORMAT), PREF_DAY_HEADER_DATE_FORMAT_DEFAULT);
}
if (json.has(PREF_HORIZONTAL_LINE_BELOW_DAY_HEADER)) {
horizontalLineBelowDayHeader = json.getBoolean(PREF_HORIZONTAL_LINE_BELOW_DAY_HEADER);
}
Expand Down Expand Up @@ -352,6 +359,7 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId
settings.eventsBackgroundColor = ApplicationPreferences.getEventsBackgroundColor(context);
settings.showDaysWithoutEvents = ApplicationPreferences.getShowDaysWithoutEvents(context);
settings.showDayHeaders = ApplicationPreferences.getShowDayHeaders(context);
settings.dayHeaderDateFormat = ApplicationPreferences.getDayHeaderDateFormat(context);
settings.horizontalLineBelowDayHeader = ApplicationPreferences.getHorizontalLineBelowDayHeader(context);
settings.showPastEventsUnderOneHeader = ApplicationPreferences.getShowPastEventsUnderOneHeader(context);
settings.showPastEventsWithDefaultColor = ApplicationPreferences.getShowPastEventsWithDefaultColor(context);
Expand Down Expand Up @@ -442,6 +450,7 @@ public JSONObject toJson() {
json.put(PREF_EVENTS_BACKGROUND_COLOR, eventsBackgroundColor);
json.put(PREF_SHOW_DAYS_WITHOUT_EVENTS, showDaysWithoutEvents);
json.put(PREF_SHOW_DAY_HEADERS, showDayHeaders);
json.put(PREF_DAY_HEADER_DATE_FORMAT, dayHeaderDateFormat.save());
json.put(PREF_HORIZONTAL_LINE_BELOW_DAY_HEADER, horizontalLineBelowDayHeader);
json.put(PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER, showPastEventsUnderOneHeader);
json.put(PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR, showPastEventsWithDefaultColor);
Expand Down Expand Up @@ -576,6 +585,14 @@ public DateFormatValue getWidgetHeaderDateFormat() {
return widgetHeaderDateFormat;
}

public DateFormatter dayHeaderDateFormatter() {
return new DateFormatter(context, getDayHeaderDateFormat(), clock().now());
}

public DateFormatValue getDayHeaderDateFormat() {
return dayHeaderDateFormat;
}

public DateFormatter entryDateFormatter() {
return new DateFormatter(context, getEntryDateFormat(), clock().now());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.andstatus.todoagenda.MainActivity;
import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatter;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.DateUtil;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -95,8 +97,9 @@ private void showSnapshotMode() {
private String formatSnapshotModeSummary(InstanceSettings settings, int valueResId) {
return String.format(
getText(valueResId).toString(),
DateUtil.createDateString(getSettings(), settings.clock().now()) + " " +
DateUtil.formatTime(this::getSettings, settings.clock().now())
new DateFormatter(settings.getContext(), DateFormatType.DEFAULT_WEEKDAY.defaultValue(),
settings.clock().now()) + " " +
DateUtil.formatTime(this::getSettings, settings.clock().now())
);
}

Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/org/andstatus/todoagenda/util/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public static boolean isMidnight(DateTime date) {
return date.isEqual(date.withTimeAtStartOfDay());
}

public static String createDayHeaderTitle(InstanceSettings settings, DateTime dateTime) {
return createDateString(settings, dateTime, true);
}

public static String createDateString(InstanceSettings settings, DateTime dateTime) {
return createDateString(settings, dateTime, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.andstatus.todoagenda.prefs.TextShadingPref;
import org.andstatus.todoagenda.provider.EventProvider;
import org.andstatus.todoagenda.provider.EventProviderType;
import org.andstatus.todoagenda.util.DateUtil;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -54,8 +53,7 @@ public RemoteViews getRemoteViews(WidgetEntry eventEntry, int position) {
}

private void setDayHeaderTitle(int position, DayHeader entry, RemoteViews rv, ContextThemeWrapper shadingContext) {
String dateString = getTitleString(entry)
.toUpperCase(Locale.getDefault());
String dateString = getTitleString(entry).toString().toUpperCase(Locale.getDefault());
rv.setTextViewText(R.id.day_header_title, dateString);
setTextSize(getSettings(), rv, R.id.day_header_title, R.dimen.day_header_title);
setTextColorFromAttr(shadingContext, rv, R.id.day_header_title, R.attr.dayHeaderTitle);
Expand All @@ -70,14 +68,14 @@ private void setDayHeaderTitle(int position, DayHeader entry, RemoteViews rv, Co
R.dimen.day_header_padding_left, paddingTopId, R.dimen.day_header_padding_right, paddingBottomId);
}

protected String getTitleString(DayHeader entry) {
protected CharSequence getTitleString(DayHeader entry) {
switch (entry.entryPosition) {
case PAST_AND_DUE_HEADER:
return getContext().getString(R.string.past_header);
case END_OF_LIST_HEADER:
return getContext().getString(R.string.end_of_list_header);
default:
return DateUtil.createDayHeaderTitle(getSettings(), entry.entryDate);
return getSettings().dayHeaderDateFormatter().formatDate(entry.entryDate);
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<string name="appearance_show_days_without_events_desc">Show day headers for days without events</string>
<string name="appearance_show_day_headers_title">Show day headers</string>
<string name="appearance_show_day_headers_desc">Show day headers at all</string>
<string name="day_header_date_format">Day header date format</string>
<string name="show_past_events_under_one_header_title">Header for past/due events</string>
<string name="show_past_events_under_one_header_desc">Show all past/due events under one "Past and due" header</string>
<string name="show_event_icon_title">Show event icon</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
android:summary="@string/appearance_show_day_headers_desc"
android:title="@string/appearance_show_day_headers_title" />

<org.andstatus.todoagenda.prefs.dateformat.DateFormatPreference
android:key="dayHeaderDateFormat"
android:defaultValue="defaultWeekday:"
android:title="@string/day_header_date_format"
android:dependency="showDayHeaders" />

<org.andstatus.todoagenda.prefs.MultilineCheckBoxPreference
android:key="showPastEventsUnderOneHeader"
android:layout_width="wrap_content"
Expand Down

0 comments on commit d0a45b9

Please sign in to comment.