open-merou

Open Mérou, a free iOS app for the Oceanic+ underwater iPhone housing
git clone https://git.schurger.org/open-merou.git
Log | Files | Refs | README | LICENSE

DiveViews.swift (8807B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 // What the dive computer shows over the camera. Words, colours and alarm styles are those of the watch app
      3 // (components of RemoraUI); the layout is this app's: landscape, over the preview, driven by the housing buttons.
      4 import RemoraDeco
      5 import RemoraUI
      6 import SwiftUI
      7 
      8 /// Permanent band under the top bar during a dive: ascent rate, depth, dive time, and what to do now
      9 /// (NDL, stop with its countdown, safety stop, warning).
     10 struct DiveBand: View {
     11     let model: AppModel
     12 
     13     var body: some View {
     14         let monitor = model.dive.monitor
     15         VStack(alignment: .leading, spacing: 6) {
     16             if let s = monitor.status {
     17                 HStack(spacing: 16) {
     18                     AscentScale(rate: s.ascentRate, scale: 1.1)
     19                         .blinking(monitor.activeWarnings.contains(.ascentFast))
     20                     DepthReadout(depth: s.depth, size: 46, limit: monitor.activeWarnings.contains(.depthLimit))
     21                     VStack(alignment: .leading, spacing: 0) {
     22                         Text(Format.clock(s.elapsed * 60))
     23                             .font(.system(size: 22, weight: .semibold, design: .rounded)).monospacedDigit()
     24                         Text("max \(Format.depth(s.maxDepth)) m").font(.caption).foregroundStyle(.secondary)
     25                     }
     26                     DiveInstruction(status: s, scale: 1.25)
     27                 }
     28                 .padding(.horizontal, 14).padding(.vertical, 6)
     29                 .background(.black.opacity(0.55), in: RoundedRectangle(cornerRadius: 14))
     30             }
     31             if monitor.depthUnknown { DepthUnknownBanner(scale: 1.4).frame(maxWidth: 420) }
     32         }
     33         .frame(maxWidth: .infinity, alignment: .leading)
     34     }
     35 }
     36 
     37 /// The depth in large type; orange and blinking at the depth limit, as on the watch.
     38 struct DepthReadout: View {
     39     let depth: Double
     40     let size: CGFloat
     41     var limit = false
     42 
     43     var body: some View {
     44         HStack(alignment: .firstTextBaseline, spacing: 3) {
     45             Text(Format.depth(depth))
     46                 .font(.system(size: size, weight: .semibold, design: .rounded))
     47                 .monospacedDigit()
     48                 .lineLimit(1).fixedSize()
     49                 .foregroundStyle(limit ? .orange : .white)
     50                 .blinking(limit)
     51             Text(verbatim: "m").font(.system(size: size * 0.4)).foregroundStyle(.secondary)
     52         }
     53     }
     54 }
     55 
     56 /// Alarm in the middle of the screen, over the red flash: what is wrong, in very large type.
     57 struct DiveAlarmLabel: View {
     58     let monitor: DiveMonitor
     59 
     60     var body: some View {
     61         VStack(spacing: 10) {
     62             if monitor.depthUnknown {
     63                 DepthUnknownBanner(scale: 2.2).frame(maxWidth: 560)
     64             } else if let warning = monitor.warnings.filter(\.isViolation).sorted(by: { $0.rawValue < $1.rawValue }).first {
     65                 Text(warning.label.uppercased())
     66                     .font(.system(size: 52, weight: .black, design: .rounded))
     67                     .minimumScaleFactor(0.5).lineLimit(1)
     68             }
     69         }
     70         .foregroundStyle(.white)
     71         .shadow(radius: 4)
     72         .padding()
     73         .allowsHitTesting(false)
     74     }
     75 }
     76 
     77 /// Full-screen page of the dive computer (long press up, or Menu → Dive computer; left goes back).
     78 /// During a dive: the same read-outs as the watch, larger, with the details. At the surface: the settings,
     79 /// read-only. Computer off: the basic dive information only.
     80 struct ComputerPage: View {
     81     let model: AppModel
     82 
     83     private var dive: DiveSession { model.dive }
     84     /// Simulated housing: the on-screen buttons take the right of the screen, everything is smaller.
     85     private var compact: Bool { model.housing.simulated }
     86 
     87     var body: some View {
     88         let monitor = dive.monitor
     89         ZStack {
     90             // During an alarm the red flash stays visible through the page.
     91             Color.black.opacity(dive.isComputing && monitor.isAlarming ? 0.35 : 0.92).ignoresSafeArea()
     92             VStack(spacing: 8) {
     93                 if !dive.isActive {
     94                     off
     95                 } else if dive.isComputing, let status = monitor.status {
     96                     if monitor.depthUnknown { DepthUnknownBanner(scale: 1.6).frame(maxWidth: 520) }
     97                     diving(status)
     98                 } else {
     99                     ready
    100                 }
    101                 Text("Left or long press up: back to the camera").font(.caption2).foregroundStyle(.secondary)
    102             }
    103             // In simulation the page leaves the simulated buttons visible, as the other panels do.
    104             .padding(.leading, 24).padding(.trailing, model.housing.simulated ? 300 : 24).padding(.vertical, 46)
    105         }
    106     }
    107 
    108     private func diving(_ s: DiveComputer.Status) -> some View {
    109         let settings = dive.store.settings, monitor = dive.monitor
    110         return HStack(spacing: compact ? 12 : 24) {
    111             VStack(spacing: 4) {
    112                 HStack(spacing: 14) {
    113                     AscentScale(rate: s.ascentRate, scale: compact ? 1.4 : 2)
    114                         .blinking(monitor.activeWarnings.contains(.ascentFast))
    115                     DepthReadout(depth: s.depth, size: compact ? 62 : 92, limit: monitor.activeWarnings.contains(.depthLimit))
    116                 }
    117                 DiveInstruction(status: s, scale: compact ? 1.3 : 2).fixedSize()
    118             }
    119             .frame(maxWidth: .infinity)
    120 
    121             Grid(horizontalSpacing: 18, verticalSpacing: 8) {
    122                 GridRow {
    123                     cell("Time", Format.clock(s.elapsed * 60))
    124                     cell("Max", "\(Format.depth(s.maxDepth)) m")
    125                 }
    126                 GridRow {
    127                     cell("Surface", "\(Int(s.plan.timeToSurface.rounded(.up))) min")
    128                     cell("ppO2", Format.decimal(s.ppO2, digits: 2), color: s.ppO2 > settings.ppO2Max ? .orange : .white)
    129                 }
    130                 GridRow {
    131                     cell("Gas", settings.gas.description)
    132                     cell("Conservatism", settings.gradientFactors.description)
    133                 }
    134                 GridRow {
    135                     cell("Clock", Date().formatted(date: .omitted, time: .shortened))
    136                     cell("In the housing", model.housing.readings.temperatureC.map { "\(Format.decimal($0, digits: 1)) °C" } ?? "—")
    137                 }
    138             }
    139             .frame(maxWidth: .infinity)
    140         }
    141     }
    142 
    143     /// At the surface: the settings as they will be used, read-only.
    144     private var ready: some View {
    145         VStack(alignment: .leading, spacing: 6) {
    146             Text("Dive computer").font(.title2.bold())
    147             if dive.sensorImplausible {
    148                 Label("The depth sensor of the housing gives implausible values: no dive computer.", systemImage: "exclamationmark.triangle.fill")
    149                     .foregroundStyle(.orange)
    150             } else {
    151                 Label("Get in the water: the dive starts by itself", systemImage: "water.waves").foregroundStyle(.cyan)
    152             }
    153             SettingsSummary(settings: dive.store.settings)
    154             Text("Secondary computer, not certified. To be used with a primary dive computer.")
    155                 .font(.footnote).foregroundStyle(.secondary)
    156         }
    157         .frame(maxWidth: 460)
    158     }
    159 
    160     /// Computer off: depth, dive time and the temperature inside the housing, nothing else.
    161     private var off: some View {
    162         VStack(spacing: 8) {
    163             HStack(spacing: 28) {
    164                 Text(verbatim: "≈").font(.system(size: 48, weight: .semibold, design: .rounded)).foregroundStyle(.secondary)
    165                 DepthReadout(depth: dive.depth ?? 0, size: compact ? 62 : 92)
    166                 VStack(alignment: .leading, spacing: 6) {
    167                     if let start = dive.diveStart {
    168                         Text(start, style: .timer).font(.system(size: 34, weight: .semibold, design: .rounded)).monospacedDigit()
    169                     }
    170                     if let t = model.housing.readings.temperatureC {
    171                         Text("In the housing: \(Format.decimal(t, digits: 1)) °C").font(.title3)
    172                     }
    173                 }
    174             }
    175             Text("Dive computer off: estimated depth and dive time only. Nothing is computed or recorded.")
    176                 .font(.footnote).foregroundStyle(.secondary).multilineTextAlignment(.center)
    177         }
    178     }
    179 
    180     private func cell(_ title: LocalizedStringKey, _ value: String, color: Color = .white) -> some View {
    181         VStack(spacing: 0) {
    182             Text(title).font(.caption).foregroundStyle(.secondary)
    183             Text(value).font(.system(size: compact ? 19 : 26, weight: .semibold, design: .rounded)).monospacedDigit()
    184                 .foregroundStyle(color).minimumScaleFactor(0.6).lineLimit(1)
    185         }
    186         .frame(maxWidth: .infinity)
    187     }
    188 }