| @@ -0,0 +1,98 @@ |
| 1 | import SkyEngine |
| 2 | import SwiftUI |
| 3 | import WatchKit |
| 4 | |
| 5 | /// A total or annular eclipse at the Watch's position within two weeks, and its second and third contacts. |
| 6 | struct WatchEclipse: Sendable, Equatable { |
| 7 | var kind: String |
| 8 | var c2: Date |
| 9 | var c3: Date |
| 10 | |
| 11 | static func next(from date: Date, observer: Observer) -> WatchEclipse? { |
| 12 | let events = SkyEngine.events(from: date, days: 14, observer: observer) |
| 13 | for e in events where e.kind == .solarEclipse && e.visibleHere == true { |
| 14 | if let l = LocalEclipse.at(observer, near: e.date), let c2 = l.centralBegin, let c3 = l.centralEnd, c3 > date { |
| 15 | return WatchEclipse(kind: l.kind, c2: c2, c3: c3) |
| 16 | } |
| 17 | } |
| 18 | return nil |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /// Taps the wrist at second and third contact. A smart alarm session may start in the background at a set time |
| 23 | /// (within 36 hours) and run for 30 minutes, so it is scheduled a minute before second contact. |
| 24 | @MainActor |
| 25 | @Observable |
| 26 | final class EclipseHaptics: NSObject, WKExtendedRuntimeSessionDelegate { |
| 27 | private(set) var armed: Date? |
| 28 | private var session: WKExtendedRuntimeSession? |
| 29 | private var eclipse: WatchEclipse? |
| 30 | |
| 31 | func arm(_ e: WatchEclipse) { |
| 32 | session?.invalidate() |
| 33 | eclipse = e |
| 34 | let s = WKExtendedRuntimeSession() |
| 35 | s.delegate = self |
| 36 | s.start(at: max(.now.addingTimeInterval(5), e.c2.addingTimeInterval(-60))) |
| 37 | session = s |
| 38 | armed = e.c2 |
| 39 | } |
| 40 | |
| 41 | func disarm() { |
| 42 | session?.invalidate() |
| 43 | session = nil |
| 44 | armed = nil |
| 45 | } |
| 46 | |
| 47 | nonisolated func extendedRuntimeSessionDidStart(_ started: WKExtendedRuntimeSession) { |
| 48 | let id = ObjectIdentifier(started) |
| 49 | Task { @MainActor in |
| 50 | guard let e = eclipse, let s = session, ObjectIdentifier(s) == id else { return } |
| 51 | for contact in [e.c2, e.c3] where contact > .now { |
| 52 | try? await Task.sleep(for: .seconds(contact.timeIntervalSinceNow)) |
| 53 | s.notifyUser(hapticType: .notification) { _ in 0 } |
| 54 | } |
| 55 | s.invalidate() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | nonisolated func extendedRuntimeSessionWillExpire(_ s: WKExtendedRuntimeSession) {} |
| 60 | |
| 61 | nonisolated func extendedRuntimeSession(_ ended: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) { |
| 62 | let id = ObjectIdentifier(ended) |
| 63 | Task { @MainActor in |
| 64 | if session.map({ ObjectIdentifier($0) == id }) == true { session = nil; armed = nil } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Contact times and the switch for the wrist taps. |
| 70 | struct EclipseSection: View { |
| 71 | var eclipse: WatchEclipse |
| 72 | @Bindable var haptics: EclipseHaptics |
| 73 | |
| 74 | var body: some View { |
| 75 | let central = eclipse.kind == "Total" ? "Totality" : "Annularity" |
| 76 | VStack(alignment: .leading, spacing: 4) { |
| 77 | Text("\(eclipse.kind) eclipse").font(.footnote.weight(.medium)).foregroundStyle(.secondary) |
| 78 | row("\(central) begins", eclipse.c2) |
| 79 | row("\(central) ends", eclipse.c3) |
| 80 | if haptics.armed == eclipse.c2 { |
| 81 | Button("Stop wrist taps") { haptics.disarm() } |
| 82 | Text("Taps at both contacts, with the app closed.").font(.caption2).foregroundStyle(.secondary) |
| 83 | } else { |
| 84 | Button("Tap at both contacts") { haptics.arm(eclipse) } |
| 85 | Text("Times are for the Watch's position. Taps only: follow your glasses' guidance.").font(.caption2).foregroundStyle(.secondary) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private func row(_ label: String, _ d: Date) -> some View { |
| 91 | HStack { |
| 92 | Text(label) |
| 93 | Spacer() |
| 94 | Text(d.formatted(date: .omitted, time: .standard)).monospacedDigit().foregroundStyle(.secondary) |
| 95 | } |
| 96 | .font(.footnote) |
| 97 | } |
| 98 | } |