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

HomeView.swift (7626B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import SwiftUI
      3 import RemoraDeco
      4 import RemoraUI
      5 
      6 /// Single screen of the iPhone app: watch status, settings (read-only), last dive and post-dive waiting
      7 /// times, link to Sub Rascasse. The full logbook is in Sub Rascasse.
      8 struct HomeView: View {
      9     @Environment(PhoneStore.self) private var store
     10     @Environment(\.openURL) private var openURL
     11     @Environment(\.scenePhase) private var scenePhase
     12     @State private var subRascasseInstalled = false
     13     @State private var exportURL: URL?
     14 
     15     private static let subRascasseURL = URL(string: "subrascasse://open-remora")!
     16 
     17     var body: some View {
     18         NavigationStack {
     19             List {
     20                 watchSection
     21                 if let settings = store.settings { settingsSection(settings) }
     22                 if let dive = store.dives.first { lastDiveSection(dive) }
     23                 subRascasseSection
     24                 Section {
     25                     NavigationLink { HelpView() } label: { Label("User guide", systemImage: "book") }
     26                     Link(destination: AppTerms.link) { Label("Terms of use", systemImage: "doc.text") }
     27                     Link(destination: AppTerms.privacy) { Label("Privacy", systemImage: "hand.raised") }
     28                 } footer: {
     29                     Text("Open Rémora is a secondary dive computer, not certified. Always dive with a primary computer.")
     30                 }
     31             }
     32             .navigationTitle("Open Rémora")
     33         }
     34         .task { refresh() }
     35         .onChange(of: scenePhase) { _, phase in if phase == .active { refresh() } }
     36     }
     37 
     38     private func refresh() {
     39         store.refreshOutbox()
     40         store.refreshLiveActivity()
     41         subRascasseInstalled = UIApplication.shared.canOpenURL(Self.subRascasseURL)
     42         exportURL = store.dives.isEmpty ? nil : store.exportUDDF()
     43     }
     44 
     45     // MARK: Sections
     46 
     47     private var watchSection: some View {
     48         Section("Watch") {
     49             if !store.watchPaired {
     50                 Label("No paired Apple Watch", systemImage: "applewatch.slash").foregroundStyle(.orange)
     51             } else if !store.watchAppInstalled {
     52                 Label("Install Open Rémora on the watch (Watch app)", systemImage: "applewatch").foregroundStyle(.orange)
     53             } else {
     54                 Label("Open Rémora is installed on the watch", systemImage: "applewatch.radiowaves.left.and.right")
     55             }
     56             if let at = store.settingsReceivedAt {
     57                 Text("Last heard from the watch: \(at.formatted(.relative(presentation: .named)))")
     58                     .font(.footnote).foregroundStyle(.secondary)
     59             }
     60             if let error = store.lastError {
     61                 Text(error).font(.footnote).foregroundStyle(.red)
     62             }
     63         }
     64     }
     65 
     66     private func settingsSection(_ s: UserSettings) -> some View {
     67         Section {
     68             SettingsSummary(settings: s)
     69         } header: {
     70             Text("Settings")
     71         } footer: {
     72             Text("Settings can only be changed on the watch.")
     73         }
     74     }
     75 
     76     private func lastDiveSection(_ dive: DiveLog) -> some View {
     77         let now = Date()
     78         let fly = SurfaceAdvice.noFlyHours(logbook: store.logbook, at: now, surfacePressure: standardSurfacePressure)
     79         let mountain = SurfaceAdvice.hoursBeforeAltitude(2000, logbook: store.logbook, at: now,
     80                                                           surfacePressure: standardSurfacePressure)
     81         return Section("Last dive") {
     82             row("No. \(dive.number) · \(Format.dayTime(dive.start))",
     83                 "\(Format.depth(dive.maxDepth)) m · \(Format.minutes(dive.duration))")
     84             if dive.hadDecompression { Label("Mandatory stops", systemImage: "arrow.up.to.line").foregroundStyle(.orange) }
     85             ForEach(Array(dive.violations.enumerated()), id: \.offset) { _, event in
     86                 Label("\(event.warning.label) at \(Format.clock(event.time))", systemImage: "exclamationmark.triangle.fill")
     87                     .foregroundStyle(.red)
     88             }
     89             Label(fly > 0 ? "Flying: wait another \(Format.hours(fly))" : "Flying: no more restriction",
     90                   systemImage: "airplane")
     91             Label(mountain > 0 ? "Going up to 2000 m: wait another \(Format.hours(mountain))" : "Altitude: no more restriction",
     92                   systemImage: "mountain.2")
     93         }
     94     }
     95 
     96     private var subRascasseSection: some View {
     97         Section {
     98             if store.pendingForSubRascasse > 0 {
     99                 // Plural forms are in the String Catalog.
    100                 Label("\(store.pendingForSubRascasse) dives waiting for Sub Rascasse", systemImage: "tray.and.arrow.up")
    101             } else if !store.dives.isEmpty {
    102                 Label("All dives are in Sub Rascasse", systemImage: "checkmark.circle")
    103             }
    104             if subRascasseInstalled {
    105                 Button { openURL(Self.subRascasseURL) } label: { Label("Open Sub Rascasse", systemImage: "book.pages") }
    106             } else {
    107                 Label("Install Sub Rascasse to review your dives", systemImage: "arrow.down.app")
    108                     .foregroundStyle(.secondary)
    109             }
    110             if let exportURL {
    111                 ShareLink(item: exportURL) { Label("Export dives (UDDF)", systemImage: "square.and.arrow.up") }
    112             }
    113             if !store.sharedFolderAvailable {
    114                 Text("Shared folder unavailable: dives stay in this app.").font(.footnote).foregroundStyle(.orange)
    115             }
    116         } header: {
    117             Text("Logbook")
    118         } footer: {
    119             Text("The logbook, the profile and the charts are in Sub Rascasse, with your other dives. The UDDF export also opens in Subsurface.")
    120         }
    121     }
    122 
    123     private func row(_ title: LocalizedStringKey, _ value: String) -> some View {
    124         LabeledContent(title, value: value)
    125     }
    126 }
    127 
    128 /// Short user guide.
    129 struct HelpView: View {
    130     var body: some View {
    131         List {
    132             Section("Before the dive") {
    133                 Text("On the watch, tap “Ready to dive”: the app stays open until you get in the water. Check the gas, the conservatism and the battery.")
    134                 Text("Without it, the dive still starts by itself on submersion.")
    135             }
    136             Section("During the dive") {
    137                 Text("The depth is shown large. Below it: the NDL (time left without a mandatory stop), or the stop to do with its countdown.")
    138                 Text("The arrows at the bottom right show the ascent rate. Above 10 m/min: orange; above 12 m/min: red and vibration.")
    139                 Text("Alarm: the whole screen flashes red and the watch vibrates continuously (ceiling or stop broken, ascent too fast, critical oxygen pressure, depth unknown). Go back down to the stop or slow down.")
    140                 Text("Warning: only the value concerned blinks, with a vibration (low NDL, depth limit, fast ascent).")
    141             }
    142             Section("After the dive") {
    143                 Text("The dive ends after one minute at the surface. It arrives in this app, then in Sub Rascasse, and in Health as a diving workout.")
    144                 Text("On the watch, the logbook keeps your dives: tap a dive, then the profile, and turn the crown to browse it.")
    145             }
    146             Section("Settings (on the watch)") {
    147                 Text("Oxygen: 21% (air) to 40% (nitrox). Water: fresh, salt or standard. NDL alarm: threshold of your choice, or off.")
    148                 Text("Advanced settings: conservatism (gradient factors) and maximum oxygen pressure.")
    149             }
    150         }
    151         .navigationTitle("User guide")
    152     }
    153 }