diff --git a/SportsTime/Core/Services/DataProvider.swift b/SportsTime/Core/Services/DataProvider.swift index c60ca37..504526f 100644 --- a/SportsTime/Core/Services/DataProvider.swift +++ b/SportsTime/Core/Services/DataProvider.swift @@ -118,8 +118,8 @@ final class AppDataProvider: ObservableObject { // MARK: - Game Fetching - /// Fetch games from SwiftData within date range - func fetchGames(sports: Set, startDate: Date, endDate: Date) async throws -> [Game] { + /// Filter games from SwiftData within date range + func filterGames(sports: Set, startDate: Date, endDate: Date) async throws -> [Game] { guard let context = modelContext else { throw DataProviderError.contextNotConfigured } @@ -143,6 +143,29 @@ final class AppDataProvider: ObservableObject { } } + /// Get all games for specified sports (no date filtering) + func allGames(for sports: Set) async throws -> [Game] { + guard let context = modelContext else { + throw DataProviderError.contextNotConfigured + } + + let sportStrings = sports.map { $0.rawValue } + + let descriptor = FetchDescriptor( + predicate: #Predicate { game in + game.deprecatedAt == nil + }, + sortBy: [SortDescriptor(\.dateTime)] + ) + + let canonicalGames = try context.fetch(descriptor) + + return canonicalGames.compactMap { canonical -> Game? in + guard sportStrings.contains(canonical.sport) else { return nil } + return canonical.toDomain() + } + } + /// Fetch a single game by canonical ID func fetchGame(by id: String) async throws -> Game? { guard let context = modelContext else { @@ -160,9 +183,9 @@ final class AppDataProvider: ObservableObject { return canonical.toDomain() } - /// Fetch games with full team and stadium data - func fetchRichGames(sports: Set, startDate: Date, endDate: Date) async throws -> [RichGame] { - let games = try await fetchGames(sports: sports, startDate: startDate, endDate: endDate) + /// Filter games with full team and stadium data within date range + func filterRichGames(sports: Set, startDate: Date, endDate: Date) async throws -> [RichGame] { + let games = try await filterGames(sports: sports, startDate: startDate, endDate: endDate) return games.compactMap { game in guard let homeTeam = teamsById[game.homeTeamId],