| Code | What It Does | How It Does It |
| ▶ IMPORTS | | |
| import SwiftUI
import CoreLocation | Framework imports | Imports SwiftUI, CoreLocation. |
| struct WatchStatusView: View {
@EnvironmentObject var store: WatchEntryStore
@StateObject private var alerts = WatchAlertsService.shared
@StateObject private var arrivals = WatchArrivalsService.shared
// Lines present at the nearest stations (populated by WatchArrivalsService)
private var nearbyLines: Set<String> {
Set(arrivals.stations.flatMap { $0.lines })
}
private var localAlerts: [WatchSubwayAlert] {
guard !nearbyLines.isEmpty else { return [] }
return alerts.alerts.filter { !Set($0.affectedRoutes).isDisjoint(with: nearbyLines) }
}
private var otherAlerts: [WatchSubwayAlert] {
guard !nearbyLines.isEmpty else { return alerts.alerts }
return alerts.alerts.filter { Set($0.affectedRoutes).isDisjoint(with: nearbyLines) }
}
var body: some View {
Group {
if alerts.isLoading && alerts.alerts.isEmpty {
VStack(spacing: 10) {
ProgressView()
Text("Loading alerts…")
.font(.caption)
.foregroundColor(.secondary)
}
} else if alerts.alerts.isEmpty {
VStack(spacing: 8) {
Image(systemName: "checkmark.circle")
.font(.system(size: 32))
.foregroundColor(.green)
Text("No active alerts")
.font(.body)
.foregroundColor(.secondary)
}
} else {
List {
// ── Nearby lines ──────────────────────────────────────
if !localAlerts.isEmpty {
Section {
ForEach(localAlerts) { alert in
WatchAlertRow(alert: alert)
.listRowBackground(Color.white.opacity(0.07))
}
}
}
// ── Other lines ───────────────────────────────────────
if !otherAlerts.isEmpty {
Section(header:
Text(nearbyLines.isEmpty ? "" : "Other Lines")
.font(.system(size: 11, weight: .semibold))
.foregroundColor(.secondary)
) {
ForEach(otherAlerts) { alert in
WatchAlertRow(alert: alert)
.listRowBackground(Color.white.opacity(0.07))
}
}
}
}
.listStyle(.plain)
}
}
.navigationTitle("Status")
.navigationBarTitleDisplayMode(.inline)
.task { await fetchAll() }
.refreshable { await fetchAll() }
}
private func fetchAll() async {
// Fetch alerts always; fetch arrivals only if we don't have location data yet
async let a: () = alerts.fetch()
async let b: () = fetchArrivalsIfNeeded()
_ = await (a, b)
}
private func fetchArrivalsIfNeeded() async {
guard arrivals.stations.isEmpty else { return }
store.startLocationIfNeeded()
var waited = 0
while store.userLocation == nil && waited < 10 {
try? await Task.sleep(nanoseconds: 500_000_000)
waited += 1
}
let loc = store.userLocation ?? CLLocation(latitude: 40.7128, longitude: -74.0060)
await arrivals.refresh(near: loc)
}
} | `WatchStatusView` struct | Defines the `WatchStatusView` struct. Conforms to View. |
| #Preview {
NavigationStack { WatchStatusView() }
.environmentObject(WatchEntryStore())
} | Code block | See source code for full implementation. |