Skip to content

Sorting sports in the selector#58

Open
durualayli wants to merge 2 commits intomainfrom
Duru/sports-picker-order
Open

Sorting sports in the selector#58
durualayli wants to merge 2 commits intomainfrom
Duru/sports-picker-order

Conversation

@durualayli
Copy link
Copy Markdown
Contributor

@durualayli durualayli commented Mar 25, 2026

Screenshot 2026-03-25 at 5 45 13 PM
  • Added a function to GamesViewModel to return a list of sports sorted
  • by the date of the games they have
  • Or if there are no upcoming games, sorted alphabetically
  • Changed the sports selector so that reflects this

Summary by CodeRabbit

  • Improvements
    • Sports selector now dynamically reorders to show sports with upcoming games first, sorted by the earliest upcoming event date.
    • "All" remains pinned first; sports with no upcoming games are listed last and sorted alphabetically.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e9b38f73-659f-411e-a8aa-d1402844ca39

📥 Commits

Reviewing files that changed from the base of the PR and between c76a9e2 and 9cc1476.

📒 Files selected for processing (1)
  • score-ios/ViewModels/GamesViewModel.swift
🚧 Files skipped from review as they are similar to previous changes (1)
  • score-ios/ViewModels/GamesViewModel.swift

📝 Walkthrough

Walkthrough

Adds a new GamesViewModel.orderSportsByUpcoming(sports:) method that reorders sports: .All first, then sports with upcoming games sorted by earliest upcoming game date, then sports without upcoming games sorted by rawValue. SportSelectorView now uses this method to render sport tiles.

Changes

Cohort / File(s) Summary
Sport ordering & selector
score-ios/ViewModels/GamesViewModel.swift, score-ios/Views/ListViews/SportSelectorView.swift
Added orderSportsByUpcoming(sports:) to GamesViewModel (public) which returns sports ordered: .All first, then non-.All sports with upcoming games sorted by earliest game date, then non-.All sports without upcoming games sorted by Sport.rawValue. SportSelectorView now iterates over the ordered list instead of Sport.allCases.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hop through lists and sort with care,
.All leads the way — then games soon there,
Those with dates leap up in line,
The rest follow, tidy and fine,
Cheers to ordered tiles — a rabbit’s little flair!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Sorting sports in the selector' directly and clearly describes the main change: reordering sports in the UI selector based on upcoming games.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch Duru/sports-picker-order

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
score-ios/ViewModels/GamesViewModel.swift (1)

322-324: Add a deterministic tie-break for equal upcoming dates.

If two sports share the same next game date, current ordering can vary between refreshes. Add a secondary sort key for stable UI ordering.

Proposed refinement
 let sortedWithGames = withGames.sorted { s1, s2 in
-    (nextGameBySport[s1] ?? Date.distantFuture) < (nextGameBySport[s2] ?? Date.distantFuture)
+    let d1 = nextGameBySport[s1] ?? Date.distantFuture
+    let d2 = nextGameBySport[s2] ?? Date.distantFuture
+    return d1 == d2 ? s1.rawValue < s2.rawValue : d1 < d2
 }
🤖 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 322 - 324, The sort
for sortedWithGames is non-deterministic when nextGameBySport dates are equal;
modify the comparator used where sortedWithGames is created (the sorted { s1, s2
in ... } closure that references nextGameBySport and withGames) to add a stable
secondary key—for example compare s1 and s2 by a deterministic property such as
sport identifier or name (e.g., sport.id or sport.name) when the two Date values
are equal—so ties on (nextGameBySport[s1] ?? Date.distantFuture) result in a
consistent ordering across refreshes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@score-ios/ViewModels/GamesViewModel.swift`:
- Around line 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.

---

Nitpick comments:
In `@score-ios/ViewModels/GamesViewModel.swift`:
- Around line 322-324: The sort for sortedWithGames is non-deterministic when
nextGameBySport dates are equal; modify the comparator used where
sortedWithGames is created (the sorted { s1, s2 in ... } closure that references
nextGameBySport and withGames) to add a stable secondary key—for example compare
s1 and s2 by a deterministic property such as sport identifier or name (e.g.,
sport.id or sport.name) when the two Date values are equal—so ties on
(nextGameBySport[s1] ?? Date.distantFuture) result in a consistent ordering
across refreshes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 24bf2687-698f-4b20-b81f-838efe857810

📥 Commits

Reviewing files that changed from the base of the PR and between fe23b97 and c76a9e2.

📒 Files selected for processing (2)
  • score-ios/ViewModels/GamesViewModel.swift
  • score-ios/Views/ListViews/SportSelectorView.swift

Comment on lines +304 to +327
func orderSportsByUpcoming(sports: [Sport]) -> [Sport] {
let allOtherSports = Sport.allCases.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
}
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant