Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions score-ios/ViewModels/GamesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ class GamesViewModel: ObservableObject
func retryFetch() {
fetchGames()
}

func orderSportsByUpcoming(sports: [Sport]) -> [Sport] {
let allOtherSports = sports.filter { $0 != .All }
let (withGames, withoutGames) = allOtherSports.reduce(into: ([Sport](), [Sport]())) { acc, sport in
if allUpcomingGames.contains(where: { $0.sport == sport }) {
acc.0.append(sport)
} else {
acc.1.append(sport)
}
}
let nextGameBySport: [Sport: Date] = Dictionary(uniqueKeysWithValues:
withGames.map { sport in
let nextDate = allUpcomingGames
.filter { $0.sport == sport }
.map { $0.date }
.min() ?? Date.distantFuture
return (sport, nextDate)
}
)
let sortedWithGames = withGames.sorted { s1, s2 in
(nextGameBySport[s1] ?? Date.distantFuture) < (nextGameBySport[s2] ?? Date.distantFuture)
}
let sortedWithoutGames = withoutGames.sorted { $0.rawValue < $1.rawValue }
return [.All] + sortedWithGames + sortedWithoutGames
}
Comment on lines +304 to +327
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Use the sports argument; it is currently ignored.

The function signature accepts sports, but Line 305 derives from Sport.allCases. This can return items the caller did not request (and always forces .All in the result).

Proposed fix
 func orderSportsByUpcoming(sports: [Sport]) -> [Sport] {
-    let allOtherSports = Sport.allCases.filter { $0 != .All }
+    let allOtherSports = sports.filter { $0 != .All }
@@
-    return [.All] + sortedWithGames + sortedWithoutGames
+    return (sports.contains(.All) ? [.All] : []) + sortedWithGames + sortedWithoutGames
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@score-ios/ViewModels/GamesViewModel.swift` around lines 304 - 327,
orderSportsByUpcoming currently ignores the passed-in sports array and uses
Sport.allCases; change it to use the function parameter instead: compute
allOtherSports from the provided sports (e.g., filter out .All from the sports
parameter) so you only sort the caller-requested items, and only include .All in
the returned array if the input contained it. Update the downstream logic that
builds withGames, withoutGames, nextGameBySport, sortedWithGames, and
sortedWithoutGames to operate on that filtered sports list so the result
respects the input order/selection.

}

extension DataState: Equatable {
Expand Down
2 changes: 1 addition & 1 deletion score-ios/Views/ListViews/SportSelectorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct SportSelectorView: View {
ScrollViewReader { proxy in
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(Sport.allCases) { sport in
ForEach(gamesVM.orderSportsByUpcoming(sports: Sport.allCases)) { sport in
Button {
highlightsVM.selectedSport = sport
gamesVM.selectedSport = sport
Expand Down