Files
Sportstime/SportsTime/Features/Progress/ViewModels/GamesHistoryViewModel.swift
Trey t 91c5eac22d fix: codebase audit fixes — safety, accessibility, and production hygiene
Address 16 issues from external audit:
- Move StoreKit transaction listener ownership to StoreManager singleton with proper deinit
- Remove noisy VoiceOver announcements, add missing accessibility on StatPill and BootstrapLoadingView
- Replace String @retroactive Identifiable with IdentifiableShareCode wrapper
- Add crash guard in AchievementEngine getContributingVisitIds + cache stadium lookups
- Pre-compute GamesHistoryViewModel filtered properties to avoid redundant SwiftUI recomputation
- Remove force-unwraps in ProgressMapView with safe guard-let fallback
- Add diff-based update gating in ItineraryTableViewWrapper to prevent unnecessary reloads
- Replace deprecated UIScreen.main with UIWindowScene lookup
- Add deinit task cancellation in ScheduleViewModel and SuggestedTripsGenerator
- Wrap ~234 unguarded print() calls across 27 files in #if DEBUG

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 00:07:53 -06:00

76 lines
2.1 KiB
Swift

import SwiftUI
import SwiftData
@MainActor
@Observable
final class GamesHistoryViewModel {
private let modelContext: ModelContext
var allVisits: [StadiumVisit] = []
var selectedSports: Set<Sport> = [] {
didSet { recomputeFilteredData() }
}
var isLoading = false
var error: String?
// Pre-computed stored properties (updated via recomputeFilteredData)
private(set) var filteredVisits: [StadiumVisit] = []
private(set) var visitsByYear: [Int: [StadiumVisit]] = [:]
private(set) var sortedYears: [Int] = []
private(set) var totalGamesCount: Int = 0
init(modelContext: ModelContext) {
self.modelContext = modelContext
}
func loadGames() async {
isLoading = true
defer { isLoading = false }
let descriptor = FetchDescriptor<StadiumVisit>(
sortBy: [SortDescriptor(\.visitDate, order: .reverse)]
)
do {
allVisits = try modelContext.fetch(descriptor)
} catch {
self.error = "Failed to load games: \(error.localizedDescription)"
allVisits = []
}
recomputeFilteredData()
}
func toggleSport(_ sport: Sport) {
if selectedSports.contains(sport) {
selectedSports.remove(sport)
} else {
selectedSports.insert(sport)
}
}
func clearFilters() {
selectedSports.removeAll()
}
private func recomputeFilteredData() {
if selectedSports.isEmpty {
filteredVisits = allVisits
} else {
filteredVisits = allVisits.filter { visit in
guard let stadium = AppDataProvider.shared.stadium(for: visit.stadiumId) else {
return false
}
return selectedSports.contains(stadium.sport)
}
}
let calendar = Calendar.current
visitsByYear = Dictionary(grouping: filteredVisits) { visit in
calendar.component(.year, from: visit.visitDate)
}
sortedYears = visitsByYear.keys.sorted(by: >)
totalGamesCount = filteredVisits.count
}
}