import SwiftUI // RunListView.swift // The runs list (grouped by month), plus the welcome screen and the // "no runs found" screen. struct RunListView: View { let runs: [Run] // newest first let source: any WorkoutSource let onRefresh: @MainActor () async -> Void var body: some View { List { if source.isSampleData { Section { Text("Sample runs (simulator only). On your iPhone, RunLog shows your real runs from Health.") .font(.footnote) .foregroundStyle(.secondary) } } ForEach(monthGroups) { group in Section(group.title) { ForEach(group.runs) { run in NavigationLink { RunDetailView(run: run, source: source) } label: { RunRow(run: run) } } } } } .navigationTitle("RunLog") .refreshable { await onRefresh() } } /// Runs grouped under "November 2026", "October 2026", … private var monthGroups: [MonthGroup] { var groups: [MonthGroup] = [] for run in runs { let title = run.startDate.formatted(.dateTime.month(.wide).year()) if groups.last?.title == title { groups[groups.count - 1].runs.append(run) } else { groups.append(MonthGroup(title: title, runs: [run])) } } return groups } } private struct MonthGroup: Identifiable { let title: String var runs: [Run] var id: String { title } } /// One line in the list: day, distance (big), time and pace. struct RunRow: View { let run: Run var body: some View { HStack(spacing: 14) { VStack(spacing: 2) { Text(run.startDate, format: .dateTime.day()) .font(.title2.weight(.semibold)) .fontWidth(.condensed) Text(run.startDate, format: .dateTime.weekday(.abbreviated)) .font(.caption) .foregroundStyle(.secondary) } .frame(width: 44) VStack(alignment: .leading, spacing: 4) { if let meters = run.distanceMeters { HStack(alignment: .firstTextBaseline, spacing: 3) { Text(RunFormat.kilometres(meters)) .font(.system(size: 30, weight: .bold)) .fontWidth(.condensed) Text("km") .font(.subheadline.weight(.semibold)) .foregroundStyle(.secondary) } } HStack(spacing: 12) { Text(RunFormat.duration(run.duration)) if let pace = run.paceSecondsPerKm { Text("\(RunFormat.pace(pace)) /km") } } .font(.subheadline) .foregroundStyle(.secondary) .monospacedDigit() } Spacer(minLength: 0) if run.isIndoor { Text("Indoor") .font(.caption) .foregroundStyle(.secondary) .padding(.horizontal, 6) .padding(.vertical, 2) .overlay(RoundedRectangle(cornerRadius: 6).stroke(.quaternary)) } } .padding(.vertical, 4) } } /// First-launch screen: explains what RunLog reads, then asks for Health access. struct ConnectHealthView: View { let onConnect: () -> Void var body: some View { VStack(spacing: 0) { Spacer() Image(systemName: "figure.run") .font(.system(size: 44, weight: .semibold)) .foregroundStyle(.white) .frame(width: 84, height: 84) .background(Color.lane, in: RoundedRectangle(cornerRadius: 20, style: .continuous)) Text("Welcome to RunLog") .font(.largeTitle.bold()) .padding(.top, 22) Text("Your running, your story.") .font(.title3) .foregroundStyle(.secondary) .padding(.top, 6) VStack(alignment: .leading, spacing: 14) { Label("Reads the runs your Apple Watch saves to Health: distance, time, heart rate, steps and route.", systemImage: "heart.text.square") Label("Everything stays on this iPhone. Nothing is uploaded.", systemImage: "lock") Label("RunLog never changes your Health data.", systemImage: "hand.raised") } .font(.callout) .foregroundStyle(.secondary) .padding(.top, 28) Spacer() Button(action: onConnect) { Text("Connect to Apple Health") .font(.headline) .frame(maxWidth: .infinity) .padding(.vertical, 6) } .buttonStyle(.borderedProminent) .controlSize(.large) Text("Next, iOS asks which data to share. Turn on all of them for the full picture.") .font(.footnote) .foregroundStyle(.secondary) .multilineTextAlignment(.center) .padding(.top, 12) } .padding(24) } } /// Shown when Health returned no runs — often because access was turned off. struct NoRunsView: View { let onCheckAgain: () -> Void var body: some View { ContentUnavailableView { Label("No runs found", systemImage: "figure.run") } description: { Text("RunLog shows running workouts saved in Health. If you have runs but see none, RunLog may not have permission: open Settings › Health › Data Access & Devices › RunLog and turn on all categories.") } actions: { Button("Check again", action: onCheckAgain) } .navigationTitle("RunLog") } } #Preview("Welcome") { ConnectHealthView {} }