krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Commit 924c91d1e5

924c91d1e5c7392615f4e3ccf5b6bdbd5d8c58c9

parent: c455497599

Unsigned

cmc <hello@cleberg.net> · 2026-10-22 03:19 UTC

Watch: wrist taps at second and third contact (!113)

- `WatchEclipse.next`: a solar eclipse visible here within 14 days (`SkyEngine.events`), kept if `LocalEclipse` has a central phase at the Watch's position.
- `EclipseHaptics`: `WKExtendedRuntimeSession` (smart alarm) scheduled to start at C2 − 60 s (within its 36-hour window); in the session, `notifyUser(hapticType: .notification)` at C2 and C3, then it ends. Works with the app closed.
- Tonight view: eclipse section with C2/C3 times and "Tap at both contacts" / "Stop wrist taps". No glasses advice on the Watch: "Taps only: follow your glasses' guidance."
- Watch Info.plist: `WKBackgroundModes: [alarm]`. App Review may ask about the smart-alarm use.
- Detection checked: Tarifa, 26 July 2027 → total, C2 08:45:09, C3 08:49:48 UTC; Buenos Aires before 6 Feb 2027 → partial, no section. watchOS builds. Haptics not tried on a Watch.

Layout: unified · split

App/Watch/EclipseHaptics.swift added +98
@@ -0,0 +1,98 @@
1import SkyEngine
2import SwiftUI
3import WatchKit
4
5/// A total or annular eclipse at the Watch's position within two weeks, and its second and third contacts.
6struct 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
26final 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.
70struct 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}
App/Watch/Info.plist added +26
@@ -0,0 +1,26 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3<plist version="1.0">
4<dict>
5 <key>CFBundleDevelopmentRegion</key>
6 <string>$(DEVELOPMENT_LANGUAGE)</string>
7 <key>CFBundleExecutable</key>
8 <string>$(EXECUTABLE_NAME)</string>
9 <key>CFBundleIdentifier</key>
10 <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11 <key>CFBundleInfoDictionaryVersion</key>
12 <string>6.0</string>
13 <key>CFBundleName</key>
14 <string>$(PRODUCT_NAME)</string>
15 <key>CFBundlePackageType</key>
16 <string>APPL</string>
17 <key>CFBundleShortVersionString</key>
18 <string>1.0</string>
19 <key>CFBundleVersion</key>
20 <string>1</string>
21 <key>WKBackgroundModes</key>
22 <array>
23 <string>alarm</string>
24 </array>
25</dict>
26</plist>
App/Watch/MoonriseWatchApp.swift +4
@@ -14,6 +14,8 @@ struct MoonriseWatchApp: App {
1414struct TonightView: View {
1515 @State private var location = WatchLocation()
1616 @State private var tonight: Tonight?
17 @State private var eclipse: WatchEclipse?
18 @State private var haptics = EclipseHaptics()
1719
1820 var body: some View {
1921 ScrollView {
@@ -29,6 +31,7 @@ struct TonightView: View {
2931 }
3032 }
3133 .accessibilityElement(children: .combine)
34 if let eclipse { EclipseSection(eclipse: eclipse, haptics: haptics).padding(.vertical, 4) }
3235 if let s = t.sunset { row("Sunset", time(s)) }
3336 if let r = t.sunrise { row("Sunrise", time(r)) }
3437 Text("Planets").font(.footnote.weight(.medium)).foregroundStyle(.secondary).padding(.top, 4)
@@ -50,6 +53,7 @@ struct TonightView: View {
5053 .task(id: location.observer) {
5154 let observer = location.observer
5255 tonight = await Task.detached { try? Tonight(at: .now, from: observer) }.value
56 eclipse = await Task.detached { WatchEclipse.next(from: .now, observer: observer) }.value
5357 }
5458 .onAppear { location.start() }
5559 }
App/project.yml +4
@@ -122,6 +122,10 @@ targets:
122122 type: application
123123 platform: watchOS
124124 sources: [Watch]
125 info:
126 path: Watch/Info.plist
127 properties:
128 WKBackgroundModes: [alarm]
125129 dependencies:
126130 - package: MoonriseKit
127131 product: SkyEngine