Skip to content

Commit

Permalink
plusonelabs#356 Locked Time zone and "set now" merged in related sett…
Browse files Browse the repository at this point in the history
…ings internally to simplify usage
  • Loading branch information
yvolk committed Dec 31, 2019
1 parent 598a157 commit 21bdad8
Show file tree
Hide file tree
Showing 26 changed files with 97 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DateTime dateTime(
int hourOfDay,
int minuteOfHour) {
return new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, 0, 0,
provider.getSettings().getTimeZone());
provider.getSettings().clock().getZone());
}

protected void playResults(String tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public void testIllegalInstantDueToTimeZoneOffsetTransition() {
oneTimeDst("2015-10-25T00:00:00+00:00");
oneTimeDst("2011-03-27T00:00:00+00:00");
oneTimeDst("1980-04-06T00:00:00+00:00");
provider.addRow(new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(),false)
.setStartDate(getSettings().clock().startOfTomorrow(getSettings().getTimeZone()))
provider.addRow(new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), false)
.setStartDate(getSettings().clock().startOfTomorrow(getSettings().clock().getZone()))
.setEventSource(provider.getFirstActiveEventSource())
.setTitle("This will be the only event that will be shown"));
playResults(TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testInsidePeriod() throws IOException, JSONException {
provider.startEditingPreferences();
ApplicationPreferences.setEventRange(provider.getContext(), dateRange);
provider.savePreferences();
DateTime now = new DateTime(2015, 8, 30, 0, 0, 1, 0, getSettings().getTimeZone());
DateTime now = new DateTime(2015, 8, 30, 0, 0, 1, 0, getSettings().clock().getZone());
getSettings().clock().setNow(now);
playResults(method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.andstatus.todoagenda.widget.CalendarEntry;
import org.andstatus.todoagenda.widget.WidgetEntry;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -29,10 +28,8 @@ public class MultidayEventTest extends BaseWidgetTest {
*/
@Test
public void testEventWhichCarryOverToTheNextDay() {
DateTimeZone timeZone = getSettings().getTimeZone();
DateTime today = getSettings().clock().now(timeZone).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
timeZone, false);
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Event that carry over to the next day, show as ending midnight");
Expand Down Expand Up @@ -77,8 +74,7 @@ public void testEventWhichCarryOverToTheNextDay() {
public void testThreeDaysEvent() {
DateTime friday = dateTime(2015, 9, 18);
DateTime sunday = friday.plusDays(2);
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Leader's weekend");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public class OngoingEventTest extends BaseWidgetTest {
*/
@Test
public void testTodaysOngoingEvent() {
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Ongoing event shows original start time");
Expand Down Expand Up @@ -53,9 +52,8 @@ public void testTodaysOngoingEvent() {
*/
@Test
public void testYesterdaysOngoingEvent() {
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Ongoing event, which started yesterday, shows no start time");
Expand All @@ -82,9 +80,8 @@ public void testYesterdaysOngoingEvent() {

@Test
public void testEventWhichCarryOverToTheNextDay() {
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Event that carry over to the next day, show as ending midnight");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int countCalendarEntries() {
void generateEventInstances() {
EnvironmentChangedReceiver.sleep(MIN_MILLIS_BETWEEN_RELOADS);
provider.clear();
DateTime date = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime date = getSettings().clock().now().withTimeAtStartOfDay();
long millis = date.getMillis() + TimeUnit.HOURS.toMillis(10);
eventId++;
for (int ind = 0; ind < 15; ind++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public class SingleEventTest extends BaseWidgetTest {

@Test
public void testEventAttributes() {
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
getSettings().clock().setNow(today.plusHours(10));
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), false);
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), false);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Single Event today with all known attributes");
Expand All @@ -47,10 +46,9 @@ public void testEventAttributes() {

@Test
public void testAlldayEventAttributes() {
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
getSettings().clock().setNow(today.plusHours(10));
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), true);
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), true);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Single AllDay event today with all known attributes");
Expand All @@ -69,8 +67,7 @@ public void testAlldayEventAttributes() {
public void testAlldayEventMillis() {
EnvironmentChangedReceiver.sleep(MIN_MILLIS_BETWEEN_RELOADS);
DateTime today = getSettings().clock().now(DateTimeZone.UTC).withTimeAtStartOfDay();
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(),
getSettings().getTimeZone(), true);
CalendarEvent event = new CalendarEvent(getSettings(), provider.getContext(), provider.getWidgetId(), true);
event.setEventSource(provider.getFirstActiveEventSource());
event.setEventId(++eventId);
event.setTitle("Single All day event from millis");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private MockCalendarContentProvider(Context context) {
"ToDo Agenda " + widgetId + " " + InstanceSettings.TEST_REPLAY_SUFFIX);
settings.setActiveEventSources(settings.getActiveEventSources());

settings.setLockedTimeZoneId(ZONE_IDS[(int)(System.currentTimeMillis() % ZONE_IDS.length)]);
settings.clock().setLockedTimeZoneId(ZONE_IDS[(int)(System.currentTimeMillis() % ZONE_IDS.length)]);
setSettings(settings);
}

Expand All @@ -75,7 +75,7 @@ public void updateAppSettings(String tag) {
}
AllSettings.addNew(context, settings);
if (results.getResults().size() > 0) {
Log.d(tag, "Results executed at " + settings.clock().now(settings.getTimeZone()));
Log.d(tag, "Results executed at " + settings.clock().now());
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ public void addRow(QueryRow queryRow) {
EventProviderType providerType = EventProviderType.CALENDAR;
QueryResult result = results.findLast(providerType).orElseGet( () -> {
QueryResult r2 = new QueryResult(providerType, getSettings().getWidgetId(),
getSettings().clock().now(getSettings().getTimeZone()));
getSettings().clock().now());
results.addResult(r2);
return r2;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.andstatus.todoagenda.BaseWidgetTest;
import org.andstatus.todoagenda.calendar.CalendarEventProvider;
import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.util.PermissionsUtil;
import org.joda.time.DateTime;
import org.json.JSONException;
Expand Down Expand Up @@ -82,7 +81,7 @@ public void testTwoEventsToday() {
private QueryResult addOneResult(String selection) {
QueryResult input = new QueryResult(EventProviderType.CALENDAR, getSettings(),
CalendarContract.Instances.CONTENT_URI, projection, selection, null, sortOrder);
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
input.addRow(new QueryRow().setEventId(++eventId)
.setTitle("First Event today").setBegin(today.plusHours(8).getMillis()));
input.addRow(new QueryRow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void registerReceivers(Map<Integer, InstanceSettings> instances) {
private static void scheduleMidnightAlarms(Context context, Map<Integer, InstanceSettings> instances) {
Set<DateTime> alarmTimes = new HashSet<>();
for (InstanceSettings settings : instances.values()) {
alarmTimes.add(settings.clock().now(settings.getTimeZone()).withTimeAtStartOfDay().plusDays(1));
alarmTimes.add(settings.clock().now().withTimeAtStartOfDay().plusDays(1));
}
int counter = 0;
for (DateTime alarmTime : alarmTimes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public RemoteViewsFactory(Context context, int widgetId) {
this.context = context;
this.widgetId = widgetId;
visualizers.add(new LastEntryVisualizer(context, widgetId));
widgetEntries.add(new LastEntry(getSettings(), NOT_LOADED, getSettings().clock().now(getSettings().getTimeZone())));
widgetEntries.add(new LastEntry(getSettings(), NOT_LOADED, getSettings().clock().now()));
logEvent("Init");
}

Expand Down Expand Up @@ -220,7 +220,7 @@ private List<WidgetEntry> addDayHeaders(List<WidgetEntry> listIn) {
List<WidgetEntry> listOut = new ArrayList<>();
if (!listIn.isEmpty()) {
InstanceSettings settings = getSettings();
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
DayHeader curDayBucket = new DayHeader(settings, MyClock.DATETIME_MIN);
boolean pastEventsHeaderAdded = false;
for (WidgetEntry entry : listIn) {
Expand Down Expand Up @@ -256,7 +256,7 @@ List<? extends WidgetEntry> getWidgetEntries() {

private void addEmptyDayHeadersBetweenTwoDays(List<WidgetEntry> entries, DateTime fromDayExclusive, DateTime toDayExclusive) {
DateTime emptyDay = fromDayExclusive.plusDays(1);
DateTime today = getSettings().clock().now(getSettings().getTimeZone()).withTimeAtStartOfDay();
DateTime today = getSettings().clock().now().withTimeAtStartOfDay();
if (emptyDay.isBefore(today)) {
emptyDay = today;
}
Expand Down Expand Up @@ -320,7 +320,7 @@ private static void configureCurrentDate(InstanceSettings settings, RemoteViews
int viewId = R.id.calendar_current_date;
rv.setOnClickPendingIntent(viewId, createOpenCalendarPendingIntent(settings));
String formattedDate = settings.getShowDateOnWidgetHeader()
? DateUtil.createDateString(settings, settings.clock().now(settings.getTimeZone())).toUpperCase(Locale.getDefault())
? DateUtil.createDateString(settings, settings.clock().now()).toUpperCase(Locale.getDefault())
: " ";
rv.setTextViewText(viewId, formattedDate);
setTextSize(settings, rv, viewId, R.dimen.widget_header_title);
Expand Down Expand Up @@ -390,7 +390,7 @@ private void configureGotoToday(InstanceSettings settings, RemoteViews rv, int t
public static PendingIntent getPermittedAddEventPendingIntent(InstanceSettings settings) {
Context context = settings.getContext();
Intent intent = PermissionsUtil.getPermittedActivityIntent(context,
CalendarIntentUtil.createNewEventIntent(settings.getTimeZone()));
CalendarIntentUtil.createNewEventIntent(settings.clock().getZone()));
return isIntentAvailable(context, intent) ?
PendingIntent.getActivity(context, REQUEST_CODE_ADD_EVENT, intent, PendingIntent.FLAG_UPDATE_CURRENT) :
getEmptyPendingIntent(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.andstatus.todoagenda.prefs.AllSettings;
import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.OrderedEventSource;
import org.andstatus.todoagenda.util.DateUtil;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
Expand All @@ -19,7 +18,6 @@ public class CalendarEvent {
private final InstanceSettings settings;
private final Context context;
private final int widgetId;
private final DateTimeZone zone;
private final boolean allDay;

private OrderedEventSource eventSource;
Expand All @@ -33,11 +31,10 @@ public class CalendarEvent {
private boolean alarmActive;
private boolean recurring;

public CalendarEvent(InstanceSettings settings, Context context, int widgetId, DateTimeZone zone, boolean allDay) {
public CalendarEvent(InstanceSettings settings, Context context, int widgetId, boolean allDay) {
this.settings = settings;
this.context = context;
this.widgetId = widgetId;
this.zone = zone;
this.allDay = allDay;
}

Expand Down Expand Up @@ -70,7 +67,7 @@ public long getStartMillis() {
}

private DateTime dateFromMillis(long millis) {
return allDay ? fromAllDayMillis(millis) : new DateTime(millis, zone);
return allDay ? fromAllDayMillis(millis) : new DateTime(millis, getSettings().clock().getZone());
}

private static volatile long fixTimeOfAllDayEventLoggedAt = 0;
Expand All @@ -88,11 +85,11 @@ private DateTime fromAllDayMillis(long millis) {
.withDayOfMonth(utcDate.getDayOfMonth())
.withMillisOfDay(0);
int hour = 0;
while (zone.isLocalDateTimeGap(ldt)) {
while (getSettings().clock().getZone().isLocalDateTimeGap(ldt)) {
Log.v("fixTimeOfAllDayEvent", "Local Date Time Gap: " + ldt + "; " + msgLog);
ldt = ldt.withHourOfDay(++hour);
}
fixed = ldt.toDateTime(zone);
fixed = ldt.toDateTime(getSettings().clock().getZone());
msgLog += " -> " + fixed;
if (Math.abs(System.currentTimeMillis() - fixTimeOfAllDayEventLoggedAt) > 1000) {
fixTimeOfAllDayEventLoggedAt = System.currentTimeMillis();
Expand Down Expand Up @@ -237,7 +234,7 @@ public int hashCode() {
}

public boolean isActive() {
DateTime now = settings.clock().now(zone);
DateTime now = settings.clock().now();
return startDate.isBefore(now) && endDate.isAfter(now);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ private void filterShowOnlyClosestInstanceOfRecurringEvent(@NonNull List<Calenda
if (otherEvent == null) {
eventIds.put(event.getEventId(), event);
} else if (Math.abs(event.getStartDate().getMillis() -
getSettings().clock().now(zone).getMillis()) <
getSettings().clock().now().getMillis()) <
Math.abs(otherEvent.getStartDate().getMillis() -
getSettings().clock().now(zone).getMillis())) {
getSettings().clock().now().getMillis())) {
toDelete.add(otherEvent);
eventIds.put(event.getEventId(), event);
} else {
Expand Down Expand Up @@ -175,7 +175,7 @@ public static String[] getProjection() {
private List<CalendarEvent> getPastEventsWithColorList() {
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, 0);
ContentUris.appendId(builder, getSettings().clock().now(zone).getMillis());
ContentUris.appendId(builder, getSettings().clock().now().getMillis());
List<CalendarEvent> eventList = queryList(builder.build(), getPastEventsWithColorSelection());
for (CalendarEvent event : eventList) {
event.setDefaultCalendarColor();
Expand Down Expand Up @@ -204,7 +204,7 @@ private CalendarEvent createCalendarEvent(Cursor cursor) {
.getActiveEventSource(type, cursor.getInt(cursor.getColumnIndex(Instances.CALENDAR_ID)));

boolean allDay = cursor.getInt(cursor.getColumnIndex(Instances.ALL_DAY)) > 0;
CalendarEvent event = new CalendarEvent(getSettings(), context, widgetId, zone, allDay);
CalendarEvent event = new CalendarEvent(getSettings(), context, widgetId, allDay);
event.setEventSource(source);
event.setEventId(cursor.getInt(cursor.getColumnIndex(Instances.EVENT_ID)));
event.setTitle(cursor.getString(cursor.getColumnIndex(Instances.TITLE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void fromInstanceSettings(Context context, Integer widgetId) {
setBoolean(context, PREF_SHOW_LOCATION, settings.getShowLocation());
setString(context, PREF_DATE_FORMAT, settings.getDateFormat());
setAbbreviateDates(context, settings.getAbbreviateDates());
setLockedTimeZoneId(context, settings.getLockedTimeZoneId());
setLockedTimeZoneId(context, settings.clock().getLockedTimeZoneId());
setRefreshPeriodMinutes(context, settings.getRefreshPeriodMinutes());
setString(context, PREF_EVENT_ENTRY_LAYOUT, settings.getEventEntryLayout().value);
setBoolean(context, PREF_MULTILINE_TITLE, settings.isMultilineTitle());
Expand Down
Loading

0 comments on commit 21bdad8

Please sign in to comment.