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

DiveView.swift (7735B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import SwiftUI
      3 import RemoraDeco
      4 import RemoraUI
      5 
      6 /// Dive screen. Underwater the touch screen is locked: the crown moves from one page to the next and the
      7 /// Action button acts on the page shown.
      8 /// Page 1: depth, ascent rate, NDL or stop (Action: mark the point); page 2: details (Action: stopwatch);
      9 /// page 3: compass (Action: set the bearing, way back, clear).
     10 /// Wrist down (reduced luminance): only the depth and the NDL or the stop, in very large type.
     11 /// Alarm: the whole screen flashes red; warning: only the value concerned blinks.
     12 struct DiveView: View {
     13     @Environment(DiveSession.self) private var session
     14     @Environment(\.isLuminanceReduced) private var dimmed
     15 
     16     var body: some View {
     17         let s = session.status
     18         let locked = session.screenLocked
     19         @Bindable var session = session
     20         Group {
     21             if dimmed {
     22                 DimmedDivePage(status: s)
     23             } else {
     24                 TabView(selection: $session.divePage) {
     25                     MainDivePage(status: s).tag(0)
     26                     DetailsDivePage(status: s).tag(1)
     27                     CompassView(compact: true).tag(2)
     28                 }
     29                 .tabViewStyle(.verticalPage)
     30             }
     31         }
     32         .background { if session.isAlarming && !dimmed { AlarmFlash() } }
     33         .toolbar {
     34             // Useless while the touch screen is locked (long press on the crown unlocks it).
     35             if !locked {
     36                 ToolbarItem(placement: .cancellationAction) {
     37                     Button("End", role: .destructive) { session.end() }
     38                 }
     39             }
     40         }
     41         .overlay(alignment: .bottom) {
     42             if session.depthUnknown {
     43                 DepthUnknownBanner()
     44             }
     45         }
     46         .overlay(alignment: .top) {
     47             if let action = session.actionMessage {
     48                 Text(action).font(.system(size: 14, weight: .heavy, design: .rounded))
     49                     .padding(.horizontal, 8).padding(.vertical, 3)
     50                     .background(.cyan, in: Capsule()).foregroundStyle(.black)
     51             } else if let message = session.sensorMessage {
     52                 Text(message).font(.caption2).padding(3).background(.orange, in: Capsule())
     53             }
     54         }
     55         .task {
     56             if CommandLine.arguments.contains("-page2") { session.divePage = 1 }
     57             if CommandLine.arguments.contains("-page3") { session.divePage = 2 }
     58         }
     59     }
     60 
     61 }
     62 
     63 // MARK: - Page 1
     64 
     65 private struct MainDivePage: View {
     66     @Environment(DiveSession.self) private var session
     67     let status: DiveComputer.Status?
     68 
     69     var body: some View {
     70         let s = status
     71         VStack(spacing: 2) {
     72             HStack {
     73                 Text(Format.clock((s?.elapsed ?? 0) * 60))
     74                 Spacer()
     75                 if let t = session.temperature { Text("\(Int(t.rounded())) °C") }
     76             }
     77             .font(.system(size: 15, weight: .medium, design: .rounded))
     78             .foregroundStyle(.secondary)
     79 
     80             HStack(alignment: .center, spacing: 6) {
     81                 AscentScale(rate: s?.ascentRate ?? 0)
     82                     .blinking(session.activeWarnings.contains(.ascentFast))
     83                 VStack(spacing: 2) {
     84                     HStack(alignment: .firstTextBaseline, spacing: 2) {
     85                         Text(Format.depth(s?.depth ?? 0))
     86                             .font(.system(size: 56, weight: .semibold, design: .rounded))
     87                             .monospacedDigit()
     88                             .minimumScaleFactor(0.6)
     89                             .foregroundStyle(session.activeWarnings.contains(.depthLimit) ? .orange : .primary)
     90                             .blinking(session.activeWarnings.contains(.depthLimit))
     91                         Text("m").font(.title3).foregroundStyle(.secondary)
     92                     }
     93                     if let s { DiveInstruction(status: s) }
     94                 }
     95                 .frame(maxWidth: .infinity)
     96             }
     97 
     98             Spacer(minLength: 0)
     99 
    100             HStack {
    101                 Text("max \(Format.depth(s?.maxDepth ?? 0)) m")
    102                 Spacer()
    103                 Text("\(Int(max(0, s?.ascentRate ?? 0).rounded())) m/min ↑")
    104             }
    105             .font(.system(size: 13, design: .rounded))
    106             .foregroundStyle(.secondary)
    107         }
    108         .padding(.horizontal, 6)
    109     }
    110 }
    111 
    112 // MARK: - Page 2
    113 
    114 private struct DetailsDivePage: View {
    115     @Environment(DiveSession.self) private var session
    116     @Environment(LogStore.self) private var store
    117     let status: DiveComputer.Status?
    118 
    119     var body: some View {
    120         let s = status
    121         Grid(horizontalSpacing: 10, verticalSpacing: 8) {
    122             GridRow {
    123                 cell("Max", "\(Format.depth(s?.maxDepth ?? 0)) m")
    124                 cell("Surface", "\(Int((s?.plan.timeToSurface ?? 0).rounded(.up))) min")
    125             }
    126             GridRow {
    127                 cell("ppO2", Format.decimal(s?.ppO2 ?? 0, digits: 2),
    128                      color: (s?.ppO2 ?? 0) > store.settings.ppO2Max ? .orange : .primary)
    129                 cell("Gas", store.settings.gas.description)
    130             }
    131             GridRow {
    132                 TimelineView(.periodic(from: .now, by: 1)) { context in
    133                     cell("Stopwatch", Format.clock(session.stopwatch.elapsed(at: context.date)),
    134                          color: session.stopwatch.state == .running ? .cyan : .primary)
    135                 }
    136                 if let level = session.batteryLevel {
    137                     cell("Battery", Format.percent(Int((level * 100).rounded())), color: level < 0.3 ? .red : .primary)
    138                 } else {
    139                     cell("Conservatism", store.settings.gradientFactors.description)
    140                 }
    141             }
    142         }
    143         .padding(.horizontal, 6)
    144     }
    145 
    146     private func cell(_ title: LocalizedStringKey, _ value: String, color: Color = .primary) -> some View {
    147         VStack(spacing: 0) {
    148             Text(title).font(.caption2).foregroundStyle(.secondary)
    149             Text(value).font(.system(size: 20, weight: .semibold, design: .rounded)).monospacedDigit()
    150                 .foregroundStyle(color).minimumScaleFactor(0.6).lineLimit(1)
    151         }
    152         .frame(maxWidth: .infinity)
    153     }
    154 }
    155 
    156 // MARK: - Wrist down
    157 
    158 private struct DimmedDivePage: View {
    159     let status: DiveComputer.Status?
    160 
    161     var body: some View {
    162         let s = status
    163         VStack(spacing: 4) {
    164             HStack(alignment: .firstTextBaseline, spacing: 2) {
    165                 Text(Format.depth(s?.depth ?? 0))
    166                     .font(.system(size: 64, weight: .bold, design: .rounded))
    167                     .monospacedDigit().minimumScaleFactor(0.6)
    168                 Text("m").font(.title3)
    169             }
    170             if let s {
    171                 if let stop = s.plan.stops.first {
    172                     Text("STOP \(Int(stop.depth.rounded())) m · \(Format.clock(Double(stop.seconds ?? stop.minutes * 60)))")
    173                         .font(.system(size: 20, weight: .bold, design: .rounded))
    174                 } else if case .running(let remaining) = s.safetyStop {
    175                     Text("SAFETY \(Format.clock(remaining))").font(.system(size: 20, weight: .bold, design: .rounded))
    176                 } else {
    177                     Text("NDL \(Format.ndl(s.plan.noDecompressionLimit)) min")
    178                         .font(.system(size: 20, weight: .bold, design: .rounded))
    179                 }
    180                 if let warning = s.warnings.sorted(by: { $0.rank < $1.rank }).first {
    181                     Text(warning.label.uppercased()).font(.system(size: 15, weight: .heavy)).lineLimit(1)
    182                         .minimumScaleFactor(0.6)
    183                 }
    184             }
    185         }
    186         .foregroundStyle(.white)
    187     }
    188 }