diff --git a/android/src/main/java/com/thebluealliance/androidclient/fragments/EventsByWeekFragment.java b/android/src/main/java/com/thebluealliance/androidclient/fragments/EventsByWeekFragment.java index a740c3f70..8c3bb3667 100644 --- a/android/src/main/java/com/thebluealliance/androidclient/fragments/EventsByWeekFragment.java +++ b/android/src/main/java/com/thebluealliance/androidclient/fragments/EventsByWeekFragment.java @@ -197,7 +197,7 @@ private int getIndexForWeek(int week) { List tabs = ((EventsByWeekFragmentPagerAdapter) mViewPager.getAdapter()) .getTabs(); for (int i = 0; i < tabs.size(); i++) { - if (tabs.get(i).getWeek() >= week) { + if (tabs.get(i).includesWeek(week)) { return i; } } diff --git a/android/src/main/java/com/thebluealliance/androidclient/models/EventWeekTab.java b/android/src/main/java/com/thebluealliance/androidclient/models/EventWeekTab.java index 2959e677f..6b35869cd 100644 --- a/android/src/main/java/com/thebluealliance/androidclient/models/EventWeekTab.java +++ b/android/src/main/java/com/thebluealliance/androidclient/models/EventWeekTab.java @@ -5,12 +5,14 @@ public class EventWeekTab { private String mLabel; - private int mWeek; + private int mStartWeek = Integer.MAX_VALUE; + + private int mEndWeek = Integer.MIN_VALUE; + private ArrayList mEventKeys; - public EventWeekTab(String label, int week) { + public EventWeekTab(String label) { mLabel = label; - mWeek = week; mEventKeys = new ArrayList<>(); } @@ -18,16 +20,24 @@ public String getLabel() { return mLabel; } - public int getWeek() { - return mWeek; + public boolean includesWeek(int week) { + return week >= mStartWeek && week <= mEndWeek; } public ArrayList getEventKeys() { return mEventKeys; } - public void addEventKey(String eventKey) { - mEventKeys.add(eventKey); + public void addEvent(Event event) { + mEventKeys.add(event.getKey()); + + int eventWeek = event.getWeek() != null ? event.getWeek() : -1; + if (mStartWeek > eventWeek) { + mStartWeek = eventWeek; + } + if (mEndWeek < eventWeek) { + mEndWeek = eventWeek; + } } @Override diff --git a/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java b/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java index 6bfce6476..bd04c147b 100644 --- a/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java +++ b/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java @@ -29,19 +29,16 @@ public void parseData() { LinkedHashMap eventTabs = new LinkedHashMap<>(); for (Event event : mAPIData) { - int eventWeek = event.getWeek() != null - ? event.getWeek() - : -1; String label = EventHelper.generateLabelForEvent(event); if (!eventTabs.containsKey(label)) { - eventTabs.put(label, new EventWeekTab(label, eventWeek)); + eventTabs.put(label, new EventWeekTab(label)); } EventWeekTab tab = eventTabs.get(label); if (tab == null) { throw new RuntimeException("Expected to find event tab, but can't!"); } - tab.addEventKey(event.getKey()); + tab.addEvent(event); } for (Map.Entry tab : eventTabs.entrySet()) { diff --git a/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java b/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java new file mode 100644 index 000000000..5de91d7e5 --- /dev/null +++ b/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java @@ -0,0 +1,44 @@ +package com.thebluealliance.androidclient.models; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class EventWeekTabTest { + + @Test + public void addEventSetsStartEndWeek() { + EventWeekTab tab = new EventWeekTab("label"); + Event event = new Event(); + event.setWeek(5); + tab.addEvent(event); + + assertFalse(tab.includesWeek(4)); + assertTrue(tab.includesWeek(5)); + assertFalse(tab.includesWeek(6)); + } + + @Test + public void addMultipleEvents_multipleWeeks() { + EventWeekTab tab = new EventWeekTab("label"); + + Event event = new Event(); + event.setWeek(5); + tab.addEvent(event); + + Event event2 = new Event(); + event2.setWeek(7); + tab.addEvent(event2); + + assertFalse(tab.includesWeek(4)); + assertTrue(tab.includesWeek(5)); + assertTrue(tab.includesWeek(6)); + assertTrue(tab.includesWeek(7)); + assertFalse(tab.includesWeek(8)); + } +} \ No newline at end of file diff --git a/android/src/test/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriberTest.java b/android/src/test/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriberTest.java index 73331adff..6bd930076 100644 --- a/android/src/test/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriberTest.java +++ b/android/src/test/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriberTest.java @@ -1,6 +1,7 @@ package com.thebluealliance.androidclient.subscribers; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -52,7 +53,8 @@ public void testParsedData() { assertEquals(tabs.size(), 16); for (int i = 0; i < tabs.size(); i++) { EventWeekTab tab = tabs.get(i); - assertEquals(String.format("Tab %1$d week fail", i), weeks[i], tab.getWeek()); + int week = weeks[i]; + assertTrue(String.format("Tab %1$d week fail", i), tab.includesWeek(week)); assertEquals(String.format("Tab %1$d count fail", i), sizes[i], tab.getEventKeys().size()); assertEquals(String.format("Tab %1$d label fail", i), labels[i], tab.getLabel()); }