perf: lazy hierarchical loading for game picker

Replace upfront loading of all games with lazy Sport → Team → Game
hierarchy. Games are now fetched per-team when expanded and cached
to prevent re-fetching. Also removes pagination workaround and
pre-computes groupings in ScheduleViewModel to avoid per-render work.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-01-12 20:19:46 -06:00
parent 9531ed1008
commit 37a1347ce3
5 changed files with 235 additions and 295 deletions

View File

@@ -10,13 +10,17 @@ struct ScheduleListView: View {
@State private var viewModel = ScheduleViewModel()
@State private var showDatePicker = false
private var allGames: [RichGame] {
viewModel.gamesBySport.flatMap(\.games)
}
var body: some View {
Group {
if viewModel.isLoading && viewModel.displayedGames.isEmpty {
if viewModel.isLoading && allGames.isEmpty {
loadingView
} else if let errorMessage = viewModel.errorMessage {
errorView(message: errorMessage)
} else if viewModel.displayedGames.isEmpty {
} else if allGames.isEmpty {
emptyView
} else {
gamesList
@@ -96,12 +100,6 @@ struct ScheduleListView: View {
Section {
ForEach(sportGroup.games) { richGame in
GameRowView(game: richGame, showDate: true, showLocation: true)
.onAppear {
// Trigger load more when the last game appears
if richGame.id == viewModel.lastDisplayedGame?.id {
viewModel.loadMoreGames()
}
}
}
} header: {
HStack(spacing: 8) {
@@ -112,18 +110,6 @@ struct ScheduleListView: View {
}
.listRowBackground(Theme.cardBackground(colorScheme))
}
// Show loading indicator when more games are available
if viewModel.hasMoreGames {
Section {
HStack {
Spacer()
ProgressView()
Spacer()
}
.listRowBackground(Color.clear)
}
}
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
@@ -231,6 +217,14 @@ struct GameRowView: View {
var showDate: Bool = false
var showLocation: Bool = false
// Static formatter to avoid allocation per row (significant performance improvement)
private static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "EEE, MMM d"
return formatter
}()
// Cache isToday check to avoid repeated Calendar calls
private var isToday: Bool {
Calendar.current.isDateInToday(game.game.dateTime)
}
@@ -240,7 +234,7 @@ struct GameRowView: View {
// Date (when grouped by sport)
if showDate {
HStack(spacing: 6) {
Text(formattedDate)
Text(Self.dateFormatter.string(from: game.game.dateTime))
.font(.caption)
.fontWeight(.medium)
.foregroundStyle(.secondary)
@@ -300,12 +294,6 @@ struct GameRowView: View {
.padding(.vertical, 4)
.listRowBackground(isToday ? Color.orange.opacity(0.1) : nil)
}
private var formattedDate: String {
let formatter = DateFormatter()
formatter.dateFormat = "EEE, MMM d"
return formatter.string(from: game.game.dateTime)
}
}
// MARK: - Team Badge