Skip to content

Commit

Permalink
Return unique days in expandOrgDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
nevenz committed Mar 21, 2019
1 parent e7873da commit 994467c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,59 @@ public class AgendaUtilsTest {
private String rangeStr;
private int days;
private List<DateTime> dates;
private boolean overdueToday;

// now is 2017 May 5, 13:00:00
private GregorianCalendar now = new GregorianCalendar(2017, Calendar.MAY, 5, 13, 0);

public AgendaUtilsTest(String rangeStr, int days, List<DateTime> dates) {
public AgendaUtilsTest(String rangeStr, int days, boolean overdueToday, List<DateTime> dates) {
this.rangeStr = rangeStr;
this.days = days;
this.overdueToday = overdueToday;
this.dates = dates;
}

@Parameterized.Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "<2017-05-03 Wed>--<2017-05-11 Do>", 2, Arrays.asList(
{ "<2017-05-03 Wed>--<2017-05-11 Do>", 2, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0),
new DateTime(2017, 5, 6, 0, 0))
},
{ "<2017-05-03 Wed>--<2017-05-11 Do>", 1, Arrays.asList(
{ "<2017-05-03 Wed>--<2017-05-11 Do>", 1, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0))
},
{ "<2017-05-06 Sat>--<2017-05-08 Mon>", 10, Arrays.asList(
{ "<2017-05-06 Sat>--<2017-05-08 Mon>", 10, true, Arrays.asList(
new DateTime(2017, 5, 6, 0, 0),
new DateTime(2017, 5, 7, 0, 0),
new DateTime(2017, 5, 8, 0, 0))
},
{ "<2017-05-02 Tue ++3d>", 5, Arrays.asList(
{ "<2017-05-02 Tue ++3d>", 5, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0),
new DateTime(2017, 5, 8, 0, 0))
},
{ "<2017-05-04 Do>", 5, Arrays.asList(
{ "<2017-05-04 Do>", 5, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0))
},
{ "<2017-05-05 Do>", 5, Arrays.asList(
{ "<2017-05-05 Do>", 5, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0))
},
{ "<2017-05-06 Do>", 5, Arrays.asList(
{ "<2017-05-06 Do>", 5, true, Arrays.asList(
new DateTime(2017, 5, 6, 0, 0))
},
{ "<2017-05-03 Wed 09:00 ++12h>", 2, Arrays.asList(
{ "<2017-05-03 Wed 09:00 ++12h>", 2, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0), // overdue
new DateTime(2017, 5, 5, 21, 0),
new DateTime(2017, 5, 6, 9, 0),
new DateTime(2017, 5, 6, 21, 0))
},
{ "<2017-05-05 Fri 09:00 ++12h>", 2, Arrays.asList(
{ "<2017-05-05 Fri 09:00 ++12h>", 2, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0), // overdue
new DateTime(2017, 5, 5, 21, 0),
new DateTime(2017, 5, 6, 9, 0),
new DateTime(2017, 5, 6, 21, 0))
},
{ "<2017-05-07 Sun 09:00 ++6h>", 4, Arrays.asList(
{ "<2017-05-07 Sun 09:00 ++6h>", 4, true, Arrays.asList(
new DateTime(2017, 5, 7, 9, 0),
new DateTime(2017, 5, 7, 15, 0),
new DateTime(2017, 5, 7, 21, 0),
Expand All @@ -78,39 +81,44 @@ public static Iterable<Object[]> data() {
new DateTime(2017, 5, 8, 15, 0),
new DateTime(2017, 5, 8, 21, 0))
},
{ "<2017-05-03 Wed 09:00 .+12h>", 2, Arrays.asList(
{ "<2017-05-03 Wed 09:00 .+12h>", 2, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0), // overdue
new DateTime(2017, 5, 5, 21, 0),
new DateTime(2017, 5, 6, 9, 0),
new DateTime(2017, 5, 6, 21, 0))
},
{ "<2017-05-03 Wed 09:00 +12h>", 3, Arrays.asList(
{ "<2017-05-03 Wed 09:00 +12h>", 3, true, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0), // overdue
new DateTime(2017, 5, 5, 21, 0),
new DateTime(2017, 5, 6, 9, 0),
new DateTime(2017, 5, 6, 21, 0),
new DateTime(2017, 5, 7, 9, 0),
new DateTime(2017, 5, 7, 21, 0))
},
{ "<2017-05-08 Mon 09:00 +12h>", 5, Arrays.asList(
{ "<2017-05-08 Mon 09:00 +12h>", 5, true, Arrays.asList(
new DateTime(2017, 5, 8, 9, 0),
new DateTime(2017, 5, 8, 21, 0),
new DateTime(2017, 5, 9, 9, 0),
new DateTime(2017, 5, 9, 21, 0))
},
{ "<2017-05-06 Sat +1w>", 5, Arrays.asList(
{ "<2017-05-06 Sat +1w>", 5, true, Arrays.asList(
new DateTime(2017, 5, 6, 0, 0))
},
{ "<2017-05-06 Sat +1w>", 10, Arrays.asList(
{ "<2017-05-06 Sat +1w>", 10, true, Arrays.asList(
new DateTime(2017, 5, 6, 0, 0),
new DateTime(2017, 5, 13, 0, 0))
},
{ "<2017-05-04 Sat +1d>", 3, false, Arrays.asList(
new DateTime(2017, 5, 5, 0, 0),
new DateTime(2017, 5, 6, 0, 0),
new DateTime(2017, 5, 7, 0, 0))
}
});
}

@Test
public void testExpander() {
List<DateTime> expandedDates = AgendaUtils.expandOrgDateTime(rangeStr, now, days);
List<DateTime> expandedDates = AgendaUtils.expandOrgDateTime(rangeStr, now, days, overdueToday);
assertEquals(dates.size(), expandedDates.size());
assertThat(toStringArray(expandedDates), is(toStringArray(dates)));
}
Expand Down
17 changes: 7 additions & 10 deletions app/src/main/java/com/orgzly/android/util/AgendaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -42,20 +43,16 @@ public static Set<DateTime> expandOrgDateTime(
}

/** Used by tests. */
static List<DateTime> expandOrgDateTime(String rangeStr, Calendar now, int days) {

OrgRange range = OrgRange.parseOrNull(rangeStr);
if (range == null) {
return new ArrayList<>();
}

return expandOrgDateTime(range, true, new DateTime(now), days);
static List<DateTime> expandOrgDateTime(String rangeStr, Calendar now, int days, boolean overdueToday) {
return expandOrgDateTime(OrgRange.parse(rangeStr), overdueToday, new DateTime(now), days);
}

private static List<DateTime> expandOrgDateTime(
OrgRange range, boolean overdueToday, DateTime now, int days) {

List<DateTime> result = new ArrayList<>();
// Only unique values
Set<DateTime> result = new LinkedHashSet<>();

OrgDateTime rangeStart = range.getStartTime();
OrgDateTime rangeEnd = range.getEndTime();

Expand Down Expand Up @@ -83,7 +80,7 @@ private static List<DateTime> expandOrgDateTime(
result.addAll(OrgDateTimeUtils.getTimesInInterval(rangeStart, now, to, true, 0));
}

return result;
return new ArrayList<>(result);
}

private static OrgDateTime buildOrgDateTimeFromDate(DateTime date, OrgRepeater repeater) {
Expand Down

0 comments on commit 994467c

Please sign in to comment.