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>
76 lines
2.1 KiB
Swift
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
|
|
}
|
|
}
|