Skip to content

Commit

Permalink
Fix "current" week calculation outside the regular season (#983)
Browse files Browse the repository at this point in the history
* Fix "current" week calculation outside the regular season

EventWeekTab now represents a range of weeks so we can find the right one

* Import cleanup
  • Loading branch information
bherbst authored Nov 27, 2024
1 parent 908b9b6 commit bb9480c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private int getIndexForWeek(int week) {
List<EventWeekTab> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,39 @@
public class EventWeekTab {

private String mLabel;
private int mWeek;
private int mStartWeek = Integer.MAX_VALUE;

private int mEndWeek = Integer.MIN_VALUE;

private ArrayList<String> mEventKeys;

public EventWeekTab(String label, int week) {
public EventWeekTab(String label) {
mLabel = label;
mWeek = week;
mEventKeys = new ArrayList<>();
}

public String getLabel() {
return mLabel;
}

public int getWeek() {
return mWeek;
public boolean includesWeek(int week) {
return week >= mStartWeek && week <= mEndWeek;
}

public ArrayList<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ public void parseData() {

LinkedHashMap<String, EventWeekTab> 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<String, EventWeekTab> tab : eventTabs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit bb9480c

Please sign in to comment.