feat(progress): add progress tracking enhancements
- 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>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import SwiftUI
|
||||
import MapKit
|
||||
|
||||
@MainActor
|
||||
@Observable
|
||||
final class MapInteractionViewModel {
|
||||
// Default region: Continental US
|
||||
static let defaultRegion = MKCoordinateRegion(
|
||||
center: CLLocationCoordinate2D(latitude: 39.8283, longitude: -98.5795),
|
||||
span: MKCoordinateSpan(latitudeDelta: 35, longitudeDelta: 55)
|
||||
)
|
||||
|
||||
// City-level zoom span
|
||||
static let stadiumZoomSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
|
||||
|
||||
var region: MKCoordinateRegion = MapInteractionViewModel.defaultRegion
|
||||
var hasUserInteracted: Bool = false
|
||||
var selectedStadiumId: String?
|
||||
|
||||
var shouldShowResetButton: Bool {
|
||||
hasUserInteracted
|
||||
}
|
||||
|
||||
func userDidInteract() {
|
||||
hasUserInteracted = true
|
||||
}
|
||||
|
||||
func resetToDefault() {
|
||||
withAnimation(.easeInOut(duration: 0.5)) {
|
||||
region = MapInteractionViewModel.defaultRegion
|
||||
}
|
||||
hasUserInteracted = false
|
||||
selectedStadiumId = nil
|
||||
}
|
||||
|
||||
func zoomToStadium(at coordinate: CLLocationCoordinate2D) {
|
||||
withAnimation(.easeInOut(duration: 0.3)) {
|
||||
region = MKCoordinateRegion(
|
||||
center: coordinate,
|
||||
span: MapInteractionViewModel.stadiumZoomSpan
|
||||
)
|
||||
}
|
||||
hasUserInteracted = true
|
||||
}
|
||||
|
||||
func selectStadium(id: String, coordinate: CLLocationCoordinate2D) {
|
||||
selectedStadiumId = id
|
||||
zoomToStadium(at: coordinate)
|
||||
}
|
||||
|
||||
func deselectStadium() {
|
||||
selectedStadiumId = nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import SwiftUI
|
||||
|
||||
struct GamesHistoryRow: View {
|
||||
let visit: StadiumVisit
|
||||
let stadium: Stadium?
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 12) {
|
||||
// Sport icon
|
||||
if let stadium {
|
||||
Image(systemName: sportIcon(for: stadium.sport))
|
||||
.font(.title3)
|
||||
.foregroundStyle(stadium.sport.themeColor)
|
||||
.frame(width: 32)
|
||||
}
|
||||
|
||||
// Visit info
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
// Date
|
||||
Text(visit.visitDate.formatted(date: .abbreviated, time: .omitted))
|
||||
.font(.subheadline.bold())
|
||||
|
||||
// Teams (if game)
|
||||
if let matchup = visit.matchupDescription {
|
||||
Text(matchup)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
} else {
|
||||
Text(visit.stadiumNameAtVisit)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
// Chevron
|
||||
Image(systemName: "chevron.right")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.tertiary)
|
||||
}
|
||||
.padding()
|
||||
.background(Color(.systemBackground))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 10))
|
||||
}
|
||||
|
||||
private func sportIcon(for sport: Sport) -> String {
|
||||
switch sport {
|
||||
case .mlb: return "baseball"
|
||||
case .nba: return "basketball"
|
||||
case .nhl: return "hockey.puck"
|
||||
case .nfl: return "football"
|
||||
case .mls: return "soccerball"
|
||||
@unknown default: return "sportscourt"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
VStack {
|
||||
Text("GamesHistoryRow Preview")
|
||||
}
|
||||
.padding()
|
||||
.background(Color(.systemGroupedBackground))
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
import SwiftUI
|
||||
|
||||
struct VisitListCard: View {
|
||||
let visit: StadiumVisit
|
||||
@State private var isExpanded = false
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
// Header row (always visible)
|
||||
Button {
|
||||
withAnimation(.easeInOut(duration: 0.2)) {
|
||||
isExpanded.toggle()
|
||||
}
|
||||
} label: {
|
||||
HStack {
|
||||
// Date
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(visit.visitDate.formatted(date: .abbreviated, time: .omitted))
|
||||
.font(.subheadline.bold())
|
||||
Text(visit.visitType.displayName)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
// Game info preview (if available)
|
||||
if let matchup = visit.matchupDescription {
|
||||
Text(matchup)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
|
||||
// Chevron
|
||||
Image(systemName: "chevron.right")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.rotationEffect(.degrees(isExpanded ? 90 : 0))
|
||||
}
|
||||
.padding()
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
// Expanded content
|
||||
if isExpanded {
|
||||
Divider()
|
||||
.padding(.horizontal)
|
||||
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
// Game details
|
||||
if let matchup = visit.matchupDescription {
|
||||
GameInfoRow(matchup: matchup, score: visit.finalScore)
|
||||
}
|
||||
|
||||
// Seat location
|
||||
if let seat = visit.seatLocation, !seat.isEmpty {
|
||||
InfoRow(icon: "ticket", label: "Seat", value: seat)
|
||||
}
|
||||
|
||||
// Notes
|
||||
if let notes = visit.notes, !notes.isEmpty {
|
||||
InfoRow(icon: "note.text", label: "Notes", value: notes)
|
||||
}
|
||||
|
||||
// Photos count
|
||||
if let photos = visit.photoMetadata, !photos.isEmpty {
|
||||
InfoRow(
|
||||
icon: "photo.on.rectangle",
|
||||
label: "Photos",
|
||||
value: "\(photos.count) photo\(photos.count == 1 ? "" : "s")"
|
||||
)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.transition(.opacity.combined(with: .move(edge: .top)))
|
||||
}
|
||||
}
|
||||
.background(Color(.systemBackground))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 12))
|
||||
}
|
||||
}
|
||||
|
||||
private struct GameInfoRow: View {
|
||||
let matchup: String
|
||||
let score: String?
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(matchup)
|
||||
.font(.subheadline.bold())
|
||||
|
||||
if let score {
|
||||
Text("Final: \(score)")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct InfoRow: View {
|
||||
let icon: String
|
||||
let label: String
|
||||
let value: String
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 8) {
|
||||
Image(systemName: icon)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(width: 16)
|
||||
|
||||
Text(label)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
Text(value)
|
||||
.font(.caption)
|
||||
.lineLimit(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
VStack {
|
||||
Text("VisitListCard Preview")
|
||||
}
|
||||
.padding()
|
||||
.background(Color(.systemGroupedBackground))
|
||||
}
|
||||
210
SportsTime/Features/Progress/Views/GamesHistoryView.swift
Normal file
210
SportsTime/Features/Progress/Views/GamesHistoryView.swift
Normal file
@@ -0,0 +1,210 @@
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct GamesHistoryView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@State private var viewModel: GamesHistoryViewModel?
|
||||
@State private var selectedVisit: StadiumVisit?
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if let viewModel {
|
||||
GamesHistoryContent(
|
||||
viewModel: viewModel,
|
||||
selectedVisit: $selectedVisit
|
||||
)
|
||||
} else {
|
||||
ProgressView("Loading games...")
|
||||
}
|
||||
}
|
||||
.navigationTitle("Games Attended")
|
||||
.sheet(item: $selectedVisit) { visit in
|
||||
if let stadium = AppDataProvider.shared.stadium(for: visit.stadiumId) {
|
||||
VisitDetailView(visit: visit, stadium: stadium)
|
||||
}
|
||||
}
|
||||
.task {
|
||||
if viewModel == nil {
|
||||
let vm = GamesHistoryViewModel(modelContext: modelContext)
|
||||
await vm.loadGames()
|
||||
viewModel = vm
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct GamesHistoryContent: View {
|
||||
@Bindable var viewModel: GamesHistoryViewModel
|
||||
@Binding var selectedVisit: StadiumVisit?
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
// Header with count and filters
|
||||
VStack(spacing: 12) {
|
||||
// Total count
|
||||
HStack {
|
||||
Text("\(viewModel.totalGamesCount) Games")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
|
||||
if !viewModel.selectedSports.isEmpty {
|
||||
Button("Clear") {
|
||||
viewModel.clearFilters()
|
||||
}
|
||||
.font(.caption)
|
||||
}
|
||||
}
|
||||
|
||||
// Sport filter chips
|
||||
SportFilterChips(
|
||||
selectedSports: viewModel.selectedSports,
|
||||
onToggle: viewModel.toggleSport
|
||||
)
|
||||
}
|
||||
.padding()
|
||||
.background(Color(.systemBackground))
|
||||
|
||||
Divider()
|
||||
|
||||
// Games list grouped by year
|
||||
if viewModel.filteredVisits.isEmpty {
|
||||
EmptyGamesView()
|
||||
} else {
|
||||
GamesListByYear(
|
||||
visitsByYear: viewModel.visitsByYear,
|
||||
sortedYears: viewModel.sortedYears,
|
||||
onSelect: { visit in
|
||||
selectedVisit = visit
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
.background(Color(.systemGroupedBackground))
|
||||
}
|
||||
}
|
||||
|
||||
private struct SportFilterChips: View {
|
||||
let selectedSports: Set<Sport>
|
||||
let onToggle: (Sport) -> Void
|
||||
|
||||
private let sports: [Sport] = [.mlb, .nba, .nhl]
|
||||
|
||||
var body: some View {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(spacing: 8) {
|
||||
ForEach(sports, id: \.self) { sport in
|
||||
SportChip(
|
||||
sport: sport,
|
||||
isSelected: selectedSports.contains(sport),
|
||||
onTap: { onToggle(sport) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct SportChip: View {
|
||||
let sport: Sport
|
||||
let isSelected: Bool
|
||||
let onTap: () -> Void
|
||||
|
||||
var body: some View {
|
||||
Button(action: onTap) {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: sportIcon)
|
||||
.font(.caption)
|
||||
Text(sport.rawValue)
|
||||
.font(.caption.bold())
|
||||
}
|
||||
.foregroundStyle(isSelected ? .white : .primary)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 6)
|
||||
.background(
|
||||
Capsule()
|
||||
.fill(isSelected ? sport.themeColor : Color(.systemGray5))
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
|
||||
private var sportIcon: String {
|
||||
switch sport {
|
||||
case .mlb: return "baseball"
|
||||
case .nba: return "basketball"
|
||||
case .nhl: return "hockey.puck"
|
||||
case .nfl: return "football"
|
||||
case .mls: return "soccerball"
|
||||
@unknown default: return "sportscourt"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct GamesListByYear: View {
|
||||
let visitsByYear: [Int: [StadiumVisit]]
|
||||
let sortedYears: [Int]
|
||||
let onSelect: (StadiumVisit) -> Void
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: 0, pinnedViews: .sectionHeaders) {
|
||||
ForEach(sortedYears, id: \.self) { year in
|
||||
Section {
|
||||
VStack(spacing: 8) {
|
||||
ForEach(visitsByYear[year] ?? [], id: \.id) { visit in
|
||||
Button {
|
||||
onSelect(visit)
|
||||
} label: {
|
||||
GamesHistoryRow(
|
||||
visit: visit,
|
||||
stadium: AppDataProvider.shared.stadium(for: visit.stadiumId)
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal)
|
||||
.padding(.vertical, 8)
|
||||
} header: {
|
||||
YearHeader(year: year)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct YearHeader: View {
|
||||
let year: Int
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(String(year))
|
||||
.font(.title3.bold())
|
||||
Spacer()
|
||||
}
|
||||
.padding(.horizontal)
|
||||
.padding(.vertical, 8)
|
||||
.background(Color(.systemGroupedBackground))
|
||||
}
|
||||
}
|
||||
|
||||
private struct EmptyGamesView: View {
|
||||
var body: some View {
|
||||
VStack(spacing: 16) {
|
||||
Image(systemName: "ticket")
|
||||
.font(.system(size: 48))
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
Text("No games recorded yet")
|
||||
.font(.headline)
|
||||
|
||||
Text("Add your first stadium visit to see it here!")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,11 @@ struct ProgressMapView: View {
|
||||
let visitStatus: [String: StadiumVisitStatus]
|
||||
@Binding var selectedStadium: Stadium?
|
||||
|
||||
// Fixed region for continental US - map is locked to this view
|
||||
private let usRegion = MKCoordinateRegion(
|
||||
center: CLLocationCoordinate2D(latitude: 39.8283, longitude: -98.5795),
|
||||
span: MKCoordinateSpan(latitudeDelta: 50, longitudeDelta: 60)
|
||||
)
|
||||
@State private var mapViewModel = MapInteractionViewModel()
|
||||
@State private var cameraPosition: MapCameraPosition = .region(MapInteractionViewModel.defaultRegion)
|
||||
|
||||
var body: some View {
|
||||
// Use initialPosition with empty interactionModes to disable pan/zoom
|
||||
// while keeping annotations tappable
|
||||
Map(initialPosition: .region(usRegion), interactionModes: []) {
|
||||
Map(position: $cameraPosition, interactionModes: [.zoom, .pan]) {
|
||||
ForEach(stadiums) { stadium in
|
||||
Annotation(
|
||||
stadium.name,
|
||||
@@ -50,6 +45,26 @@ struct ProgressMapView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.onMapCameraChange { _ in
|
||||
mapViewModel.userDidInteract()
|
||||
}
|
||||
.overlay(alignment: .bottomTrailing) {
|
||||
if mapViewModel.shouldShowResetButton {
|
||||
Button {
|
||||
withAnimation(.easeInOut(duration: 0.5)) {
|
||||
cameraPosition = .region(MapInteractionViewModel.defaultRegion)
|
||||
mapViewModel.resetToDefault()
|
||||
selectedStadium = nil
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "arrow.counterclockwise")
|
||||
.font(.title3)
|
||||
.padding(12)
|
||||
.background(.regularMaterial, in: Circle())
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
.mapStyle(.standard(elevation: .realistic))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 16))
|
||||
}
|
||||
|
||||
@@ -261,7 +261,8 @@ struct ProgressTabView: View {
|
||||
ForEach(viewModel.visitedStadiums) { stadium in
|
||||
StadiumChip(
|
||||
stadium: stadium,
|
||||
isVisited: true
|
||||
isVisited: true,
|
||||
visitCount: viewModel.stadiumVisitStatus[stadium.id]?.visitCount ?? 1
|
||||
) {
|
||||
selectedStadium = stadium
|
||||
}
|
||||
@@ -368,9 +369,24 @@ struct ProgressTabView: View {
|
||||
|
||||
private var recentVisitsSection: some View {
|
||||
VStack(alignment: .leading, spacing: Theme.Spacing.sm) {
|
||||
Text("Recent Visits")
|
||||
.font(.title2)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
HStack {
|
||||
Text("Recent Visits")
|
||||
.font(.title2)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
|
||||
Spacer()
|
||||
|
||||
NavigationLink {
|
||||
GamesHistoryView()
|
||||
} label: {
|
||||
HStack(spacing: 4) {
|
||||
Text("See All")
|
||||
Image(systemName: "chevron.right")
|
||||
}
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Theme.warmOrange)
|
||||
}
|
||||
}
|
||||
|
||||
ForEach(viewModel.recentVisits) { visitSummary in
|
||||
if let stadiumVisit = visits.first(where: { $0.id == visitSummary.id }) {
|
||||
@@ -418,6 +434,7 @@ struct ProgressStatPill: View {
|
||||
struct StadiumChip: View {
|
||||
let stadium: Stadium
|
||||
let isVisited: Bool
|
||||
var visitCount: Int = 1
|
||||
let action: () -> Void
|
||||
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
@@ -432,10 +449,22 @@ struct StadiumChip: View {
|
||||
}
|
||||
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(stadium.name)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
.lineLimit(1)
|
||||
HStack(spacing: 4) {
|
||||
Text(stadium.name)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
.lineLimit(1)
|
||||
|
||||
// Visit count badge (if more than 1)
|
||||
if visitCount > 1 {
|
||||
Text("\(visitCount)")
|
||||
.font(.caption2.bold())
|
||||
.foregroundStyle(.white)
|
||||
.padding(.horizontal, 6)
|
||||
.padding(.vertical, 2)
|
||||
.background(Theme.warmOrange, in: Capsule())
|
||||
}
|
||||
}
|
||||
|
||||
Text(stadium.city)
|
||||
.font(.caption)
|
||||
|
||||
109
SportsTime/Features/Progress/Views/StadiumVisitHistoryView.swift
Normal file
109
SportsTime/Features/Progress/Views/StadiumVisitHistoryView.swift
Normal file
@@ -0,0 +1,109 @@
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct StadiumVisitHistoryView: View {
|
||||
let stadium: Stadium
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var visits: [StadiumVisit] = []
|
||||
@State private var isLoading = true
|
||||
@State private var showingAddVisit = false
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Group {
|
||||
if isLoading {
|
||||
ProgressView()
|
||||
} else if visits.isEmpty {
|
||||
EmptyVisitHistoryView()
|
||||
} else {
|
||||
VisitHistoryList(visits: visits)
|
||||
}
|
||||
}
|
||||
.navigationTitle(stadium.name)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Done") { dismiss() }
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button {
|
||||
showingAddVisit = true
|
||||
} label: {
|
||||
Image(systemName: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showingAddVisit) {
|
||||
StadiumVisitSheet(initialStadium: stadium)
|
||||
}
|
||||
}
|
||||
.task {
|
||||
await loadVisits()
|
||||
}
|
||||
}
|
||||
|
||||
private func loadVisits() async {
|
||||
isLoading = true
|
||||
defer { isLoading = false }
|
||||
|
||||
let stadiumId = stadium.id
|
||||
let descriptor = FetchDescriptor<StadiumVisit>(
|
||||
predicate: #Predicate { $0.stadiumId == stadiumId },
|
||||
sortBy: [SortDescriptor(\.visitDate, order: .reverse)]
|
||||
)
|
||||
|
||||
do {
|
||||
visits = try modelContext.fetch(descriptor)
|
||||
} catch {
|
||||
visits = []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct VisitHistoryList: View {
|
||||
let visits: [StadiumVisit]
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: 12) {
|
||||
// Visit count header
|
||||
HStack {
|
||||
Text("\(visits.count) Visit\(visits.count == 1 ? "" : "s")")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
}
|
||||
.padding(.horizontal)
|
||||
|
||||
// Visit cards
|
||||
ForEach(visits, id: \.id) { visit in
|
||||
VisitListCard(visit: visit)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
}
|
||||
.padding(.vertical)
|
||||
}
|
||||
.background(Color(.systemGroupedBackground))
|
||||
}
|
||||
}
|
||||
|
||||
private struct EmptyVisitHistoryView: View {
|
||||
var body: some View {
|
||||
VStack(spacing: 16) {
|
||||
Image(systemName: "calendar.badge.plus")
|
||||
.font(.system(size: 48))
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
Text("No visits recorded")
|
||||
.font(.headline)
|
||||
|
||||
Text("Tap + to add your first visit to this stadium")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user