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

App/Widgets/EventLiveActivity.swift

v1.0.0
moonrise/App/Widgets/EventLiveActivity.swift history · blame · raw

49 lines · 2284 bytes

 1import ActivityKit
 2import SwiftUI
 3import WidgetKit
 4
 5struct EventLiveActivity: Widget {
 6    private let ink = Color(red: 0.95, green: 0.95, blue: 0.93)
 7
 8    var body: some WidgetConfiguration {
 9        ActivityConfiguration(for: EventActivityAttributes.self) { context in
10            VStack(alignment: .leading, spacing: 4) {
11                HStack {
12                    Text(context.attributes.title).font(.system(size: 17, weight: .medium))
13                    Spacer()
14                    Text(context.attributes.launch, style: .timer).font(.system(size: 17, weight: .medium).monospacedDigit())
15                        .multilineTextAlignment(.trailing)
16                }
17                Text(context.state.phase).font(.system(size: 14))
18                HStack {
19                    Text("\(Int(context.state.distanceKm).formatted()) km from Earth").font(.system(size: 12)).opacity(0.8)
20                    Spacer()
21                    if let next = context.state.next, let at = context.state.nextAt {
22                        Text("\(next) \(at, style: .relative)").font(.system(size: 12)).opacity(0.8).lineLimit(1)
23                    }
24                }
25            }
26            .padding(16)
27            .foregroundStyle(ink)
28            .activityBackgroundTint(Color(red: 0.043, green: 0.078, blue: 0.141))
29        } dynamicIsland: { context in
30            DynamicIsland {
31                DynamicIslandExpandedRegion(.leading) {
32                    Text(context.attributes.title).font(.system(size: 14, weight: .medium))
33                }
34                DynamicIslandExpandedRegion(.trailing) {
35                    Text(context.attributes.launch, style: .timer).font(.system(size: 14).monospacedDigit())
36                }
37                DynamicIslandExpandedRegion(.bottom) {
38                    Text("\(context.state.phase) · \(Int(context.state.distanceKm).formatted()) km").font(.system(size: 12)).opacity(0.8)
39                }
40            } compactLeading: {
41                Image(systemName: "moon.stars")
42            } compactTrailing: {
43                Text(context.attributes.launch, style: .timer).font(.system(size: 12).monospacedDigit()).frame(maxWidth: 64)
44            } minimal: {
45                Image(systemName: "moon.stars")
46            }
47        }
48    }
49}