open-remora

Open Remora, a free secondary dive computer for Apple Watch Ultra
git clone https://git.schurger.org/open-remora.git
Log | Files | Refs | README | LICENSE

NoFlyLiveActivity.swift (3216B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import ActivityKit
      3 import SwiftUI
      4 import WidgetKit
      5 
      6 @main
      7 struct OpenRemoraWidgets: WidgetBundle {
      8     var body: some Widget {
      9         NoFlyLiveActivity()
     10     }
     11 }
     12 
     13 /// Lock screen and Dynamic Island: countdown to the time when flying becomes possible again.
     14 struct NoFlyLiveActivity: Widget {
     15     var body: some WidgetConfiguration {
     16         ActivityConfiguration(for: NoFlyAttributes.self) { context in
     17             LockScreenView(context: context)
     18                 .padding()
     19                 .activityBackgroundTint(Color(red: 0.02, green: 0.12, blue: 0.28))
     20                 .activitySystemActionForegroundColor(.white)
     21         } dynamicIsland: { context in
     22             DynamicIsland {
     23                 DynamicIslandExpandedRegion(.leading) {
     24                     Label("Flying", systemImage: "airplane").font(.headline)
     25                 }
     26                 DynamicIslandExpandedRegion(.trailing) {
     27                     Countdown(until: context.state.until).font(.title3.monospacedDigit())
     28                 }
     29                 DynamicIslandExpandedRegion(.bottom) {
     30                     Text("Dive no. \(context.attributes.diveNumber) · \(depth(context.attributes.maxDepth)) m")
     31                         .font(.caption).foregroundStyle(.secondary)
     32                 }
     33             } compactLeading: {
     34                 Image(systemName: "airplane").foregroundStyle(.orange)
     35             } compactTrailing: {
     36                 Countdown(until: context.state.until).monospacedDigit().frame(maxWidth: 52)
     37             } minimal: {
     38                 Image(systemName: "airplane").foregroundStyle(.orange)
     39             }
     40         }
     41     }
     42 }
     43 
     44 private struct LockScreenView: View {
     45     let context: ActivityViewContext<NoFlyAttributes>
     46 
     47     var body: some View {
     48         HStack(alignment: .center, spacing: 12) {
     49             Image(systemName: "airplane")
     50                 .font(.title2)
     51                 .foregroundStyle(.orange)
     52             VStack(alignment: .leading, spacing: 2) {
     53                 Text("No flying before \(context.state.until.formatted(date: .omitted, time: .shortened))")
     54                     .font(.headline)
     55                 if let altitude = context.state.altitudeUntil, altitude > .now {
     56                     Text("Not above 2000 m before \(altitude.formatted(date: .omitted, time: .shortened))")
     57                         .font(.caption)
     58                 }
     59                 Text("Open Rémora · dive no. \(context.attributes.diveNumber), \(depth(context.attributes.maxDepth)) m")
     60                     .font(.caption2).foregroundStyle(.secondary)
     61             }
     62             Spacer(minLength: 0)
     63             Countdown(until: context.state.until)
     64                 .font(.title2.monospacedDigit().bold())
     65                 .multilineTextAlignment(.trailing)
     66         }
     67         .foregroundStyle(.white)
     68     }
     69 }
     70 
     71 /// Countdown kept up to date by the system, without waking the app.
     72 private struct Countdown: View {
     73     let until: Date
     74 
     75     var body: some View {
     76         if until > .now {
     77             Text(timerInterval: Date.now...until, countsDown: true)
     78         } else {
     79             Text("OK")
     80         }
     81     }
     82 }
     83 
     84 private func depth(_ metres: Double) -> String {
     85     String(format: "%.0f", metres)
     86 }