Skip to content

Commit

Permalink
plusonelabs#356 Snapshot mode setting moved to MyClock
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Dec 31, 2019
1 parent 21bdad8 commit c2514dc
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testBirthdayOneDayOnly() throws IOException, JSONException {
private void playAtOneTime(QueryResultsStorage inputs, DateTime now, int entriesWithoutLastExpected) {
provider.clear();
provider.addResults(inputs.getResults());
getSettings().clock().setNow(now);
getSettings().clock().setSnapshotDate(now);
EnvironmentChangedReceiver.sleep(MIN_MILLIS_BETWEEN_RELOADS);
playResults(TAG);
assertEquals(entriesWithoutLastExpected == 0 ? 0 : entriesWithoutLastExpected + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testInsidePeriod() throws IOException, JSONException {
ApplicationPreferences.setEventRange(provider.getContext(), dateRange);
provider.savePreferences();
DateTime now = new DateTime(2015, 8, 30, 0, 0, 1, 0, getSettings().clock().getZone());
getSettings().clock().setNow(now);
getSettings().clock().setSnapshotDate(now);
playResults(method);

DateTime today = now.withTimeAtStartOfDay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testEventWhichCarryOverToTheNextDay() {
event.setStartDate(today.plusHours(19));
event.setEndDate(today.plusDays(1).plusHours(7));

getSettings().clock().setNow(today.plusHours(10).plusMinutes(33));
getSettings().clock().setSnapshotDate(today.plusHours(10).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry1 = null;
Expand Down Expand Up @@ -97,7 +97,7 @@ private void assertSundayEntryAt(CalendarEvent event, DateTime sunday, DateTime
}

private CalendarEntry getSundayEntryAt(CalendarEvent event, DateTime currentDateTime) {
getSettings().clock().setNow(currentDateTime);
getSettings().clock().setSnapshotDate(currentDateTime);
provider.clear();
provider.addRow(event);
Log.i(TAG, "getSundayEntryAt " + currentDateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testTodaysOngoingEvent() {
event.setStartDate(today.plusHours(9));
event.setEndDate(today.plusHours(12));

getSettings().clock().setNow(today.plusHours(10).plusMinutes(33));
getSettings().clock().setSnapshotDate(today.plusHours(10).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry = null;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void testYesterdaysOngoingEvent() {
event.setStartDate(today.minusDays(1).plusHours(9));
event.setEndDate(today.plusHours(12));

getSettings().clock().setNow(today.plusHours(10).plusMinutes(33));
getSettings().clock().setSnapshotDate(today.plusHours(10).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry = null;
Expand Down Expand Up @@ -88,7 +88,7 @@ public void testEventWhichCarryOverToTheNextDay() {
event.setStartDate(today.plusHours(19));
event.setEndDate(today.plusDays(1).plusHours(7));

getSettings().clock().setNow(today.plusHours(20).plusMinutes(33));
getSettings().clock().setSnapshotDate(today.plusHours(20).plusMinutes(33));
provider.addRow(event);
playResults(TAG);
CalendarEntry entry = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SingleEventTest extends BaseWidgetTest {
@Test
public void testEventAttributes() {
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
getSettings().clock().setNow(today.plusHours(10));
getSettings().clock().setSnapshotDate(today.plusHours(10));
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
Expand All @@ -47,7 +47,7 @@ public void testEventAttributes() {
@Test
public void testAlldayEventAttributes() {
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
getSettings().clock().setNow(today.plusHours(10));
getSettings().clock().setSnapshotDate(today.plusHours(10));
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), true);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void setSettings(InstanceSettings settings) {
public void updateAppSettings(String tag) {
if (!results.getResults().isEmpty()) {
settings.setResultsStorage(results);
settings.setSnapshotMode(SnapshotMode.SNAPSHOT_TIME);
settings.clock().setSnapshotMode(SnapshotMode.SNAPSHOT_TIME);
}
AllSettings.addNew(context, settings);
if (results.getResults().size() > 0) {
Expand Down Expand Up @@ -164,7 +164,7 @@ public QueryResultsStorage loadResultsAndSettings(@RawRes int jsonResId)
QueryResultsStorage results = QueryResultsStorage.fromJson(widgetId, json);
if (!results.getResults().isEmpty()) {
DateTime now = results.getResults().get(0).getExecutedAt();
settings.clock().setNow(now);
settings.clock().setSnapshotDate(now);
}

setSettings(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public class InstanceSettings {
private volatile MyClock clock = new MyClock();

static final String PREF_SNAPSHOT_MODE = "snapshotMode";
private SnapshotMode snapshotMode = SnapshotMode.defaultValue;
public final static String PREF_REFRESH_PERIOD_MINUTES = "refreshPeriodMinutes";
public final static int PREF_REFRESH_PERIOD_MINUTES_DEFAULT = 10;
private int refreshPeriodMinutes = PREF_REFRESH_PERIOD_MINUTES_DEFAULT;
Expand Down Expand Up @@ -253,7 +252,7 @@ private InstanceSettings setFromJson(JSONObject json) {
clock().setLockedTimeZoneId(json.getString(PREF_LOCKED_TIME_ZONE_ID));
}
if (json.has(PREF_SNAPSHOT_MODE)) {
setSnapshotMode(SnapshotMode.fromValue(json.getString(PREF_SNAPSHOT_MODE)));
clock().setSnapshotMode(SnapshotMode.fromValue(json.getString(PREF_SNAPSHOT_MODE)));
}
if (json.has(PREF_REFRESH_PERIOD_MINUTES)) {
setRefreshPeriodMinutes(json.getInt(PREF_REFRESH_PERIOD_MINUTES));
Expand Down Expand Up @@ -340,8 +339,6 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId
settings.showLocation = ApplicationPreferences.getShowLocation(context);
settings.dateFormat = ApplicationPreferences.getDateFormat(context);
settings.abbreviateDates = ApplicationPreferences.getAbbreviateDates(context);
settings.clock().setLockedTimeZoneId(ApplicationPreferences.getLockedTimeZoneId(context));
settings.setSnapshotMode(ApplicationPreferences.getSnapshotMode(context));
settings.setRefreshPeriodMinutes(ApplicationPreferences.getRefreshPeriodMinutes(context));
settings.eventEntryLayout = ApplicationPreferences.getEventEntryLayout(context);
settings.multilineTitle = ApplicationPreferences.isMultilineTitle(context);
Expand All @@ -365,6 +362,8 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId
settings.dayHeaderAlignment = ApplicationPreferences.getString(context, PREF_DAY_HEADER_ALIGNMENT,
PREF_DAY_HEADER_ALIGNMENT_DEFAULT);

settings.clock().setLockedTimeZoneId(ApplicationPreferences.getLockedTimeZoneId(context));
settings.clock().setSnapshotMode(ApplicationPreferences.getSnapshotMode(context));
if (settingsStored != null) {
settings.setResultsStorage(settingsStored.getResultsStorage());
settings.clock.setFromPrevious(settingsStored.clock);
Expand Down Expand Up @@ -428,7 +427,7 @@ public JSONObject toJson() {
json.put(PREF_DATE_FORMAT, dateFormat);
json.put(PREF_ABBREVIATE_DATES, abbreviateDates);
json.put(PREF_LOCKED_TIME_ZONE_ID, clock().getLockedTimeZoneId());
json.put(PREF_SNAPSHOT_MODE, snapshotMode.value);
json.put(PREF_SNAPSHOT_MODE, clock().getSnapshotMode().value);
json.put(PREF_REFRESH_PERIOD_MINUTES, refreshPeriodMinutes);
json.put(PREF_EVENT_ENTRY_LAYOUT, eventEntryLayout.value);
json.put(PREF_MULTILINE_TITLE, multilineTitle);
Expand Down Expand Up @@ -562,16 +561,8 @@ public MyClock clock() {
return clock;
}

public void setSnapshotMode(SnapshotMode snapshotMode) {
this.snapshotMode = snapshotMode;
}

public SnapshotMode getSnapshotMode() {
return resultsStorage == null ? SnapshotMode.LIVE_DATA : snapshotMode;
}

public boolean isLiveMode() {
return getSnapshotMode() == SnapshotMode.LIVE_DATA;
return clock().getSnapshotMode() == SnapshotMode.LIVE_DATA;
}

public void setRefreshPeriodMinutes(int refreshPeriodMinutes) {
Expand Down Expand Up @@ -731,6 +722,9 @@ public void setResultsStorage(QueryResultsStorage resultsStorage) {
addActiveEventSource(providerType);
}
}
if (!resultsStorage.getResults().isEmpty() && !clock().isSnapshotDateSet()) {
clock().setSnapshotDate(resultsStorage.getResults().get(0).getExecutedAt());
}
}
}

Expand Down
53 changes: 35 additions & 18 deletions app/src/main/java/org/andstatus/todoagenda/util/MyClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import androidx.annotation.Nullable;

import org.andstatus.todoagenda.prefs.SnapshotMode;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

Expand All @@ -13,10 +14,11 @@ public class MyClock {
public static final DateTime DATETIME_MIN = new DateTime(0, DateTimeZone.UTC);
public static final DateTime DATETIME_MAX = new DateTime(Long.MAX_VALUE, DateTimeZone.UTC);

private volatile SnapshotMode snapshotMode = SnapshotMode.defaultValue;
private volatile DateTime snapshotDate = null;
private volatile DateTime snapshotDateSetAt = null;
private volatile String lockedTimeZoneId = "";
private volatile DateTimeZone zone;
private volatile DateTime mNow = null;
private volatile DateTime mNowSetAt = DateTime.now();

public MyClock() {
zone = DateTimeZone.getDefault();
Expand All @@ -41,23 +43,35 @@ public boolean isTimeZoneLocked() {
return !StringUtil.isEmpty(lockedTimeZoneId);
}

public void setSnapshotMode(SnapshotMode snapshotMode) {
this.snapshotMode = snapshotMode;
}

public void setSnapshotDate(DateTime snapshotDate) {
this.snapshotDate = snapshotDate;
snapshotDateSetAt = DateTime.now();
zone = snapshotDate == null
? getLockedOrDefaultZone()
: snapshotDate.getZone();
}

public boolean isSnapshotDateSet() {
return snapshotDate != null;
}

public SnapshotMode getSnapshotMode() {
return snapshotDate == null ? SnapshotMode.LIVE_DATA : snapshotMode;
}

public void setFromPrevious(MyClock prevClock) {
if (prevClock.isTimeZoneLocked()) {
setLockedTimeZoneId(prevClock.lockedTimeZoneId);
}
if (prevClock.mNow != null) {
setNow(prevClock.now());
if (prevClock.snapshotDate != null) {
setSnapshotDate(prevClock.snapshotDate);
}
}

public void setNow(DateTime now) {
mNowSetAt = DateTime.now();
mNow = now;
zone = now == null
? getLockedOrDefaultZone()
: now.getZone();
}

/**
* Usually returns real "now", but may be #setNow to some other time for testing purposes
*/
Expand All @@ -66,12 +80,15 @@ public DateTime now() {
}

public DateTime now(DateTimeZone zone) {
DateTime nowSetAt;
DateTime now;
do {
nowSetAt = mNowSetAt;
now = mNow;
} while (nowSetAt != mNowSetAt); // Ensure concurrent consistency
DateTime nowSetAt = null;
DateTime now = null;
if (getSnapshotMode() == SnapshotMode.SNAPSHOT_TIME) {
do {
nowSetAt = snapshotDateSetAt;
now = snapshotDate;
} while (nowSetAt != snapshotDateSetAt); // Ensure concurrent consistency
}

if (now == null) {
return DateTime.now(zone);
} else {
Expand Down

0 comments on commit c2514dc

Please sign in to comment.