From 994467cca20b10895e6a02f6663666ed36ac6b50 Mon Sep 17 00:00:00 2001 From: Neven Date: Thu, 21 Mar 2019 17:00:52 +0100 Subject: [PATCH] Return unique days in expandOrgDateTime --- .../orgzly/android/util/AgendaUtilsTest.java | 42 +++++++++++-------- .../com/orgzly/android/util/AgendaUtils.java | 17 ++++---- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/src/androidTest/java/com/orgzly/android/util/AgendaUtilsTest.java b/app/src/androidTest/java/com/orgzly/android/util/AgendaUtilsTest.java index fa628f5f6..96fd404ca 100644 --- a/app/src/androidTest/java/com/orgzly/android/util/AgendaUtilsTest.java +++ b/app/src/androidTest/java/com/orgzly/android/util/AgendaUtilsTest.java @@ -20,56 +20,59 @@ public class AgendaUtilsTest { private String rangeStr; private int days; private List 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 dates) { + public AgendaUtilsTest(String rangeStr, int days, boolean overdueToday, List dates) { this.rangeStr = rangeStr; this.days = days; + this.overdueToday = overdueToday; this.dates = dates; } @Parameterized.Parameters public static Iterable 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), @@ -78,13 +81,13 @@ public static Iterable 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), @@ -92,25 +95,30 @@ public static Iterable data() { 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 expandedDates = AgendaUtils.expandOrgDateTime(rangeStr, now, days); + List expandedDates = AgendaUtils.expandOrgDateTime(rangeStr, now, days, overdueToday); assertEquals(dates.size(), expandedDates.size()); assertThat(toStringArray(expandedDates), is(toStringArray(dates))); } diff --git a/app/src/main/java/com/orgzly/android/util/AgendaUtils.java b/app/src/main/java/com/orgzly/android/util/AgendaUtils.java index 657701fac..f1a59d57c 100644 --- a/app/src/main/java/com/orgzly/android/util/AgendaUtils.java +++ b/app/src/main/java/com/orgzly/android/util/AgendaUtils.java @@ -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; @@ -42,20 +43,16 @@ public static Set expandOrgDateTime( } /** Used by tests. */ - static List 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 expandOrgDateTime(String rangeStr, Calendar now, int days, boolean overdueToday) { + return expandOrgDateTime(OrgRange.parse(rangeStr), overdueToday, new DateTime(now), days); } private static List expandOrgDateTime( OrgRange range, boolean overdueToday, DateTime now, int days) { - List result = new ArrayList<>(); + // Only unique values + Set result = new LinkedHashSet<>(); + OrgDateTime rangeStart = range.getStartTime(); OrgDateTime rangeEnd = range.getEndTime(); @@ -83,7 +80,7 @@ private static List expandOrgDateTime( result.addAll(OrgDateTimeUtils.getTimesInInterval(rangeStart, now, to, true, 0)); } - return result; + return new ArrayList<>(result); } private static OrgDateTime buildOrgDateTimeFromDate(DateTime date, OrgRepeater repeater) {