Commit e5cd77581e
Unsigned
Layout: unified · split
App/Widgets/HomeWidgets.swift added +148
| @@ -0,0 +1,148 @@ | ||
| 1 | import Answers | |
| 2 | import Feeds | |
| 3 | import ImageIO | |
| 4 | import SwiftUI | |
| 5 | import UIKit | |
| 6 | import WidgetKit | |
| 7 | ||
| 8 | private let night = Color(red: 0.043, green: 0.078, blue: 0.141) | |
| 9 | private let paper = Color(red: 0.95, green: 0.95, blue: 0.93) | |
| 10 | ||
| 11 | // MARK: - Voyager 1 | |
| 12 | ||
| 13 | struct VoyagerEntry: TimelineEntry { | |
| 14 | var date: Date | |
| 15 | var answer: ProbeAnswer? | |
| 16 | } | |
| 17 | ||
| 18 | /// Worked out on the device from the bundled trajectory, so no network. Hourly entries for a day. | |
| 19 | struct VoyagerProvider: TimelineProvider { | |
| 20 | func placeholder(in context: Context) -> VoyagerEntry { entry(at: .now) } | |
| 21 | ||
| 22 | func getSnapshot(in context: Context, completion: @escaping (VoyagerEntry) -> Void) { | |
| 23 | completion(entry(at: .now)) | |
| 24 | } | |
| 25 | ||
| 26 | func getTimeline(in context: Context, completion: @escaping (Timeline<VoyagerEntry>) -> Void) { | |
| 27 | completion(Timeline(entries: (0..<24).map { entry(at: .now.addingTimeInterval(Double($0) * 3600)) }, policy: .atEnd)) | |
| 28 | } | |
| 29 | ||
| 30 | func entry(at date: Date) -> VoyagerEntry { | |
| 31 | engineLock.lock() | |
| 32 | defer { engineLock.unlock() } | |
| 33 | return VoyagerEntry(date: date, answer: try? ProbeAnswer(probe: .voyager1, at: date)) | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | struct VoyagerWidgetView: View { | |
| 38 | var entry: VoyagerEntry | |
| 39 | ||
| 40 | var body: some View { | |
| 41 | VStack(alignment: .leading, spacing: 2) { | |
| 42 | Text("VOYAGER 1").font(.system(size: 11, weight: .medium)).tracking(0.6).opacity(0.7) | |
| 43 | Spacer(minLength: 0) | |
| 44 | if let a = entry.answer { | |
| 45 | Text((a.fromEarthKm / 1e9).formatted(.number.precision(.fractionLength(2)))) | |
| 46 | .font(.system(size: 30, weight: .medium)).monospacedDigit() | |
| 47 | Text("billion km from Earth").font(.system(size: 11)).opacity(0.8) | |
| 48 | Text("Light time \(Duration.seconds(a.lightTime).formatted(.units(allowed: [.hours, .minutes], width: .abbreviated)))") | |
| 49 | .font(.system(size: 11)).monospacedDigit().opacity(0.7) | |
| 50 | } else { | |
| 51 | Text("No trajectory data").font(.system(size: 13)) | |
| 52 | } | |
| 53 | } | |
| 54 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) | |
| 55 | .foregroundStyle(paper) | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | struct VoyagerWidget: Widget { | |
| 60 | var body: some WidgetConfiguration { | |
| 61 | StaticConfiguration(kind: "voyager1", provider: VoyagerProvider()) { entry in | |
| 62 | VoyagerWidgetView(entry: entry).containerBackground(night, for: .widget) | |
| 63 | } | |
| 64 | .configurationDisplayName("Voyager 1") | |
| 65 | .description("How far Voyager 1 is from Earth, and how long its signal takes. Trajectory: JPL Horizons.") | |
| 66 | .supportedFamilies([.systemSmall]) | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | // MARK: - Sun | |
| 71 | ||
| 72 | struct SunEntry: TimelineEntry { | |
| 73 | var date: Date | |
| 74 | var image: UIImage? | |
| 75 | /// When the frame was taken, for a frame older than SDO's latest. | |
| 76 | var frameTime: Date? | |
| 77 | } | |
| 78 | ||
| 79 | /// SDO's latest 171 Å frame, hourly; when it is black or partly covered, the newest usable frame from the archive. | |
| 80 | struct SunProvider: TimelineProvider { | |
| 81 | func placeholder(in context: Context) -> SunEntry { SunEntry(date: .now, image: nil) } | |
| 82 | ||
| 83 | func getSnapshot(in context: Context, completion: @escaping (SunEntry) -> Void) { | |
| 84 | if context.isPreview { return completion(placeholder(in: context)) } | |
| 85 | deliver(completion) { await Self.load() } | |
| 86 | } | |
| 87 | ||
| 88 | func getTimeline(in context: Context, completion: @escaping (Timeline<SunEntry>) -> Void) { | |
| 89 | deliver(completion) { | |
| 90 | let entry = await Self.load() | |
| 91 | return Timeline(entries: [entry], policy: .after(.now.addingTimeInterval(entry.image == nil ? 15 * 60 : 3600))) | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | static func load() async -> SunEntry { | |
| 96 | let w = SolarImage.corona | |
| 97 | if let (data, _) = try? await URLSession.shared.data(from: w.url(size: 512)), w.isUsable(data), let image = thumbnail(data) { | |
| 98 | return SunEntry(date: .now, image: image) | |
| 99 | } | |
| 100 | if let frame = await w.lastUsableFrame(), let image = thumbnail(frame.data) { | |
| 101 | return SunEntry(date: .now, image: image, frameTime: frame.time) | |
| 102 | } | |
| 103 | return SunEntry(date: .now, image: nil) | |
| 104 | } | |
| 105 | ||
| 106 | /// Decoded at 400 px, within a widget's memory limit whatever size the frame is. | |
| 107 | static func thumbnail(_ data: Data) -> UIImage? { | |
| 108 | guard let source = CGImageSourceCreateWithData(data as CFData, nil), | |
| 109 | let cg = CGImageSourceCreateThumbnailAtIndex(source, 0, [ | |
| 110 | kCGImageSourceCreateThumbnailFromImageAlways: true, | |
| 111 | kCGImageSourceThumbnailMaxPixelSize: 400, | |
| 112 | ] as CFDictionary) | |
| 113 | else { return nil } | |
| 114 | return UIImage(cgImage: cg) | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | struct SunWidgetView: View { | |
| 119 | var entry: SunEntry | |
| 120 | ||
| 121 | var body: some View { | |
| 122 | ZStack(alignment: .bottomLeading) { | |
| 123 | if let image = entry.image { | |
| 124 | Image(uiImage: image).resizable().scaledToFill() | |
| 125 | } | |
| 126 | VStack(alignment: .leading, spacing: 0) { | |
| 127 | Text("SUN · 171 Å").font(.system(size: 11, weight: .medium)).tracking(0.6) | |
| 128 | Text(entry.frameTime.map { "Frame \($0.formatted(date: .omitted, time: .shortened))" } ?? (entry.image == nil ? "No image" : "NASA/SDO")) | |
| 129 | .font(.system(size: 9)).opacity(0.8) | |
| 130 | } | |
| 131 | .foregroundStyle(paper) | |
| 132 | .shadow(color: .black, radius: 2) | |
| 133 | .padding(10) | |
| 134 | } | |
| 135 | } | |
| 136 | } | |
| 137 | ||
| 138 | struct SunWidget: Widget { | |
| 139 | var body: some WidgetConfiguration { | |
| 140 | StaticConfiguration(kind: "sun", provider: SunProvider()) { entry in | |
| 141 | SunWidgetView(entry: entry).containerBackground(.black, for: .widget) | |
| 142 | } | |
| 143 | .configurationDisplayName("Sun today") | |
| 144 | .description("The Sun's corona from NASA's Solar Dynamics Observatory, updated hourly. Courtesy of NASA/SDO and the AIA, EVE, and HMI science teams.") | |
| 145 | .supportedFamilies([.systemSmall]) | |
| 146 | .contentMarginsDisabled() | |
| 147 | } | |
| 148 | } | |
App/Widgets/MoonriseWidgets.swift +2
| @@ -12,6 +12,8 @@ struct MoonriseWidgets: WidgetBundle { | ||
| 12 | 12 | #if os(iOS) |
| 13 | 13 | KpWidget() |
| 14 | 14 | NextLaunchWidget() |
| 15 | VoyagerWidget() | |
| 16 | SunWidget() | |
| 15 | 17 | LaunchLiveActivity() |
| 16 | 18 | EventLiveActivity() |
| 17 | 19 | EclipseLiveActivity() |