- Enable zoom/pan on progress map with reset button - Add visit count badges to stadium chips - Create GamesHistoryView with year grouping and sport filters - Create StadiumVisitHistoryView for viewing all visits to a stadium - Add VisitListCard and GamesHistoryRow components - Add "See All" navigation from Recent Visits to Games History - Add tests for map interactions, visit lists, and games history Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
77 lines
1.9 KiB
Swift
77 lines
1.9 KiB
Swift
import SwiftUI
|
|
import SwiftData
|
|
|
|
@MainActor
|
|
@Observable
|
|
final class GamesHistoryViewModel {
|
|
private let modelContext: ModelContext
|
|
|
|
var allVisits: [StadiumVisit] = []
|
|
var selectedSports: Set<Sport> = []
|
|
var isLoading = false
|
|
var error: String?
|
|
|
|
// Computed: visits grouped by year
|
|
var visitsByYear: [Int: [StadiumVisit]] {
|
|
let calendar = Calendar.current
|
|
let filtered = filteredVisits
|
|
return Dictionary(grouping: filtered) { visit in
|
|
calendar.component(.year, from: visit.visitDate)
|
|
}
|
|
}
|
|
|
|
// Computed: sorted year keys (descending)
|
|
var sortedYears: [Int] {
|
|
visitsByYear.keys.sorted(by: >)
|
|
}
|
|
|
|
// Computed: filtered by selected sports
|
|
var filteredVisits: [StadiumVisit] {
|
|
guard !selectedSports.isEmpty else { return allVisits }
|
|
|
|
return allVisits.filter { visit in
|
|
guard let stadium = AppDataProvider.shared.stadium(for: visit.stadiumId) else {
|
|
return false
|
|
}
|
|
return selectedSports.contains(stadium.sport)
|
|
}
|
|
}
|
|
|
|
// Total count
|
|
var totalGamesCount: Int {
|
|
filteredVisits.count
|
|
}
|
|
|
|
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 = []
|
|
}
|
|
}
|
|
|
|
func toggleSport(_ sport: Sport) {
|
|
if selectedSports.contains(sport) {
|
|
selectedSports.remove(sport)
|
|
} else {
|
|
selectedSports.insert(sport)
|
|
}
|
|
}
|
|
|
|
func clearFilters() {
|
|
selectedSports.removeAll()
|
|
}
|
|
}
|