Skip to content

Commit

Permalink
plusonelabs#356 In Settings show when a snapshot was taken
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Jan 12, 2020
1 parent 14a3e15 commit 0f4c828
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,26 @@ private void showLockTimeZone(boolean setAlso) {

private void showSnapshotMode() {
Preference preference = findPreference(InstanceSettings.PREF_SNAPSHOT_MODE);
if (preference != null) {
preference.setSummary(ApplicationPreferences.getSnapshotMode(getActivity()).valueResId);
if (preference == null) return;

SnapshotMode snapshotMode = ApplicationPreferences.getSnapshotMode(getActivity());
if (snapshotMode == SnapshotMode.LIVE_DATA) {
preference.setSummary(snapshotMode.valueResId);
} else {
InstanceSettings settings = getSettings();
preference.setSummary(String.format(
getText(snapshotMode.valueResId).toString(),
DateUtil.createDateString(getSettings(), settings.clock().now()) + " " +
DateUtil.formatTime(this::getSettings, settings.clock().now())
));
}
}

private InstanceSettings getSettings() {
int widgetId = ApplicationPreferences.getWidgetId(getActivity());
return AllSettings.instanceFromId(getActivity(), widgetId);
}

private void showRefreshPeriod() {
EditTextPreference preference = (EditTextPreference) findPreference(InstanceSettings.PREF_REFRESH_PERIOD_MINUTES);

Expand Down Expand Up @@ -105,10 +120,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
case InstanceSettings.PREF_SNAPSHOT_MODE:
SnapshotMode snapshotMode = ApplicationPreferences.getSnapshotMode(getActivity());
if (snapshotMode != SnapshotMode.LIVE_DATA) {
int widgetId = ApplicationPreferences.getWidgetId(getActivity());
InstanceSettings settings = AllSettings.instanceFromId(getActivity(), widgetId);
InstanceSettings settings = getSettings();
if (!settings.hasResults()) {
settings.setResultsStorage(QueryResultsStorage.getNewResults(getActivity(), widgetId));
settings.setResultsStorage(QueryResultsStorage.getNewResults(getActivity(), settings.widgetId));
settings.clock().setSnapshotMode(snapshotMode);
settings.save("newResultsForSnapshotMode");
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/org/andstatus/todoagenda/util/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.util.Log;

Expand All @@ -18,9 +19,15 @@
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.function.Supplier;

import static org.andstatus.todoagenda.util.MyClock.isDateDefined;

public class DateUtil {
private static final String COMMA_SPACE = ", ";
private static final String TWELVE = "12";
private static final String AUTO = "auto";
public static final String EMPTY_STRING = "";

public static boolean isMidnight(DateTime date) {
return date.isEqual(date.withTimeAtStartOfDay());
Expand Down Expand Up @@ -70,6 +77,20 @@ private static String formatDateTimeAtTimeZone(InstanceSettings settings, DateTi
timeZoneId).toString();
}

public static String formatTime(Supplier<InstanceSettings> settingsSupplier, @Nullable DateTime time) {
if (!isDateDefined(time)) return EMPTY_STRING;

InstanceSettings settings = settingsSupplier.get();
String dateFormat = settings.getDateFormat();
if (!DateFormat.is24HourFormat(settings.getContext()) && dateFormat.equals(AUTO)
|| dateFormat.equals(TWELVE)) {
return formatDateTime(settings, time,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR);
}
return formatDateTime(settings, time,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR);
}

public static CharSequence getDaysFromTodayString(Context context, int daysFromToday) {
switch (daysFromToday) {
case -1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;

import androidx.annotation.Nullable;

Expand All @@ -18,11 +16,8 @@

public class CalendarEntry extends WidgetEntry<CalendarEntry> {

private static final String TWELVE = "12";
private static final String AUTO = "auto";
private static final String ARROW = "→";
private static final String SPACE = " ";
private static final String EMPTY_STRING = "";
static final String SPACE_DASH_SPACE = " - ";

private boolean allDay;
Expand Down Expand Up @@ -130,18 +125,18 @@ private String createTimeStringForCalendarEntry(Context context) {
startStr = ARROW;
separator = SPACE;
} else {
startStr = createTimeString(context, entryDate);
startStr = DateUtil.formatTime(this::getSettings, entryDate);
}
if (getSettings().getShowEndTime()) {
if (!isDateDefined(event.getEndDate()) || (isPartOfMultiDayEvent() && !isLastEntryOfEvent())) {
endStr = ARROW;
separator = SPACE;
} else {
endStr = createTimeString(context, event.getEndDate());
endStr = DateUtil.formatTime(this::getSettings, event.getEndDate());
}
} else {
separator = EMPTY_STRING;
endStr = EMPTY_STRING;
separator = DateUtil.EMPTY_STRING;
endStr = DateUtil.EMPTY_STRING;
}

if (startStr.equals(endStr)) {
Expand All @@ -151,19 +146,6 @@ private String createTimeStringForCalendarEntry(Context context) {
return startStr + separator + endStr;
}

private String createTimeString(Context context, @Nullable DateTime time) {
if (!isDateDefined(time)) return EMPTY_STRING;

String dateFormat = getSettings().getDateFormat();
if (!DateFormat.is24HourFormat(context) && dateFormat.equals(AUTO)
|| dateFormat.equals(TWELVE)) {
return DateUtil.formatDateTime(getSettings(), time,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR);
}
return DateUtil.formatDateTime(getSettings(), time,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR);
}

public Context getContext() {
return event.getContext();
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@
<!-- Preference header: Other -->
<string name="snapshot_mode">Lock list of events (Snapshot mode)</string>
<string name="snapshot_mode_live_data">Live data from real sources</string>
<string name="snapshot_mode_time">Snapshot as seen at the time, when it was taken</string>
<string name="snapshot_mode_now">Stored snapshot shown at current time</string>
<string name="snapshot_mode_time">Snapshot as seen at the time, when it was taken: %s</string>
<string name="snapshot_mode_now">Stored snapshot shown at current time (taken at %s)</string>
<string name="refresh_period_minutes">Auto refresh period, minutes</string>
<string name="refresh_period_minutes_desc">Automatically refresh the widget every %d minute(s)</string>

Expand Down

0 comments on commit 0f4c828

Please sign in to comment.