import SwiftUI import MapKit // RunDetailView.swift // One run: date, route map, big distance, and a grid of stats. // Only numbers Health actually recorded are shown; anything missing is // listed at the bottom as "Not recorded for this run". struct RunDetailView: View { let run: Run let source: any WorkoutSource @State private var details: RunDetails? @State private var loadFailed = false var body: some View { ScrollView { VStack(alignment: .leading, spacing: 0) { header routeSection .padding(.top, 14) if let meters = run.distanceMeters { HStack(alignment: .firstTextBaseline, spacing: 4) { Text(RunFormat.kilometres(meters)) .font(.system(size: 76, weight: .bold)) .fontWidth(.condensed) Text("km") .font(.title2.weight(.semibold)) .foregroundStyle(.secondary) } .padding(.top, 20) .padding(.horizontal, 4) } StatGrid(tiles: tiles) .padding(.top, 12) footnotes .padding(.top, 14) .padding(.horizontal, 4) } .padding(.horizontal, 16) .padding(.bottom, 32) } .background(Color(.systemGroupedBackground)) .navigationTitle(run.startDate.formatted(.dateTime.day().month())) .navigationBarTitleDisplayMode(.inline) .task { await loadDetails() } } private func loadDetails() async { guard details == nil else { return } do { details = try await source.fetchDetails(for: run) } catch { loadFailed = true } } // MARK: Pieces private var header: some View { VStack(alignment: .leading, spacing: 2) { Text(run.startDate.formatted(.dateTime.weekday(.abbreviated).day().month(.wide).year())) .font(.title2.bold()) Text("\(run.isIndoor ? "Indoor run" : "Outdoor run"), started \(run.startDate.formatted(date: .omitted, time: .shortened))") .foregroundStyle(.secondary) } .padding(.horizontal, 4) .padding(.top, 8) } @ViewBuilder private var routeSection: some View { if let details { if details.route.count >= 2 { RouteMapView(points: details.route) .frame(height: 240) .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous)) } else { MessageCard(title: "No route for this run", text: run.isIndoor ? "Indoor runs don't record GPS." : "This run was saved without GPS.") } } else if loadFailed { MessageCard(title: "Couldn't load the route", text: "Go back and open this run again.") } else { RoundedRectangle(cornerRadius: 16, style: .continuous) .fill(Color(.secondarySystemGroupedBackground)) .frame(height: 240) .overlay(ProgressView()) } } private var cadence: Double? { details?.cadence(duration: run.duration) } private var tiles: [StatTile] { var tiles = [StatTile(label: "Duration", value: RunFormat.duration(run.duration))] if let pace = run.paceSecondsPerKm { tiles.append(StatTile(label: "Avg pace", value: RunFormat.pace(pace), unit: "/km")) } if let hr = run.averageHeartRate { tiles.append(StatTile(label: "Avg heart rate", value: RunFormat.whole(hr), unit: "bpm", dot: .pink)) } if let hr = run.maxHeartRate { tiles.append(StatTile(label: "Max heart rate", value: RunFormat.whole(hr), unit: "bpm", dot: .pink)) } if let cadence { tiles.append(StatTile(label: "Cadence", value: RunFormat.whole(cadence), unit: "spm", dot: .teal)) } else if details == nil && !loadFailed { tiles.append(StatTile(label: "Cadence", value: "…", dot: .teal)) // still loading } if let kcal = run.activeCalories { tiles.append(StatTile(label: "Active calories", value: RunFormat.whole(kcal), unit: "kcal")) } if let climb = run.elevationGainMeters { tiles.append(StatTile(label: "Elevation gain", value: RunFormat.whole(climb), unit: "m")) } return tiles } private var footnotes: some View { VStack(alignment: .leading, spacing: 6) { Text("Recorded by \(run.sourceName)") if !missing.isEmpty { Text("Not recorded for this run: \(missing.joined(separator: ", ")).") .foregroundStyle(.tertiary) } } .font(.footnote) .foregroundStyle(.secondary) } private var missing: [String] { var list: [String] = [] if run.averageHeartRate == nil { list.append("heart rate") } if details != nil && cadence == nil { list.append("cadence") } if run.activeCalories == nil { list.append("calories") } return list } } // MARK: - Route map /// The route as a line on an Apple Map, with a green start dot and a dark finish dot. struct RouteMapView: View { let points: [RoutePoint] var body: some View { let coordinates = points.map { $0.coordinate } Map(initialPosition: .automatic) { MapPolyline(coordinates: coordinates) .stroke(Color.lane, style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round)) if let start = coordinates.first { Annotation("Start", coordinate: start, anchor: .center) { EndDot(color: .green) } .annotationTitles(.hidden) } if let finish = coordinates.last { Annotation("Finish", coordinate: finish, anchor: .center) { EndDot(color: .black) } .annotationTitles(.hidden) } } .mapStyle(.standard(elevation: .flat, pointsOfInterest: .excludingAll)) } } private struct EndDot: View { let color: Color var body: some View { Circle() .fill(color) .frame(width: 14, height: 14) .overlay(Circle().stroke(.white, lineWidth: 2.5)) } } // MARK: - Stats grid struct StatTile: Identifiable { let label: String let value: String var unit: String? = nil var dot: Color? = nil var id: String { label } } /// Two columns of stats on one card, separated by hairlines. struct StatGrid: View { let tiles: [StatTile] var body: some View { let columns = [GridItem(.flexible(), spacing: 0.5), GridItem(.flexible(), spacing: 0.5)] LazyVGrid(columns: columns, spacing: 0.5) { ForEach(tiles) { tile in StatCell(tile: tile) } if tiles.count % 2 == 1 { StatCell(tile: StatTile(label: " ", value: " ")) // blank cell keeps the card even } } .background(Color(.separator)) .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous)) } } private struct StatCell: View { let tile: StatTile var body: some View { VStack(alignment: .leading, spacing: 2) { HStack(spacing: 5) { if let dot = tile.dot { Circle().fill(dot).frame(width: 7, height: 7) } Text(tile.label) } .font(.footnote) .foregroundStyle(.secondary) HStack(alignment: .firstTextBaseline, spacing: 2) { Text(tile.value) .font(.title2.weight(.semibold)) .fontWidth(.condensed) .monospacedDigit() if let unit = tile.unit { Text(unit) .font(.subheadline) .foregroundStyle(.secondary) } } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, 16) .padding(.vertical, 14) .background(Color(.secondarySystemGroupedBackground)) } } /// A rounded card with a short message (e.g. "No route for this run"). private struct MessageCard: View { let title: String let text: String var body: some View { VStack(spacing: 4) { Text(title).font(.headline) Text(text).foregroundStyle(.secondary) } .frame(maxWidth: .infinity) .padding(.vertical, 22) .padding(.horizontal, 18) .background(Color(.secondarySystemGroupedBackground), in: RoundedRectangle(cornerRadius: 16, style: .continuous)) } }