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

SettingsViews.swift (5179B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import SwiftUI
      3 import RemoraDeco
      4 
      5 /// Diver settings: oxygen percentage, water type and NDL alarm; conservatism and maximum ppO2 are one level
      6 /// down, in the advanced settings. The app owns the storage: it passes a binding and saves on change.
      7 public struct DiveSettingsView: View {
      8     @Binding var settings: UserSettings
      9     let ndlAlarmNote: String?
     10     let altitudeNote: String?
     11 
     12     /// The notes are the footers specific to the device (how the alarm shows, where the altitude comes from).
     13     public init(settings: Binding<UserSettings>, ndlAlarmNote: String? = nil, altitudeNote: String? = nil) {
     14         _settings = settings
     15         self.ndlAlarmNote = ndlAlarmNote
     16         self.altitudeNote = altitudeNote
     17     }
     18 
     19     public var body: some View {
     20         List {
     21             Section {
     22                 Picker(selection: $settings.oxygenPercent) {
     23                     ForEach(Array(UserSettings.Limits.oxygenPercent), id: \.self) { p in
     24                         Text(SettingsText.oxygen(p)).tag(p)
     25                     }
     26                 } label: {
     27                     Text("Oxygen", bundle: .module)
     28                 }
     29                 Picker(selection: $settings.water) {
     30                     ForEach(Water.allCases, id: \.self) { Text($0.label).tag($0) }
     31                 } label: {
     32                     Text("Water", bundle: .module)
     33                 }
     34             } footer: {
     35                 Text("Maximum operating depth: \(Format.depth(settings.maximumOperatingDepth())) m", bundle: .module)
     36             }
     37 
     38             Section {
     39                 Picker(selection: $settings.ndlAlarmMinutes) {
     40                     ForEach(UserSettings.Limits.ndlAlarmMinutes, id: \.self) { m in
     41                         Text(SettingsText.ndlAlarm(m)).tag(m)
     42                     }
     43                 } label: {
     44                     Text("NDL alarm", bundle: .module)
     45                 }
     46             } footer: {
     47                 if let ndlAlarmNote { Text(ndlAlarmNote) }
     48             }
     49 
     50             Section {
     51                 NavigationLink {
     52                     AdvancedDiveSettingsView(settings: $settings)
     53                 } label: {
     54                     Text("Advanced settings", bundle: .module)
     55                 }
     56             } footer: {
     57                 if let altitudeNote { Text(altitudeNote) }
     58             }
     59         }
     60         .navigationTitle(Text("Settings", bundle: .module))
     61     }
     62 }
     63 
     64 /// Advanced settings: conservatism (gradient factors) and maximum ppO2.
     65 public struct AdvancedDiveSettingsView: View {
     66     @Binding var settings: UserSettings
     67 
     68     public init(settings: Binding<UserSettings>) { _settings = settings }
     69 
     70     public var body: some View {
     71         List {
     72             Section {
     73                 Picker(selection: $settings.conservatism) {
     74                     ForEach(UserSettings.Conservatism.allCases, id: \.self) { Text($0.label).tag($0) }
     75                 } label: {
     76                     Text("Conservatism", bundle: .module)
     77                 }
     78                 if settings.conservatism == .custom {
     79                     Stepper(value: $settings.customGFLowPercent,
     80                             in: UserSettings.Limits.gfLowPercent.lowerBound...settings.customGFHighPercent, step: 5) {
     81                         Text("GF low \(settings.customGFLowPercent)%", bundle: .module)
     82                     }
     83                     Stepper(value: $settings.customGFHighPercent,
     84                             in: max(UserSettings.Limits.gfHighPercent.lowerBound, settings.customGFLowPercent)...100, step: 5) {
     85                         Text("GF high \(settings.customGFHighPercent)%", bundle: .module)
     86                     }
     87                 }
     88             } header: {
     89                 Text("Conservatism", bundle: .module)
     90             }
     91             Section {
     92                 Picker(selection: $settings.ppO2Max) {
     93                     ForEach(SettingsText.ppO2Choices, id: \.self) { v in
     94                         Text(verbatim: SettingsText.ppO2(v)).tag(v)
     95                     }
     96                 } label: {
     97                     Text("Maximum ppO2", bundle: .module)
     98                 }
     99             } header: {
    100                 Text("Oxygen", bundle: .module)
    101             }
    102         }
    103         .navigationTitle(Text("Advanced", bundle: .module))
    104     }
    105 }
    106 
    107 /// The settings as read-only rows, in the same order and with the same words as the settings screens.
    108 /// Meant to be placed in a `List` section or a `Form`.
    109 public struct SettingsSummary: View {
    110     let settings: UserSettings
    111 
    112     public init(settings: UserSettings) { self.settings = settings }
    113 
    114     public var body: some View {
    115         row("Gas", settings.gas.description)
    116         row("Maximum operating depth", SettingsText.maximumOperatingDepth(settings))
    117         row("Water", settings.water.label)
    118         row("NDL alarm", SettingsText.ndlAlarm(settings.ndlAlarmMinutes))
    119         row("Conservatism", SettingsText.conservatism(settings))
    120         row("Maximum ppO2", SettingsText.ppO2(settings.ppO2Max))
    121     }
    122 
    123     private func row(_ title: LocalizedStringKey, _ value: String) -> some View {
    124         LabeledContent {
    125             Text(value)
    126         } label: {
    127             Text(title, bundle: .module)
    128         }
    129     }
    130 }