Skip to content

Commit

Permalink
plusonelabs#356 Fix TimeSection for today's events (no "tomorrows" in…
Browse files Browse the repository at this point in the history
… Today)
  • Loading branch information
yvolk committed Jan 19, 2020
1 parent 445bcac commit 8253250
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public void testEventWhichCarryOverToTheNextDay() {
assertTrue("Is start of Multi Day Event", entry1.isStartOfMultiDayEvent());
assertFalse("Is not an end of Multi Day Event", entry1.isEndOfMultiDayEvent());
assertEquals("Start Time didn't change for today's event", event.getStartDate(), entry1.entryDate);
assertEquals("Entry end time should be the same as Event end time", event.getEndDate(), entry1.getEndDate());
assertEquals("Entry end time should be the same as Event end time", event.getEndDate(), entry1.endDate);

assertNotNull(entry2);
assertFalse("Is not active event", entry2.getEvent().isActive());
assertTrue("Is Part of Multi Day Event", entry2.isPartOfMultiDayEvent());
assertFalse("Is not start of Multi Day Event", entry2.isStartOfMultiDayEvent());
assertTrue("Is end of Multi Day Event", entry2.isEndOfMultiDayEvent());
assertEquals("Start Time of tomorrow's entry is midnight", today.plusDays(1), entry2.entryDate);
assertEquals("Tomorrow event entry end time is the same as for the event", entry2.getEvent().getEndDate(), entry2.getEndDate());
assertEquals("Tomorrow event entry end time is the same as for the event", entry2.getEvent().getEndDate(), entry2.endDate);
}

/**
Expand All @@ -89,7 +89,7 @@ public void testThreeDaysEvent() {
private void assertSundayEntryAt(CalendarEvent event, DateTime sunday, DateTime currentDateTime) {
CalendarEntry entry1 = getSundayEntryAt(event, currentDateTime);
assertEquals(sunday, entry1.entryDate);
assertEquals(event.getEndDate(), entry1.getEndDate());
assertEquals(event.getEndDate(), entry1.endDate);
assertEquals(event.getTitle(), entry1.getTitle());
String timeString = entry1.getEventTimeString();
assertTrue(timeString, timeString.contains(ARROW));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testTodaysOngoingEvent() {
assertTrue("Is active event", entry.getEvent().isActive());
assertFalse("Is not part of Multi Day Event", entry.isPartOfMultiDayEvent());
assertEquals("Start Time didn't change for today's event", event.getStartDate(), entry.entryDate);
assertEquals("End Time didn't change for today's event", event.getEndDate(), entry.getEndDate());
assertEquals("End Time didn't change for today's event", event.getEndDate(), entry.endDate);
}

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ public void testYesterdaysOngoingEvent() {
assertFalse("Is not start of Multi Day Event", entry.isStartOfMultiDayEvent());
assertTrue("Is end of Multi Day Event", entry.isEndOfMultiDayEvent());
assertEquals("Yesterday's event entry start time is midnight", today, entry.entryDate);
assertEquals("End Time didn't change for yesterday's event", event.getEndDate(), entry.getEndDate());
assertEquals("End Time didn't change for yesterday's event", event.getEndDate(), entry.endDate);
}

@Test
Expand Down Expand Up @@ -104,7 +104,7 @@ public void testEventWhichCarryOverToTheNextDay() {
assertTrue("Is start of Multi Day Event", entry.isStartOfMultiDayEvent());
assertFalse("Is not an end of Multi Day Event", entry.isEndOfMultiDayEvent());
assertEquals("Start Time didn't change for today's event", event.getStartDate(), entry.entryDate);
assertEquals("Entry end time is the same as Event end time", event.getEndDate(), entry.getEndDate());
assertEquals("Entry end time is the same as Event end time", event.getEndDate(), entry.endDate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ private List<WidgetEntryVisualizer<? extends WidgetEntry>> getVisualizers() {

private int getTodaysPosition() {
for (int ind = 0; ind < getWidgetEntries().size() - 1; ind++) {
if (getWidgetEntries().get(ind).getTimeSection() != TimeSection.PAST) return ind;
if (getWidgetEntries().get(ind).timeSection != TimeSection.PAST) return ind;
}
return getWidgetEntries().size() - 1;
}

private int getTomorrowsPosition() {
for (int ind = 0; ind < getWidgetEntries().size() - 1; ind++) {
if (getWidgetEntries().get(ind).getTimeSection() == TimeSection.FUTURE) return ind;
if (getWidgetEntries().get(ind).timeSection == TimeSection.FUTURE) return ind;
}
return getWidgetEntries().size() > 0 ? 0 : -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ public TextShading getShading(TextShadingPref pref) {
}

public int getEntryBackgroundColor(WidgetEntry<?> entry) {
return entry.getTimeSection()
return entry.timeSection
.select(getPastEventsBackgroundColor(), getTodaysEventsBackgroundColor(), getEventsBackgroundColor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public enum TextShadingPref {
}

public static TextShadingPref forDayHeader(WidgetEntry<?> entry) {
return entry.getTimeSection().select(DAY_HEADER_PAST, DAY_HEADER_TODAY, DAY_HEADER_FUTURE);
return entry.timeSection.select(DAY_HEADER_PAST, DAY_HEADER_TODAY, DAY_HEADER_FUTURE);
}

public static TextShadingPref forDetails(WidgetEntry<?> entry) {
return entry.getTimeSection().select(DAY_HEADER_PAST, DAY_HEADER_TODAY, DAY_HEADER_FUTURE);
return entry.timeSection.select(DAY_HEADER_PAST, DAY_HEADER_TODAY, DAY_HEADER_FUTURE);
}

public static TextShadingPref forTitle(WidgetEntry<?> entry) {
return entry.getTimeSection().select(ENTRY_PAST, ENTRY_TODAY, ENTRY_FUTURE);
return entry.timeSection.select(ENTRY_PAST, ENTRY_TODAY, ENTRY_FUTURE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.calendar.CalendarEvent;
import org.andstatus.todoagenda.prefs.InstanceSettings;
Expand All @@ -31,13 +29,7 @@ public static CalendarEntry fromEvent(InstanceSettings settings, CalendarEvent e
}

private CalendarEntry(InstanceSettings settings, DateTime entryDate, DateTime endDate) {
super(settings, WidgetEntry.getEntryPosition(settings, entryDate, endDate), entryDate);
}

@Nullable
@Override
public DateTime getEndDate() {
return event.getEndDate();
super(settings, WidgetEntry.getEntryPosition(settings, entryDate, endDate), entryDate, endDate);
}

@Override
Expand Down Expand Up @@ -162,8 +154,7 @@ public OrderedEventSource getSource() {
@Override
public String toString() {
return super.toString() + " CalendarEntry ["
+ "endDate=" + getEndDate()
+ ", allDay=" + allDay
+ "allDay=" + allDay
+ ", time=" + getEventTimeString()
+ ", location=" + getLocationString()
+ ", event=" + event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
public class DayHeader extends WidgetEntry<DayHeader> {

public DayHeader(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime date) {
super(settings, entryPosition, date);
super(settings, entryPosition, date, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum LastEntryType {
public final LastEntryType type;

public LastEntry(InstanceSettings settings, LastEntryType type, DateTime date) {
super(settings, LIST_FOOTER, date);
super(settings, LIST_FOOTER, date, null);
this.type = type;
}
}
14 changes: 3 additions & 11 deletions app/src/main/java/org/andstatus/todoagenda/widget/TaskEntry.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.andstatus.todoagenda.widget;

import androidx.annotation.Nullable;

import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.OrderedEventSource;
import org.andstatus.todoagenda.prefs.TaskScheduling;
Expand All @@ -19,13 +17,13 @@ public class TaskEntry extends WidgetEntry<TaskEntry> {

public static TaskEntry fromEvent(InstanceSettings settings, TaskEvent event) {
WidgetEntryPosition entryPosition = getEntryPosition(settings, event);
TaskEntry entry = new TaskEntry(settings, entryPosition, getEntryDate(settings, entryPosition, event));
TaskEntry entry = new TaskEntry(settings, entryPosition, getEntryDate(settings, entryPosition, event), event.getDueDate());
entry.event = event;
return entry;
}

private TaskEntry(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime entryDate) {
super(settings, entryPosition, entryDate);
private TaskEntry(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime entryDate, DateTime endDate) {
super(settings, entryPosition, entryDate, endDate);
}

/** See https://github.com/plusonelabs/calendar-widget/issues/356#issuecomment-559910887 **/
Expand Down Expand Up @@ -98,12 +96,6 @@ private static DateTime getEntryDateOrElse(InstanceSettings settings, TaskEvent
}
}

@Nullable
@Override
public DateTime getEndDate() {
return event.getDueDate();
}

@Override
public OrderedEventSource getSource() {
return event.getEventSource();
Expand Down
111 changes: 55 additions & 56 deletions app/src/main/java/org/andstatus/todoagenda/widget/WidgetEntry.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.andstatus.todoagenda.widget;

import androidx.annotation.Nullable;

import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.OrderedEventSource;
import org.andstatus.todoagenda.util.DateUtil;
Expand All @@ -10,35 +8,30 @@
import org.joda.time.Days;

import static org.andstatus.todoagenda.util.DateUtil.isSameDate;
import static org.andstatus.todoagenda.widget.WidgetEntryPosition.DAY_HEADER;
import static org.andstatus.todoagenda.widget.WidgetEntryPosition.END_OF_LIST;
import static org.andstatus.todoagenda.widget.WidgetEntryPosition.ENTRY_DATE;
import static org.andstatus.todoagenda.widget.WidgetEntryPosition.PAST_AND_DUE;

public abstract class WidgetEntry<T extends WidgetEntry<T>> implements Comparable<WidgetEntry<T>> {

protected final InstanceSettings settings;
public final WidgetEntryPosition entryPosition;
public final DateTime entryDate;
protected final InstanceSettings settings;
public final DateTime entryDay;
public final DateTime endDate;
public final TimeSection timeSection;

protected WidgetEntry(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime entryDate) {
protected WidgetEntry(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime entryDate, DateTime endDate) {
this.settings = settings;
this.entryPosition = entryPosition;
this.endDate = endDate;
this.entryDate = fixEntryDate(entryPosition, entryDate);
entryDay = calcEntryDay(settings, entryPosition, this.entryDate);
timeSection = calcTimeSection(settings, entryPosition, entryDay, endDate);
}

private DateTime calcEntryDay(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime entryDate) {
switch (entryPosition) {
case START_OF_TODAY:
case END_OF_TODAY:
return settings.clock().now().withTimeAtStartOfDay();
default:
return entryDate.withTimeAtStartOfDay();
}
}

private DateTime fixEntryDate(WidgetEntryPosition entryPosition, DateTime entryDate) {
private static DateTime fixEntryDate(WidgetEntryPosition entryPosition, DateTime entryDate) {
switch (entryPosition) {
case ENTRY_DATE:
throwIfNull(entryPosition, entryDate);
Expand Down Expand Up @@ -66,16 +59,56 @@ private DateTime fixEntryDate(WidgetEntryPosition entryPosition, DateTime entryD
}
}

private static DateTime calcEntryDay(InstanceSettings settings, WidgetEntryPosition entryPosition, DateTime entryDate) {
switch (entryPosition) {
case START_OF_TODAY:
case END_OF_TODAY:
return settings.clock().now().withTimeAtStartOfDay();
default:
return entryDate.withTimeAtStartOfDay();
}
}

private static TimeSection calcTimeSection(InstanceSettings settings, WidgetEntryPosition entryPosition,
DateTime entryDay, DateTime endDate) {
switch (entryPosition) {
case PAST_AND_DUE_HEADER:
return TimeSection.PAST;
case START_OF_TODAY:
return TimeSection.TODAY;
case END_OF_TODAY:
case END_OF_LIST_HEADER:
case END_OF_LIST:
case LIST_FOOTER:
return TimeSection.FUTURE;
default:
break;
}
if (settings.clock().isToday(entryDay)) {
if (entryPosition == DAY_HEADER) return TimeSection.TODAY;

if (settings.clock().isToday(endDate)) {
return settings.clock().isBeforeNow(endDate)
? TimeSection.PAST
: TimeSection.TODAY;
}
return TimeSection.TODAY;
}
return settings.clock().isBeforeToday(entryDay)
? TimeSection.PAST
: (settings.clock().isToday(endDate) ? TimeSection.TODAY : TimeSection.FUTURE);
}

private static void throwIfNull(WidgetEntryPosition entryPosition, DateTime entryDate) {
if (entryDate == null) {
throw new IllegalArgumentException("Invalid entry date: " + entryDate + " at position " + entryPosition);
}
}

public boolean isLastEntryOfEvent() {
return getEndDate() == null ||
return endDate == null ||
!entryPosition.entryDateIsRequired ||
getEndDate().isBefore(MyClock.startOfNextDay(this.entryDate));
endDate.isBefore(MyClock.startOfNextDay(this.entryDate));
}

public static WidgetEntryPosition getEntryPosition(InstanceSettings settings, DateTime mainDate, DateTime otherDate) {
Expand All @@ -89,11 +122,6 @@ public static WidgetEntryPosition getEntryPosition(InstanceSettings settings, Da
return ENTRY_DATE;
}

@Nullable
public DateTime getEndDate() {
return null;
}

public OrderedEventSource getSource() {
return OrderedEventSource.EMPTY;
}
Expand Down Expand Up @@ -139,50 +167,21 @@ public int compareTo(WidgetEntry other) {
: sourceSignum;
}

public TimeSection getTimeSection() {
switch (entryPosition) {
case PAST_AND_DUE_HEADER:
return TimeSection.PAST;
case START_OF_TODAY:
return TimeSection.TODAY;
case END_OF_TODAY:
case END_OF_LIST_HEADER:
case END_OF_LIST:
case LIST_FOOTER:
return TimeSection.FUTURE;
default:
break;
}
if (settings.clock().isToday(entryDate)) {
switch (entryPosition) {
case DAY_HEADER:
return TimeSection.TODAY;
default:
if (settings.clock().isToday(getEndDate())) {
return settings.clock().isBeforeNow(getEndDate())
? TimeSection.PAST
: TimeSection.TODAY;
}
}
}
return settings.clock().isBeforeToday(entryDate)
? TimeSection.PAST
: (settings.clock().isToday(getEndDate()) ? TimeSection.TODAY : TimeSection.FUTURE);
}

public boolean duplicates(WidgetEntry other) {
return entryPosition == other.entryPosition &&
entryDate.equals(other.entryDate) &&
isSameDate(getEndDate(), other.getEndDate()) &&
isSameDate(endDate, other.endDate) &&
getTitle().equals(other.getTitle()) &&
getLocation().equals(other.getLocation());
}

@Override
public String toString() {
return entryPosition.value + " [entryDate=" +
return entryPosition.value + " [" +
"entryDate=" +
(entryDate == MyClock.DATETIME_MIN ? "min" :
(entryDate == MyClock.DATETIME_MAX) ? "max" : entryDate) +
"]";
", endDate=" + endDate +
"]";
}
}

0 comments on commit 8253250

Please sign in to comment.