| @@ -0,0 +1,194 @@ |
| 1 | import CoreLocation |
| 2 | import SkyEngine |
| 3 | import SkyRenderer |
| 4 | import SwiftUI |
| 5 | import WidgetKit |
| 6 | |
| 7 | @main |
| 8 | struct MoonriseWidgets: WidgetBundle { |
| 9 | var body: some Widget { |
| 10 | MoonWidget() |
| 11 | PlanetsWidget() |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | /// Last known location, or Chicago. The widget does not request a fresh fix. |
| 16 | func widgetObserver() -> (Observer, Bool) { |
| 17 | if let c = CLLocationManager().location?.coordinate { |
| 18 | return (Observer(latitude: c.latitude, longitude: c.longitude), true) |
| 19 | } |
| 20 | return (Observer(latitude: 41.8781, longitude: -87.6298), false) |
| 21 | } |
| 22 | |
| 23 | /// Astronomy Engine is not thread-safe and WidgetKit may build timelines concurrently. |
| 24 | let engineLock = NSLock() |
| 25 | |
| 26 | /// Hourly entries for the next 12 hours. |
| 27 | func hourly(from date: Date) -> [Date] { |
| 28 | (0..<12).map { date.addingTimeInterval(Double($0) * 3600) } |
| 29 | } |
| 30 | |
| 31 | // MARK: - Moon |
| 32 | |
| 33 | struct MoonEntry: TimelineEntry { |
| 34 | var date: Date |
| 35 | var illumination: Double |
| 36 | var waxing: Bool |
| 37 | var nextQuarter: (SkyEngine.MoonQuarter, Date)? |
| 38 | } |
| 39 | |
| 40 | struct MoonProvider: TimelineProvider { |
| 41 | func placeholder(in context: Context) -> MoonEntry { |
| 42 | MoonEntry(date: .now, illumination: 0.44, waxing: true, nextQuarter: nil) |
| 43 | } |
| 44 | |
| 45 | func getSnapshot(in context: Context, completion: @escaping (MoonEntry) -> Void) { |
| 46 | completion(entry(at: .now)) |
| 47 | } |
| 48 | |
| 49 | func getTimeline(in context: Context, completion: @escaping (Timeline<MoonEntry>) -> Void) { |
| 50 | completion(Timeline(entries: hourly(from: .now).map(entry), policy: .atEnd)) |
| 51 | } |
| 52 | |
| 53 | func entry(at date: Date) -> MoonEntry { |
| 54 | engineLock.lock() |
| 55 | defer { engineLock.unlock() } |
| 56 | let illumination = (try? SkyEngine.illumination(.moon, at: date).phaseFraction) ?? 0 |
| 57 | let phase = (try? SkyEngine.moonPhase(at: date)) ?? 0 |
| 58 | let q = try? SkyEngine.nextMoonQuarter(after: date) |
| 59 | return MoonEntry(date: date, illumination: illumination, waxing: phase < 180, nextQuarter: q.map { ($0.quarter, $0.date) }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func quarterName(_ q: SkyEngine.MoonQuarter) -> String { |
| 64 | switch q { |
| 65 | case .new: "New moon" |
| 66 | case .firstQuarter: "First quarter" |
| 67 | case .full: "Full moon" |
| 68 | case .lastQuarter: "Last quarter" |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | struct MoonWidgetView: View { |
| 73 | @Environment(\.widgetFamily) var family |
| 74 | var entry: MoonEntry |
| 75 | |
| 76 | var percent: String { "\(Int((entry.illumination * 100).rounded()))%" } |
| 77 | |
| 78 | var body: some View { |
| 79 | switch family { |
| 80 | case .accessoryCircular: |
| 81 | ZStack { |
| 82 | AccessoryWidgetBackground() |
| 83 | MoonDisc(fraction: entry.illumination, waxing: entry.waxing, light: .white, dark: .white.opacity(0.15)) |
| 84 | .padding(6) |
| 85 | } |
| 86 | case .accessoryInline: |
| 87 | Text("Moon \(percent) \(entry.waxing ? "waxing" : "waning")") |
| 88 | default: |
| 89 | VStack(alignment: .leading, spacing: 6) { |
| 90 | MoonDisc(fraction: entry.illumination, waxing: entry.waxing) |
| 91 | .frame(maxWidth: .infinity, alignment: .leading) |
| 92 | .frame(height: 64) |
| 93 | Spacer(minLength: 0) |
| 94 | Text("Moon \(percent)").font(.system(size: 15, weight: .medium)) |
| 95 | if let (q, d) = entry.nextQuarter { |
| 96 | Text("\(quarterName(q)) \(d.formatted(.dateTime.weekday(.abbreviated).hour().minute()))") |
| 97 | .font(.system(size: 11)).opacity(0.7) |
| 98 | } |
| 99 | } |
| 100 | .foregroundStyle(Color(red: 0.95, green: 0.95, blue: 0.93)) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | struct MoonWidget: Widget { |
| 106 | var body: some WidgetConfiguration { |
| 107 | StaticConfiguration(kind: "moon", provider: MoonProvider()) { entry in |
| 108 | MoonWidgetView(entry: entry) |
| 109 | .containerBackground(Color(red: 0.043, green: 0.078, blue: 0.141), for: .widget) |
| 110 | } |
| 111 | .configurationDisplayName("Moon") |
| 112 | .description("Tonight's moon phase.") |
| 113 | .supportedFamilies([.systemSmall, .accessoryCircular, .accessoryInline]) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // MARK: - Planets |
| 118 | |
| 119 | struct PlanetsEntry: TimelineEntry { |
| 120 | struct Row: Hashable { |
| 121 | var name: String |
| 122 | var detail: String |
| 123 | } |
| 124 | |
| 125 | var date: Date |
| 126 | var rows: [Row] |
| 127 | var located: Bool |
| 128 | } |
| 129 | |
| 130 | struct PlanetsProvider: TimelineProvider { |
| 131 | func placeholder(in context: Context) -> PlanetsEntry { |
| 132 | PlanetsEntry(date: .now, rows: [.init(name: "Saturn", detail: "up now, 30° SE")], located: true) |
| 133 | } |
| 134 | |
| 135 | func getSnapshot(in context: Context, completion: @escaping (PlanetsEntry) -> Void) { |
| 136 | completion(entry(at: .now)) |
| 137 | } |
| 138 | |
| 139 | func getTimeline(in context: Context, completion: @escaping (Timeline<PlanetsEntry>) -> Void) { |
| 140 | completion(Timeline(entries: hourly(from: .now).map(entry), policy: .atEnd)) |
| 141 | } |
| 142 | |
| 143 | func entry(at date: Date) -> PlanetsEntry { |
| 144 | let (observer, located) = widgetObserver() |
| 145 | engineLock.lock() |
| 146 | defer { engineLock.unlock() } |
| 147 | guard let t = try? Tonight(at: date, from: observer) else { return PlanetsEntry(date: date, rows: [], located: located) } |
| 148 | let time: (Date) -> String = { $0.formatted(date: .omitted, time: .shortened) } |
| 149 | let rows = t.planets.filter { $0.best != nil }.map { p -> PlanetsEntry.Row in |
| 150 | let name = p.body.rawValue.capitalized |
| 151 | if p.now.altitude > 0 { return .init(name: name, detail: String(format: "up, %.0f°", p.now.altitude)) } |
| 152 | if let rise = p.rise, rise < t.end { return .init(name: name, detail: "rises \(time(rise))") } |
| 153 | return .init(name: name, detail: "tonight") |
| 154 | } |
| 155 | return PlanetsEntry(date: date, rows: rows, located: located) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | struct PlanetsWidgetView: View { |
| 160 | var entry: PlanetsEntry |
| 161 | |
| 162 | var body: some View { |
| 163 | VStack(alignment: .leading, spacing: 4) { |
| 164 | Text("Planets tonight").font(.system(size: 11, weight: .medium)).textCase(.uppercase).opacity(0.6) |
| 165 | if entry.rows.isEmpty { |
| 166 | Text("None well placed").font(.system(size: 13)) |
| 167 | } |
| 168 | ForEach(entry.rows, id: \.self) { row in |
| 169 | HStack { |
| 170 | Text(row.name).font(.system(size: 14, weight: .medium)) |
| 171 | Spacer() |
| 172 | Text(row.detail).font(.system(size: 12).monospacedDigit()).opacity(0.75) |
| 173 | } |
| 174 | } |
| 175 | Spacer(minLength: 0) |
| 176 | if !entry.located { |
| 177 | Text("Open Moonrise to set location").font(.system(size: 10)).opacity(0.5) |
| 178 | } |
| 179 | } |
| 180 | .foregroundStyle(Color(red: 0.95, green: 0.95, blue: 0.93)) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | struct PlanetsWidget: Widget { |
| 185 | var body: some WidgetConfiguration { |
| 186 | StaticConfiguration(kind: "planets", provider: PlanetsProvider()) { entry in |
| 187 | PlanetsWidgetView(entry: entry) |
| 188 | .containerBackground(Color(red: 0.043, green: 0.078, blue: 0.141), for: .widget) |
| 189 | } |
| 190 | .configurationDisplayName("Planets tonight") |
| 191 | .description("Which planets are up tonight and when they rise.") |
| 192 | .supportedFamilies([.systemSmall, .systemMedium]) |
| 193 | } |
| 194 | } |