From ddbe37cee0b212eaa1ce62d368d2c455063c7ff4 Mon Sep 17 00:00:00 2001 From: Bryan Herbst Date: Wed, 20 Nov 2024 14:24:43 -0600 Subject: [PATCH 1/2] Fix "current" week calculation outside the regular season EventWeekTab now represents a range of weeks so we can find the right one --- .../fragments/EventsByWeekFragment.java | 2 +- .../androidclient/models/EventWeekTab.java | 24 +++++++--- .../subscribers/EventTabSubscriber.java | 8 ++-- .../models/EventWeekTabTest.java | 46 +++++++++++++++++++ .../subscribers/EventTabSubscriberTest.java | 4 +- 5 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java 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..7e6ffb17e 100644 --- a/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java +++ b/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java @@ -4,6 +4,7 @@ import com.thebluealliance.androidclient.helpers.EventHelper; import com.thebluealliance.androidclient.models.Event; import com.thebluealliance.androidclient.models.EventWeekTab; +import com.thebluealliance.androidclient.types.EventType; import java.util.ArrayList; import java.util.Collections; @@ -29,19 +30,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..6be0c4304 --- /dev/null +++ b/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java @@ -0,0 +1,46 @@ +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 junit.framework.TestCase; + +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()); } From 1ed59ea45453c0d78ae720b21b26d9493877ec43 Mon Sep 17 00:00:00 2001 From: Bryan Herbst Date: Wed, 20 Nov 2024 14:36:51 -0600 Subject: [PATCH 2/2] Import cleanup --- .../androidclient/subscribers/EventTabSubscriber.java | 1 - .../thebluealliance/androidclient/models/EventWeekTabTest.java | 2 -- 2 files changed, 3 deletions(-) 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 7e6ffb17e..bd04c147b 100644 --- a/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java +++ b/android/src/main/java/com/thebluealliance/androidclient/subscribers/EventTabSubscriber.java @@ -4,7 +4,6 @@ import com.thebluealliance.androidclient.helpers.EventHelper; import com.thebluealliance.androidclient.models.Event; import com.thebluealliance.androidclient.models.EventWeekTab; -import com.thebluealliance.androidclient.types.EventType; import java.util.ArrayList; import java.util.Collections; diff --git a/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java b/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java index 6be0c4304..5de91d7e5 100644 --- a/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java +++ b/android/src/test/java/com/thebluealliance/androidclient/models/EventWeekTabTest.java @@ -5,8 +5,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; -import junit.framework.TestCase; - import org.junit.Test; import org.junit.runner.RunWith;