remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

DiveReadouts.swift (4577B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import SwiftUI
      3 import RemoraDeco
      4 
      5 /// What to do now: alarm or warning, stop (countdown), safety stop, or no-stop time (NDL).
      6 /// Sizes are those of the watch; `scale` enlarges everything for a bigger screen.
      7 public struct DiveInstruction: View {
      8     let status: DiveComputer.Status
      9     var scale: CGFloat
     10 
     11     public init(status: DiveComputer.Status, scale: CGFloat = 1) {
     12         self.status = status
     13         self.scale = scale
     14     }
     15 
     16     private func rounded(_ size: CGFloat, _ weight: Font.Weight) -> Font {
     17         .system(size: size * scale, weight: weight, design: .rounded)
     18     }
     19 
     20     /// Text styles follow the system on the watch; scaled, they become fixed sizes.
     21     private func small(_ style: Font, _ size: CGFloat) -> Font {
     22         scale == 1 ? style : .system(size: size * scale)
     23     }
     24 
     25     public var body: some View {
     26         let s = status
     27         VStack(spacing: 1 * scale) {
     28             if let warning = s.warnings.sorted(by: { $0.rank < $1.rank }).first {
     29                 Text(warning.label.uppercased())
     30                     .font(rounded(14, .bold))
     31                     .foregroundStyle(warning.isViolation ? .white : warning.color)
     32                     .minimumScaleFactor(0.6)
     33                     .lineLimit(1)
     34                     .blinking([.ppO2High, .missedSafetyStop].contains(warning))
     35             }
     36             if let stop = s.plan.stops.first {
     37                 HStack(alignment: .firstTextBaseline, spacing: 4 * scale) {
     38                     Text("STOP \(Int(stop.depth.rounded())) m", bundle: .module)
     39                         .font(rounded(16, .bold))
     40                     Text(Format.clock(Double(stop.seconds ?? stop.minutes * 60)))
     41                         .font(rounded(22, .bold))
     42                         .monospacedDigit()
     43                 }
     44                 .foregroundStyle(.orange)
     45                 Text("surface in \(Int(s.plan.timeToSurface.rounded(.up))) min", bundle: .module)
     46                     .font(small(.caption2, 11)).foregroundStyle(.secondary)
     47             } else if case .running(let remaining) = s.safetyStop {
     48                 Text("SAFETY STOP", bundle: .module).font(rounded(12, .bold)).foregroundStyle(.cyan)
     49                 Text(Format.clock(remaining))
     50                     .font(rounded(26, .bold))
     51                     .monospacedDigit()
     52                     .foregroundStyle(.cyan)
     53             } else {
     54                 let low = s.warnings.contains(.lowNoDecompression)
     55                 HStack(alignment: .firstTextBaseline, spacing: 4 * scale) {
     56                     Text(verbatim: "NDL").font(rounded(15, .semibold)).foregroundStyle(.secondary)
     57                     Text(Format.ndl(s.plan.noDecompressionLimit))
     58                         .font(rounded(26, .bold))
     59                         .monospacedDigit()
     60                         .foregroundStyle(low ? .orange : .green)
     61                         .blinking(low)
     62                     Text(verbatim: "min").font(small(.caption, 12)).foregroundStyle(.secondary)
     63                 }
     64                 switch s.safetyStop {
     65                 case .pending where s.depth < 9:
     66                     Text("Safety stop 3 min at 5 m", bundle: .module).font(small(.caption2, 11)).foregroundStyle(.cyan)
     67                 case .done:
     68                     Text("Safety stop done ✓", bundle: .module).font(small(.caption2, 11)).foregroundStyle(.green)
     69                 default:
     70                     EmptyView()
     71                 }
     72             }
     73         }
     74     }
     75 }
     76 
     77 /// Vertical ascent rate scale: 5 segments (3, 6, 9, 10, 12 m/min) that fill from the bottom;
     78 /// green, yellow above 10 m/min, red above 12.
     79 public struct AscentScale: View {
     80     let rate: Double
     81     var scale: CGFloat
     82     public static let thresholds: [Double] = [3, 6, 9, 10, 12]
     83 
     84     public init(rate: Double, scale: CGFloat = 1) {
     85         self.rate = rate
     86         self.scale = scale
     87     }
     88 
     89     public var body: some View {
     90         VStack(spacing: 3 * scale) {
     91             Image(systemName: "arrow.up").font(.system(size: 9 * scale, weight: .heavy)).foregroundStyle(.secondary)
     92             ForEach(Self.thresholds.indices.reversed(), id: \.self) { i in
     93                 RoundedRectangle(cornerRadius: 2 * scale)
     94                     .fill(rate >= Self.thresholds[i] ? color : Color.gray.opacity(0.25))
     95                     .frame(width: 10 * scale, height: (i >= 3 ? 14 : 10) * scale)
     96             }
     97         }
     98         .accessibilityLabel(Text("Ascent \(Int(max(0, rate))) metres per minute", bundle: .module))
     99     }
    100 
    101     private var color: Color { rate > 12 ? .red : rate > 10 ? .yellow : .green }
    102 }