Commit cb314119b9
Unsigned
Layout: unified · split
App/Sources/Moonrise.entitlements +4
| @@ -6,5 +6,9 @@ | |||
| 6 | <string>development</string> | 6 | <string>development</string> |
| 7 | <key>com.apple.developer.weatherkit</key> | 7 | <key>com.apple.developer.weatherkit</key> |
| 8 | <true/> | 8 | <true/> |
| 9 | <key>com.apple.security.application-groups</key> | ||
| 10 | <array> | ||
| 11 | <string>group.sh.krz.moonrise</string> | ||
| 12 | </array> | ||
| 9 | </dict> | 13 | </dict> |
| 10 | </plist> | 14 | </plist> |
App/Sources/SkyModel.swift +6 −2
| @@ -120,7 +120,9 @@ final class SkyModel: NSObject, CLLocationManagerDelegate { | |||
| 120 | observer = Observer(latitude: lat, longitude: lon) | 120 | observer = Observer(latitude: lat, longitude: lon) |
| 121 | placeName = d.string(forKey: SettingsKey.cityName) ?? "Chicago" | 121 | placeName = d.string(forKey: SettingsKey.cityName) ?? "Chicago" |
| 122 | hasLocation = true | 122 | hasLocation = true |
| 123 | Place(latitude: lat, longitude: lon, name: placeName, kind: .city).save(to: d) | 123 | let place = Place(latitude: lat, longitude: lon, name: placeName, kind: .city) |
| 124 | place.save(to: d) | ||
| 125 | Place.shared.map(place.save) | ||
| 124 | } else { | 126 | } else { |
| 125 | if !hasLocation { placeName = "Chicago (location unavailable)" } | 127 | if !hasLocation { placeName = "Chicago (location unavailable)" } |
| 126 | manager.requestWhenInUseAuthorization() | 128 | manager.requestWhenInUseAuthorization() |
| @@ -219,7 +221,9 @@ final class SkyModel: NSObject, CLLocationManagerDelegate { | |||
| 219 | self.observer = Observer(latitude: c.latitude, longitude: c.longitude) | 221 | self.observer = Observer(latitude: c.latitude, longitude: c.longitude) |
| 220 | self.hasLocation = true | 222 | self.hasLocation = true |
| 221 | self.placeName = "Your location" | 223 | self.placeName = "Your location" |
| 222 | Place(latitude: c.latitude, longitude: c.longitude, name: self.placeName, kind: .located).save(to: .standard) | 224 | let place = Place(latitude: c.latitude, longitude: c.longitude, name: self.placeName, kind: .located) |
| 225 | place.save(to: .standard) | ||
| 226 | Place.shared.map(place.save) | ||
| 223 | self.refresh() | 227 | self.refresh() |
| 224 | } | 228 | } |
| 225 | } | 229 | } |
App/Widgets/FeedWidgets.swift added +167
| @@ -0,0 +1,167 @@ | |||
| 1 | import Answers | ||
| 2 | import Feeds | ||
| 3 | import SkyEngine | ||
| 4 | import SwiftUI | ||
| 5 | import WidgetKit | ||
| 6 | |||
| 7 | /// Backend base URL from Info.plist (MOONRISE_API_URL build setting). | ||
| 8 | let widgetAPIBase: URL? = (Bundle.main.object(forInfoDictionaryKey: "MoonriseAPIURL") as? String) | ||
| 9 | .flatMap { $0.isEmpty ? nil : URL(string: $0) } | ||
| 10 | |||
| 11 | /// The place the Sky tab last used (a fix or the city chosen in Settings), shared through the App Group. | ||
| 12 | func sharedPlace() -> Place? { | ||
| 13 | Place.shared.flatMap(Place.saved) | ||
| 14 | } | ||
| 15 | |||
| 16 | /// Runs `work` and hands its result to a WidgetKit completion handler, which is not Sendable. | ||
| 17 | func deliver<T>(_ completion: @escaping (T) -> Void, _ work: @escaping @Sendable () async -> T) { | ||
| 18 | nonisolated(unsafe) let completion = completion | ||
| 19 | Task { completion(await work()) } | ||
| 20 | } | ||
| 21 | |||
| 22 | // MARK: - Kp | ||
| 23 | |||
| 24 | struct KpEntry: TimelineEntry { | ||
| 25 | var date: Date | ||
| 26 | var weather: SpaceWeather? | ||
| 27 | } | ||
| 28 | |||
| 29 | struct KpProvider: TimelineProvider { | ||
| 30 | func placeholder(in context: Context) -> KpEntry { | ||
| 31 | KpEntry(date: .now, weather: SpaceWeather(kpNow: 2.33, observedAt: .now, kpNext24h: 4)) | ||
| 32 | } | ||
| 33 | |||
| 34 | func getSnapshot(in context: Context, completion: @escaping (KpEntry) -> Void) { | ||
| 35 | if context.isPreview { return completion(placeholder(in: context)) } | ||
| 36 | deliver(completion) { KpEntry(date: .now, weather: try? await SpaceWeather.fetch(base: widgetAPIBase)) } | ||
| 37 | } | ||
| 38 | |||
| 39 | /// The backend polls SWPC every 5 minutes; widgets refresh every 30, or 15 after a failure. | ||
| 40 | func getTimeline(in context: Context, completion: @escaping (Timeline<KpEntry>) -> Void) { | ||
| 41 | deliver(completion) { | ||
| 42 | let weather = try? await SpaceWeather.fetch(base: widgetAPIBase) | ||
| 43 | let next = Date.now.addingTimeInterval(weather == nil ? 15 * 60 : 30 * 60) | ||
| 44 | return Timeline(entries: [KpEntry(date: .now, weather: weather)], policy: .after(next)) | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | struct KpWidgetView: View { | ||
| 50 | @Environment(\.widgetFamily) var family | ||
| 51 | var entry: KpEntry | ||
| 52 | |||
| 53 | private func kp(_ v: Double) -> String { v.formatted(.number.precision(.fractionLength(1))) } | ||
| 54 | |||
| 55 | var body: some View { | ||
| 56 | if let w = entry.weather { | ||
| 57 | switch family { | ||
| 58 | case .accessoryCircular: | ||
| 59 | Gauge(value: min(w.kpNow, 9), in: 0...9) { Text("Kp") } currentValueLabel: { Text(kp(w.kpNow)) } | ||
| 60 | .gaugeStyle(.accessoryCircular) | ||
| 61 | case .accessoryInline: | ||
| 62 | Text("Kp \(kp(w.kpNow))" + (w.kpNext24h.map { " · up to \(kp($0)) today" } ?? "")) | ||
| 63 | default: | ||
| 64 | VStack(alignment: .leading, spacing: 1) { | ||
| 65 | Text("KP INDEX").font(.system(size: 11, weight: .medium)).tracking(0.6).widgetAccentable() | ||
| 66 | Text("\(kp(w.kpNow)) now").font(.system(size: 17, weight: .medium)).monospacedDigit() | ||
| 67 | if let next = w.kpNext24h { | ||
| 68 | Text("Up to \(kp(next)) in 24 h" + (SpaceWeather.gScale(next).map { ", G\($0)" } ?? "")) | ||
| 69 | .font(.system(size: 13)).monospacedDigit() | ||
| 70 | } | ||
| 71 | } | ||
| 72 | .frame(maxWidth: .infinity, alignment: .leading) | ||
| 73 | } | ||
| 74 | } else { | ||
| 75 | Text(family == .accessoryCircular ? "Kp" : "Kp unavailable") | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | struct KpWidget: Widget { | ||
| 81 | var body: some WidgetConfiguration { | ||
| 82 | StaticConfiguration(kind: "kp", provider: KpProvider()) { entry in | ||
| 83 | KpWidgetView(entry: entry).containerBackground(.clear, for: .widget) | ||
| 84 | } | ||
| 85 | .configurationDisplayName("Kp index") | ||
| 86 | .description("Geomagnetic activity now and the highest forecast in the next 24 hours. Data: NOAA SWPC.") | ||
| 87 | .supportedFamilies([.accessoryCircular, .accessoryInline, .accessoryRectangular]) | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | // MARK: - Next launch | ||
| 92 | |||
| 93 | struct LaunchEntry: TimelineEntry { | ||
| 94 | var date: Date | ||
| 95 | var launch: Launch? | ||
| 96 | } | ||
| 97 | |||
| 98 | struct NextLaunchProvider: TimelineProvider { | ||
| 99 | func placeholder(in context: Context) -> LaunchEntry { LaunchEntry(date: .now, launch: nil) } | ||
| 100 | |||
| 101 | func getSnapshot(in context: Context, completion: @escaping (LaunchEntry) -> Void) { | ||
| 102 | deliver(completion) { LaunchEntry(date: .now, launch: await Self.next()) } | ||
| 103 | } | ||
| 104 | |||
| 105 | /// Hourly, and again 15 minutes after the launch so the next one replaces it. | ||
| 106 | func getTimeline(in context: Context, completion: @escaping (Timeline<LaunchEntry>) -> Void) { | ||
| 107 | deliver(completion) { | ||
| 108 | let launch = await Self.next() | ||
| 109 | var refresh = Date.now.addingTimeInterval(3600) | ||
| 110 | if let net = launch?.net { refresh = min(refresh, net.addingTimeInterval(15 * 60)) } | ||
| 111 | return Timeline(entries: [LaunchEntry(date: .now, launch: launch)], policy: .after(refresh)) | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | private static func next() async -> Launch? { | ||
| 116 | guard let base = widgetAPIBase, let list = try? await LaunchFeed.fetch(base: base) else { return nil } | ||
| 117 | return list.launches.first { $0.net > .now.addingTimeInterval(-15 * 60) } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | struct NextLaunchWidgetView: View { | ||
| 122 | @Environment(\.widgetFamily) var family | ||
| 123 | var entry: LaunchEntry | ||
| 124 | |||
| 125 | var body: some View { | ||
| 126 | if let l = entry.launch { | ||
| 127 | let soon = l.net.timeIntervalSince(entry.date) < 24 * 3600 | ||
| 128 | switch family { | ||
| 129 | case .accessoryInline: | ||
| 130 | if soon { | ||
| 131 | Text("\(l.rocket) \(Text(l.net, style: .relative))") | ||
| 132 | } else { | ||
| 133 | Text("\(l.rocket) \(l.net.formatted(.dateTime.weekday(.abbreviated).hour().minute()))") | ||
| 134 | } | ||
| 135 | default: | ||
| 136 | VStack(alignment: .leading, spacing: 1) { | ||
| 137 | Text(l.rocket).font(.system(size: 15, weight: .medium)).lineLimit(1).widgetAccentable() | ||
| 138 | Text(l.mission ?? l.name).font(.system(size: 13)).lineLimit(1) | ||
| 139 | if soon { | ||
| 140 | HStack(spacing: 2) { | ||
| 141 | Text(l.net > entry.date ? "T−" : "T+") | ||
| 142 | Text(l.net, style: .timer) | ||
| 143 | } | ||
| 144 | .font(.system(size: 13, weight: .medium)).monospacedDigit() | ||
| 145 | } else { | ||
| 146 | Text(l.net.formatted(.dateTime.weekday(.abbreviated).day().month(.abbreviated).hour().minute())) | ||
| 147 | .font(.system(size: 13)).monospacedDigit() | ||
| 148 | } | ||
| 149 | } | ||
| 150 | .frame(maxWidth: .infinity, alignment: .leading) | ||
| 151 | } | ||
| 152 | } else { | ||
| 153 | Text("No launch data") | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | struct NextLaunchWidget: Widget { | ||
| 159 | var body: some WidgetConfiguration { | ||
| 160 | StaticConfiguration(kind: "next-launch", provider: NextLaunchProvider()) { entry in | ||
| 161 | NextLaunchWidgetView(entry: entry).containerBackground(.clear, for: .widget) | ||
| 162 | } | ||
| 163 | .configurationDisplayName("Next launch") | ||
| 164 | .description("The next rocket launch with a countdown. Data: Launch Library 2.") | ||
| 165 | .supportedFamilies([.accessoryInline, .accessoryRectangular]) | ||
| 166 | } | ||
| 167 | } | ||
App/Widgets/Info.plist +2
| @@ -20,6 +20,8 @@ | |||
| 20 | <string>$(MARKETING_VERSION)</string> | 20 | <string>$(MARKETING_VERSION)</string> |
| 21 | <key>CFBundleVersion</key> | 21 | <key>CFBundleVersion</key> |
| 22 | <string>$(CURRENT_PROJECT_VERSION)</string> | 22 | <string>$(CURRENT_PROJECT_VERSION)</string> |
| 23 | <key>MoonriseAPIURL</key> | ||
| 24 | <string>$(MOONRISE_API_URL)</string> | ||
| 23 | <key>NSExtension</key> | 25 | <key>NSExtension</key> |
| 24 | <dict> | 26 | <dict> |
| 25 | <key>NSExtensionPointIdentifier</key> | 27 | <key>NSExtensionPointIdentifier</key> |
App/Widgets/MoonriseWidgets.entitlements added +10
| @@ -0,0 +1,10 @@ | |||
| 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>com.apple.security.application-groups</key> | ||
| 6 | <array> | ||
| 7 | <string>group.sh.krz.moonrise</string> | ||
| 8 | </array> | ||
| 9 | </dict> | ||
| 10 | </plist> | ||
App/Widgets/MoonriseWidgets.swift +7 −1
| @@ -10,6 +10,8 @@ struct MoonriseWidgets: WidgetBundle { | |||
| 10 | MoonWidget() | 10 | MoonWidget() |
| 11 | PlanetsWidget() | 11 | PlanetsWidget() |
| 12 | #if os(iOS) | 12 | #if os(iOS) |
| 13 | KpWidget() | ||
| 14 | NextLaunchWidget() | ||
| 13 | LaunchLiveActivity() | 15 | LaunchLiveActivity() |
| 14 | EventLiveActivity() | 16 | EventLiveActivity() |
| 15 | EclipseLiveActivity() | 17 | EclipseLiveActivity() |
| @@ -18,8 +20,12 @@ struct MoonriseWidgets: WidgetBundle { | |||
| 18 | } | 20 | } |
| 19 | } | 21 | } |
| 20 | 22 | ||
| 21 | /// Last known location, or Chicago. The widget does not request a fresh fix. | 23 | /// The Sky tab's place (iPhone, through the App Group), else the last known location, else Chicago. |
| 24 | /// The widget does not request a fresh fix. | ||
| 22 | func widgetObserver() -> (Observer, Bool) { | 25 | func widgetObserver() -> (Observer, Bool) { |
| 26 | #if os(iOS) | ||
| 27 | if let p = sharedPlace(), p.kind != .fallback { return (p.observer, true) } | ||
| 28 | #endif | ||
| 23 | if let c = CLLocationManager().location?.coordinate { | 29 | if let c = CLLocationManager().location?.coordinate { |
| 24 | return (Observer(latitude: c.latitude, longitude: c.longitude), true) | 30 | return (Observer(latitude: c.latitude, longitude: c.longitude), true) |
| 25 | } | 31 | } |
App/project.yml +15
| @@ -41,6 +41,7 @@ targets: | |||
| 41 | properties: | 41 | properties: |
| 42 | aps-environment: development | 42 | aps-environment: development |
| 43 | com.apple.developer.weatherkit: true | 43 | com.apple.developer.weatherkit: true |
| 44 | com.apple.security.application-groups: [group.sh.krz.moonrise] | ||
| 44 | info: | 45 | info: |
| 45 | path: Sources/Info.plist | 46 | path: Sources/Info.plist |
| 46 | properties: | 47 | properties: |
| @@ -82,8 +83,16 @@ targets: | |||
| 82 | product: SkyEngine | 83 | product: SkyEngine |
| 83 | - package: MoonriseKit | 84 | - package: MoonriseKit |
| 84 | product: SkyRenderer | 85 | product: SkyRenderer |
| 86 | - package: MoonriseKit | ||
| 87 | product: Feeds | ||
| 88 | - package: MoonriseKit | ||
| 89 | product: Answers | ||
| 85 | - sdk: WidgetKit.framework | 90 | - sdk: WidgetKit.framework |
| 86 | - sdk: SwiftUI.framework | 91 | - sdk: SwiftUI.framework |
| 92 | entitlements: | ||
| 93 | path: Widgets/MoonriseWidgets.entitlements | ||
| 94 | properties: | ||
| 95 | com.apple.security.application-groups: [group.sh.krz.moonrise] | ||
| 87 | info: | 96 | info: |
| 88 | path: Widgets/Info.plist | 97 | path: Widgets/Info.plist |
| 89 | properties: | 98 | properties: |
| @@ -91,9 +100,15 @@ targets: | |||
| 91 | CFBundleShortVersionString: $(MARKETING_VERSION) | 100 | CFBundleShortVersionString: $(MARKETING_VERSION) |
| 92 | CFBundleVersion: $(CURRENT_PROJECT_VERSION) | 101 | CFBundleVersion: $(CURRENT_PROJECT_VERSION) |
| 93 | NSWidgetWantsLocation: true | 102 | NSWidgetWantsLocation: true |
| 103 | MoonriseAPIURL: $(MOONRISE_API_URL) | ||
| 94 | NSExtension: | 104 | NSExtension: |
| 95 | NSExtensionPointIdentifier: com.apple.widgetkit-extension | 105 | NSExtensionPointIdentifier: com.apple.widgetkit-extension |
| 96 | settings: | 106 | settings: |
| 107 | configs: | ||
| 108 | Debug: | ||
| 109 | MOONRISE_API_URL: https://moonrise.krz.sh | ||
| 110 | Release: | ||
| 111 | MOONRISE_API_URL: https://moonrise.krz.sh | ||
| 97 | base: | 112 | base: |
| 98 | PRODUCT_BUNDLE_IDENTIFIER: sh.krz.moonrise.widgets | 113 | PRODUCT_BUNDLE_IDENTIFIER: sh.krz.moonrise.widgets |
| 99 | DEVELOPMENT_TEAM: ZCNAX3VL9D | 114 | DEVELOPMENT_TEAM: ZCNAX3VL9D |
Packages/MoonriseKit/Sources/Answers/Place.swift +3
| @@ -44,6 +44,9 @@ public struct Place: Codable, Sendable, Equatable { | |||
| 44 | 44 | ||
| 45 | static let key = "answersPlace" | 45 | static let key = "answersPlace" |
| 46 | 46 | ||
| 47 | /// Shared with the widgets, which cannot read the app's own defaults. | ||
| 48 | public static var shared: UserDefaults? { UserDefaults(suiteName: "group.sh.krz.moonrise") } | ||
| 49 | |||
| 47 | public static func saved(in defaults: UserDefaults) -> Place? { | 50 | public static func saved(in defaults: UserDefaults) -> Place? { |
| 48 | defaults.data(forKey: key).flatMap { try? JSONDecoder().decode(Place.self, from: $0) } | 51 | defaults.data(forKey: key).flatMap { try? JSONDecoder().decode(Place.self, from: $0) } |
| 49 | } | 52 | } |
Packages/MoonriseKit/Sources/Feeds/SpaceWeather.swift +6
| @@ -8,6 +8,12 @@ public struct SpaceWeather: Sendable, Equatable { | |||
| 8 | /// Highest predicted 3-hour Kp in the next 24 hours, if any. | 8 | /// Highest predicted 3-hour Kp in the next 24 hours, if any. |
| 9 | public var kpNext24h: Double? | 9 | public var kpNext24h: Double? |
| 10 | 10 | ||
| 11 | public init(kpNow: Double, observedAt: Date, kpNext24h: Double?) { | ||
| 12 | self.kpNow = kpNow | ||
| 13 | self.observedAt = observedAt | ||
| 14 | self.kpNext24h = kpNext24h | ||
| 15 | } | ||
| 16 | |||
| 11 | public static let nowURL = URL(string: "https://services.swpc.noaa.gov/json/planetary_k_index_1m.json")! | 17 | public static let nowURL = URL(string: "https://services.swpc.noaa.gov/json/planetary_k_index_1m.json")! |
| 12 | public static let forecastURL = URL(string: "https://services.swpc.noaa.gov/products/noaa-planetary-k-index-forecast.json")! | 18 | public static let forecastURL = URL(string: "https://services.swpc.noaa.gov/products/noaa-planetary-k-index-forecast.json")! |
| 13 | 19 | ||