import Foundation // WorkoutSource.swift // The "workout source" layer: one list of jobs that any data source must do. // - HealthKitWorkoutSource (other file) = your real runs from Apple Health, used on the iPhone. // - SampleWorkoutSource (below) = made-up runs, used ONLY in the simulator, // because the simulator has no Apple Watch data. The app labels them as samples. @MainActor protocol WorkoutSource { /// False on devices without Apple Health (e.g. some iPads). var isAvailable: Bool { get } /// True only for the made-up simulator data. var isSampleData: Bool { get } /// Shows Apple's "allow RunLog to read…" sheet (only the first time). func requestAccess() async throws /// All running workouts, newest first. func fetchRuns() async throws -> [Run] /// Route + steps for one run (loaded when you open it). func fetchDetails(for run: Run) async throws -> RunDetails } // MARK: - Sample data (simulator only) @MainActor final class SampleWorkoutSource: WorkoutSource { let isAvailable = true let isSampleData = true private var detailsByID: [UUID: RunDetails] = [:] func requestAccess() async throws {} func fetchRuns() async throws -> [Run] { try await Task.sleep(for: .milliseconds(400)) // pretend to load, so the loading screen can be seen // (days ago, start hour, km, seconds, avg HR, max HR, kcal, steps, climb m, indoor) let samples: [(Int, Int, Double, Double, Double?, Double?, Double?, Double?, Double?, Bool)] = [ (1, 7, 10.02, 3134, 152, 171, 642, 8960, 58, false), (3, 19, 5.21, 1650, 147, 163, 331, 4720, 21, false), (6, 6, 15.40, 5262, 149, 168, 985, 14650, 96, false), (9, 20, 6.00, 2100, 139, 151, 368, 5940, nil, true), (12, 7, 8.05, 2611, 150, 166, 512, 7310, 40, false), (30, 6, 21.10, 7712, 156, 178, 1390, 20110, 133, false), (34, 18, 4.98, 1622, nil, nil, nil, nil, nil, false), ] let calendar = Calendar.current let today = calendar.startOfDay(for: .now) var runs: [Run] = [] for s in samples { let day = calendar.date(byAdding: .day, value: -s.0, to: today) ?? today let start = calendar.date(byAdding: .hour, value: s.1, to: day) ?? day let run = Run( id: UUID(), startDate: start, endDate: start.addingTimeInterval(s.3), duration: s.3, distanceMeters: s.2 * 1000, averageHeartRate: s.4, maxHeartRate: s.5, activeCalories: s.6, elevationGainMeters: s.8, isIndoor: s.9, sourceName: s.4 == nil ? "Sample app" : "Apple Watch (sample)" ) detailsByID[run.id] = RunDetails( route: s.9 ? [] : Self.loopRoute(start: start, duration: s.3, kilometres: s.2), totalSteps: s.7 ) runs.append(run) } return runs } func fetchDetails(for run: Run) async throws -> RunDetails { try await Task.sleep(for: .milliseconds(300)) return detailsByID[run.id] ?? RunDetails(route: [], totalSteps: nil) } /// A wobbly loop around Marina Bay, Singapore, roughly the run's length. private static func loopRoute(start: Date, duration: TimeInterval, kilometres: Double) -> [RoutePoint] { let centerLat = 1.2840, centerLon = 103.8590 let radiusKm = kilometres / (2 * Double.pi) let count = 240 return (0..