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

UserSettings.swift (5223B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 
      4 /// Diver settings, as the app saves and displays them.
      5 /// Settings: O2 percentage and water type. Advanced settings: conservatism (including custom gradient
      6 /// factors) and maximum ppO2. Altitude is not a setting: the watch measures the surface pressure.
      7 public struct UserSettings: Sendable, Hashable, Codable {
      8     /// Conservatism levels offered on the main screen (values specific to Open Rémora).
      9     public enum Conservatism: String, Sendable, Hashable, Codable, CaseIterable {
     10         case high       // GF 30/70, default
     11         case medium     // GF 40/80
     12         case low        // GF 50/85
     13         case custom     // gradient factors entered by the diver
     14 
     15         public var gradientFactors: GradientFactors? {
     16             switch self {
     17             case .high: GradientFactors(low: 0.30, high: 0.70)
     18             case .medium: GradientFactors(low: 0.40, high: 0.80)
     19             case .low: GradientFactors(low: 0.50, high: 0.85)
     20             case .custom: nil
     21             }
     22         }
     23     }
     24 
     25     /// Bounds of the settings (the interface offers nothing outside them).
     26     public enum Limits {
     27         public static let oxygenPercent = 21...40          // air to EAN40: a single gas, 40 m at most
     28         public static let gfLowPercent = 10...100
     29         public static let gfHighPercent = 50...100
     30         public static let ppO2Max = 1.2...1.6
     31         /// NDL alarm: thresholds offered, in minutes (0 = off).
     32         public static let ndlAlarmMinutes = [0, 3, 5, 10, 15]
     33     }
     34 
     35     // Settings
     36     public var oxygenPercent: Int = 21
     37     public var water: Water = .en13319
     38     /// Alarm when the NDL (remaining no-stop time) drops below this number of minutes; 0 = off.
     39     public var ndlAlarmMinutes: Int = 5
     40 
     41     // Advanced settings
     42     public var conservatism: Conservatism = .high
     43     /// Custom gradient factors (in %), used when `conservatism == .custom`.
     44     public var customGFLowPercent: Int = 30
     45     public var customGFHighPercent: Int = 70
     46     public var ppO2Max: Double = 1.4
     47 
     48     public init() {}
     49 
     50     /// Lenient decoding: a missing setting (saved by an older version, or sent by a watch that is not
     51     /// up to date yet) takes its default value instead of making the whole decoding fail.
     52     public init(from decoder: Decoder) throws {
     53         let v = try decoder.container(keyedBy: CodingKeys.self)
     54         let d = UserSettings()
     55         oxygenPercent = try v.decodeIfPresent(Int.self, forKey: .oxygenPercent) ?? d.oxygenPercent
     56         water = try v.decodeIfPresent(Water.self, forKey: .water) ?? d.water
     57         ndlAlarmMinutes = try v.decodeIfPresent(Int.self, forKey: .ndlAlarmMinutes) ?? d.ndlAlarmMinutes
     58         conservatism = try v.decodeIfPresent(Conservatism.self, forKey: .conservatism) ?? d.conservatism
     59         customGFLowPercent = try v.decodeIfPresent(Int.self, forKey: .customGFLowPercent) ?? d.customGFLowPercent
     60         customGFHighPercent = try v.decodeIfPresent(Int.self, forKey: .customGFHighPercent) ?? d.customGFHighPercent
     61         ppO2Max = try v.decodeIfPresent(Double.self, forKey: .ppO2Max) ?? d.ppO2Max
     62     }
     63 
     64     public enum Problem: Error, Hashable, Sendable {
     65         case oxygenOutOfRange
     66         case gfLowOutOfRange, gfHighOutOfRange, gfLowAboveHigh
     67         case ppO2OutOfRange
     68         case ndlAlarmOutOfRange
     69     }
     70 
     71     /// What prevents the settings from being applied (empty = valid).
     72     public var problems: [Problem] {
     73         var p: [Problem] = []
     74         if !Limits.oxygenPercent.contains(oxygenPercent) { p.append(.oxygenOutOfRange) }
     75         if conservatism == .custom {
     76             if !Limits.gfLowPercent.contains(customGFLowPercent) { p.append(.gfLowOutOfRange) }
     77             if !Limits.gfHighPercent.contains(customGFHighPercent) { p.append(.gfHighOutOfRange) }
     78             if customGFLowPercent > customGFHighPercent { p.append(.gfLowAboveHigh) }
     79         }
     80         if !Limits.ppO2Max.contains(ppO2Max) { p.append(.ppO2OutOfRange) }
     81         if !(0...30).contains(ndlAlarmMinutes) { p.append(.ndlAlarmOutOfRange) }
     82         return p
     83     }
     84 
     85     public var gas: Gas { Gas(o2: Double(oxygenPercent) / 100) }
     86 
     87     public var gradientFactors: GradientFactors {
     88         conservatism.gradientFactors
     89             ?? GradientFactors(low: Double(customGFLowPercent) / 100, high: Double(customGFHighPercent) / 100)
     90     }
     91 
     92     /// Maximum operating depth of the chosen gas (shown next to the percentage).
     93     public func maximumOperatingDepth(surfacePressure: Double = standardSurfacePressure) -> Double {
     94         gas.maximumOperatingDepth(ppO2Limit: ppO2Max,
     95                                   scale: DepthScale(surfacePressure: surfacePressure, water: water))
     96     }
     97 
     98     /// Computer configuration; refused when the settings are not valid.
     99     public func configuration() throws -> DiveComputer.Configuration {
    100         if let first = problems.first { throw first }
    101         var c = DiveComputer.Configuration()
    102         c.gas = gas
    103         c.water = water
    104         c.deco.gradientFactors = gradientFactors
    105         c.ppO2Warning = ppO2Max
    106         c.ppO2Alarm = max(1.6, ppO2Max)
    107         c.lowNoDecompression = Double(ndlAlarmMinutes)   // 0: never below the threshold, alarm off
    108         return c
    109     }
    110 }