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:
@@ -26,88 +26,18 @@ final class ScheduleViewModel {
|
||||
|
||||
private let dataProvider = AppDataProvider.shared
|
||||
|
||||
// MARK: - Pagination
|
||||
// MARK: - Pre-computed Groupings (avoid computed property overhead)
|
||||
|
||||
private let pageSize = 50
|
||||
private(set) var displayedGames: [RichGame] = []
|
||||
private var currentPage = 0
|
||||
private var allFilteredGames: [RichGame] = []
|
||||
/// Games grouped by sport - pre-computed to avoid re-grouping on every render
|
||||
private(set) var gamesBySport: [(sport: Sport, games: [RichGame])] = []
|
||||
|
||||
var hasMoreGames: Bool {
|
||||
displayedGames.count < allFilteredGames.count
|
||||
}
|
||||
|
||||
/// The last game in the displayed list, used for infinite scroll detection
|
||||
var lastDisplayedGame: RichGame? {
|
||||
displayedGames.last
|
||||
}
|
||||
|
||||
// MARK: - Computed Properties
|
||||
|
||||
var filteredGames: [RichGame] {
|
||||
guard !searchText.isEmpty else { return games }
|
||||
|
||||
let query = searchText.lowercased()
|
||||
return games.filter { game in
|
||||
game.homeTeam.name.lowercased().contains(query) ||
|
||||
game.homeTeam.city.lowercased().contains(query) ||
|
||||
game.awayTeam.name.lowercased().contains(query) ||
|
||||
game.awayTeam.city.lowercased().contains(query) ||
|
||||
game.stadium.name.lowercased().contains(query) ||
|
||||
game.stadium.city.lowercased().contains(query)
|
||||
}
|
||||
}
|
||||
|
||||
var gamesByDate: [(date: Date, games: [RichGame])] {
|
||||
let calendar = Calendar.current
|
||||
let grouped = Dictionary(grouping: filteredGames) { game in
|
||||
calendar.startOfDay(for: game.game.dateTime)
|
||||
}
|
||||
return grouped.sorted { $0.key < $1.key }.map { ($0.key, $0.value) }
|
||||
}
|
||||
|
||||
var gamesBySport: [(sport: Sport, games: [RichGame])] {
|
||||
let grouped = Dictionary(grouping: displayedGames) { game in
|
||||
game.game.sport
|
||||
}
|
||||
// Sort by sport order (use allCases index for consistent ordering)
|
||||
// Within each sport, games are sorted by date
|
||||
return grouped
|
||||
.sorted { lhs, rhs in
|
||||
let lhsIndex = Sport.allCases.firstIndex(of: lhs.key) ?? 0
|
||||
let rhsIndex = Sport.allCases.firstIndex(of: rhs.key) ?? 0
|
||||
return lhsIndex < rhsIndex
|
||||
}
|
||||
.map { (sport: $0.key, games: $0.value.sorted { $0.game.dateTime < $1.game.dateTime }) }
|
||||
}
|
||||
/// All games matching current filters (before any display limiting)
|
||||
private var filteredGames: [RichGame] = []
|
||||
|
||||
var hasFilters: Bool {
|
||||
selectedSports.count < Sport.supported.count || !searchText.isEmpty
|
||||
}
|
||||
|
||||
// MARK: - Pagination Actions
|
||||
|
||||
/// Updates filtered games list based on current search text and resets pagination
|
||||
func updateFilteredGames() {
|
||||
allFilteredGames = filteredGames
|
||||
loadInitialGames()
|
||||
}
|
||||
|
||||
/// Loads the first page of games
|
||||
func loadInitialGames() {
|
||||
currentPage = 0
|
||||
displayedGames = Array(allFilteredGames.prefix(pageSize))
|
||||
}
|
||||
|
||||
/// Loads more games when scrolling to the bottom
|
||||
func loadMoreGames() {
|
||||
guard hasMoreGames else { return }
|
||||
currentPage += 1
|
||||
let start = currentPage * pageSize
|
||||
let end = min(start + pageSize, allFilteredGames.count)
|
||||
displayedGames.append(contentsOf: allFilteredGames[start..<end])
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
func loadGames() async {
|
||||
@@ -186,4 +116,34 @@ final class ScheduleViewModel {
|
||||
await loadGames()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Filtering & Grouping (pre-computed, not computed properties)
|
||||
|
||||
/// Recomputes filtered games and groupings based on current search text
|
||||
func updateFilteredGames() {
|
||||
// Step 1: Filter by search text
|
||||
if searchText.isEmpty {
|
||||
filteredGames = games
|
||||
} else {
|
||||
let query = searchText.lowercased()
|
||||
filteredGames = games.filter { game in
|
||||
game.homeTeam.name.lowercased().contains(query) ||
|
||||
game.homeTeam.city.lowercased().contains(query) ||
|
||||
game.awayTeam.name.lowercased().contains(query) ||
|
||||
game.awayTeam.city.lowercased().contains(query) ||
|
||||
game.stadium.name.lowercased().contains(query) ||
|
||||
game.stadium.city.lowercased().contains(query)
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Pre-compute grouping by sport (done once, not per-render)
|
||||
let grouped = Dictionary(grouping: filteredGames) { $0.game.sport }
|
||||
gamesBySport = grouped
|
||||
.sorted { lhs, rhs in
|
||||
let lhsIndex = Sport.allCases.firstIndex(of: lhs.key) ?? 0
|
||||
let rhsIndex = Sport.allCases.firstIndex(of: rhs.key) ?? 0
|
||||
return lhsIndex < rhsIndex
|
||||
}
|
||||
.map { (sport: $0.key, games: $0.value.sorted { $0.game.dateTime < $1.game.dateTime }) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user