HomeScreen.swift (14254B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import RemoraDeco 3 import RemoraUI 4 import SwiftUI 5 6 /// Home screen, the only one made for touch: everything is set here before the housing is closed. 7 /// One side is the housing: searched from launch, then its right button starts the vacuum sequence (or a 8 /// simulated housing instead). The other is the home of the optional dive computer, as on the watch: gas 9 /// and conservatism, settings, last dive and waiting time before flying, dives pending for Sub Rascasse. 10 /// 11 /// It is a full screen on plain black, like the dive computer page: no camera preview behind it, no camera 12 /// bars around it. In landscape everything must be visible without scrolling: the regular sizes are tried 13 /// first, then the compact ones; scrolling is only the last resort (very large Dynamic Type). 14 struct HomeScreen: View { 15 let model: AppModel 16 /// Opens the diagnostics sheet (the ⓘ of the camera's top bar, which the home screen does not show). 17 let showDiagnostics: () -> Void 18 @Environment(\.openURL) private var openURL 19 @State private var showSettings = CommandLine.arguments.contains("-showDiveSettings") 20 @State private var showTerms = CommandLine.arguments.contains("-showTerms") 21 @State private var subRascasseInstalled = false 22 23 /// Sub Rascasse imports the pending dives when it is opened with this URL. 24 static let subRascasseURL = URL(string: "subrascasse://open-merou")! 25 26 /// Type sizes and spacing of one way of laying the screen out. 27 struct Metrics { 28 var title: Font, headline: Font, text: Font, note: Font 29 var spacing: CGFloat, columnSpacing: CGFloat, control: ControlSize 30 31 static let regular = Metrics(title: .title2.bold(), headline: .headline, text: .body, note: .footnote, 32 spacing: 10, columnSpacing: 32, control: .regular) 33 static let compact = Metrics(title: .title3.bold(), headline: .subheadline.weight(.semibold), text: .footnote, 34 note: .caption, spacing: 5, columnSpacing: 20, control: .small) 35 } 36 37 private var dive: DiveSession { model.dive } 38 /// "-demoHomeWorstCase": the fullest home screen there can be (screenshots, to check that it fits). 39 private var worstCase: Bool { CommandLine.arguments.contains("-demoHomeWorstCase") } 40 41 var body: some View { 42 GeometryReader { geometry in 43 if geometry.size.width > geometry.size.height { 44 ViewThatFits(in: .vertical) { 45 columns(.regular) 46 columns(.compact) 47 ScrollView { columns(.compact) } 48 } 49 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) 50 } else { 51 // Portrait orientation setting: one column, with room to scroll. 52 ScrollView { 53 VStack(alignment: .leading, spacing: 24) { 54 housingColumn(.regular) 55 computerColumn(.regular) 56 } 57 .frame(maxWidth: .infinity, alignment: .leading) 58 } 59 } 60 } 61 .padding(.horizontal, 16).padding(.vertical, 10) 62 .background(Color.black.ignoresSafeArea()) 63 .sheet(isPresented: $showSettings) { settingsSheet } 64 .sheet(isPresented: $showTerms) { termsSheet } 65 .task { 66 subRascasseInstalled = UIApplication.shared.canOpenURL(Self.subRascasseURL) 67 dive.store.refreshOutbox() 68 } 69 } 70 71 private func columns(_ m: Metrics) -> some View { 72 HStack(alignment: .top, spacing: m.columnSpacing) { 73 housingColumn(m).frame(maxWidth: .infinity, alignment: .leading) 74 computerColumn(m).frame(maxWidth: .infinity, alignment: .leading) 75 } 76 .controlSize(m.control) 77 } 78 79 // MARK: Housing 80 81 /// What is worth fixing before closing the housing. 82 private var checks: [String] { 83 var lines: [String] = [] 84 let housingBattery = worstCase ? 24 : model.housing.readings.batteryPercent 85 let phoneBattery = worstCase ? 18 : model.phoneBattery 86 let minutes = worstCase ? 12 : model.videoMinutesLeft 87 if let b = housingBattery, b < 30 { lines.append(String(localized: "Housing battery low: \(String(b)) %")) } 88 if let b = phoneBattery, b < 30 { lines.append(String(localized: "iPhone battery low: \(String(b)) %")) } 89 if let minutes, minutes < 20 { 90 lines.append(String(localized: "Low free space: about \(String(minutes)) min of video in \(model.settings.videoQuality.label)")) 91 } 92 if model.online || worstCase { lines.append(String(localized: "Airplane mode recommended (keep Bluetooth on)")) } 93 return lines 94 } 95 96 private var denied: [String] { 97 worstCase ? [String(localized: "microphone (videos without sound)"), String(localized: "photo library (nothing will be saved)")] 98 : model.camera.denied 99 } 100 101 /// A real housing answers ("-demoHousingDetected": shown as such for screenshots, nothing else changes). 102 private var housingDetected: Bool { 103 model.housing.isConnected || worstCase || CommandLine.arguments.contains("-demoHousingDetected") 104 } 105 106 /// The housing is searched from launch, nothing to touch: "searching" until it answers, then what to do 107 /// before closing it and the prompt to press its right button, which starts the vacuum sequence. 108 private func housingColumn(_ m: Metrics) -> some View { 109 let housing = model.housing 110 return VStack(alignment: .leading, spacing: m.spacing) { 111 Text("Open Mérou").font(m.title) 112 113 if housingDetected { 114 Label("Housing detected", systemImage: "checkmark.circle.fill").font(m.headline).foregroundStyle(.green) 115 Group { 116 if let p = housing.vacuum.pressurePa, p < VacuumSequence.alreadyUnderVacuumPa { 117 Text("It is already under vacuum: the leak test will start right away.") 118 } else { 119 Text("Put the iPhone in place (airplane mode recommended, Bluetooth on), close the housing and lock the latch.") 120 } 121 } 122 .font(m.text) 123 ForEach(checks, id: \.self) { line in 124 Label(line, systemImage: "exclamationmark.triangle.fill").font(m.note).foregroundStyle(.orange) 125 } 126 Text("Press the right button of the housing to continue").font(m.headline) 127 } else { 128 // Not a real button: it "confirms" by itself when the housing answers. 129 Button {} label: { 130 HStack { 131 Image(systemName: "dot.radiowaves.left.and.right") 132 if case .bluetoothUnavailable(let why) = housing.status { 133 Text("Use a housing · Bluetooth \(why)") 134 } else { 135 TimelineView(.periodic(from: .now, by: 0.5)) { context in 136 let dots = Int(context.date.timeIntervalSinceReferenceDate * 2) % 3 + 1 137 // Always three dots (fixed width, same font); only the first ones are visible. 138 HStack(spacing: 0) { 139 Text("Use a housing · searching") 140 ForEach(0..<3, id: \.self) { i in Text(".").opacity(i < dots ? 1 : 0) } 141 } 142 } 143 } 144 } 145 .frame(maxWidth: .infinity) 146 } 147 .buttonStyle(.bordered) 148 .disabled(true) 149 Text("Turn the housing on: it will be detected automatically.").font(m.note).foregroundStyle(.secondary) 150 // Only offered while no housing answers: once one is detected the column must fit the screen. 151 Button { housing.startSimulation() } label: { 152 Label("Simulate a housing", systemImage: "play.rectangle.fill").frame(maxWidth: .infinity) 153 } 154 .buttonStyle(.bordered) 155 } 156 157 ForEach(denied, id: \.self) { item in 158 Label("Permission denied: \(item). Settings → Open Mérou.", systemImage: "exclamationmark.triangle.fill") 159 .font(m.note).foregroundStyle(.orange) 160 } 161 Text("Set everything here before closing the housing: once it is closed, only its five buttons work.") 162 .font(m.note).foregroundStyle(.secondary) 163 } 164 .fixedSize(horizontal: false, vertical: true) 165 } 166 167 // MARK: Dive computer 168 169 private var enabled: Binding<Bool> { 170 Binding(get: { dive.enabled }, set: { on in 171 // Turning it on goes through the terms the first time (and again when they change). 172 if on, !dive.termsAccepted { showTerms = true } else { dive.enabled = on } 173 }) 174 } 175 176 private func computerColumn(_ m: Metrics) -> some View { 177 VStack(alignment: .leading, spacing: m.spacing) { 178 HStack(spacing: 12) { 179 Toggle(isOn: enabled) { Text("Dive computer").font(m.title) } 180 .tint(.cyan) 181 // Diagnostics of the housing (the camera's top bar, with its ⓘ, is not shown on this screen). 182 Button(action: showDiagnostics) { Image(systemName: "info.circle").font(m.title) } 183 } 184 185 if dive.isActive { 186 computerOn(m) 187 } else if dive.enabled { 188 Button { showTerms = true } label: { Label("Read and accept the terms", systemImage: "doc.text") } 189 .buttonStyle(.bordered) 190 } else { 191 Text("Off: the app only shows the depth, the dive time and the temperature inside the housing. Nothing is recorded.") 192 .font(m.note).foregroundStyle(.secondary) 193 } 194 } 195 .fixedSize(horizontal: false, vertical: true) 196 } 197 198 @ViewBuilder private func computerOn(_ m: Metrics) -> some View { 199 let store = dive.store, settings = store.settings 200 HStack(alignment: .center, spacing: 10) { 201 VStack(alignment: .leading, spacing: 2) { 202 Text(verbatim: "\(settings.gas.description) · \(SettingsText.conservatism(settings))").font(m.headline) 203 Text("max \(Format.depth(settings.maximumOperatingDepth())) m for this gas") 204 .font(m.note).foregroundStyle(.secondary) 205 } 206 Spacer(minLength: 0) 207 Button { showSettings = true } label: { Label("Settings", systemImage: "gearshape") } 208 .buttonStyle(.bordered) 209 } 210 211 if (model.housing.isConnected && dive.sensorImplausible) || worstCase { 212 Label("The depth sensor of the housing gives implausible values: no dive computer.", systemImage: "exclamationmark.triangle.fill") 213 .font(m.note).foregroundStyle(.orange) 214 } 215 216 if let last = store.lastDive { 217 let fly = store.noFlyHours() 218 Group { 219 Text("Last dive: \(Format.dayTime(last.start)) · \(Format.depth(last.maxDepth)) m · \(Format.minutes(last.duration))") 220 Label(fly > 0 ? "Flying: wait another \(Format.hours(fly))" : "Flying: no more restriction", systemImage: "airplane") 221 HStack(spacing: 10) { 222 if store.pending > 0 { 223 // Plural forms are in the String Catalog. 224 Label("\(store.pending) dives waiting for Sub Rascasse", systemImage: "tray.and.arrow.up") 225 } else if store.sharedFolderAvailable { 226 Label("All dives are in Sub Rascasse", systemImage: "checkmark.circle") 227 } 228 Spacer(minLength: 0) 229 if subRascasseInstalled || worstCase { 230 Button { openURL(Self.subRascasseURL) } label: { Label("Open Sub Rascasse", systemImage: "book.pages").labelStyle(.iconOnly) } 231 .buttonStyle(.bordered) 232 .accessibilityLabel(Text("Open Sub Rascasse")) 233 } 234 } 235 } 236 .font(m.note) 237 } 238 if !store.sharedFolderAvailable || worstCase { 239 Text("Shared folder unavailable: dives stay in this app.").font(m.note).foregroundStyle(.orange) 240 } 241 if let error = store.lastError { Text(error).font(m.note).foregroundStyle(.red) } 242 243 Text("Secondary computer, not certified. To be used with a primary dive computer.") 244 .font(m.note).foregroundStyle(.secondary) 245 Link(destination: DiveSession.terms.link) { Text("Terms of use").font(m.note) } 246 } 247 248 // MARK: Sheets 249 250 /// The settings screens are those of RemoraKit, shared with the watch; the app saves every change. 251 private var settingsSheet: some View { 252 @Bindable var store = dive.store 253 return NavigationStack { 254 DiveSettingsView( 255 settings: $store.settings, 256 ndlAlarmNote: String(localized: "NDL: time left without a mandatory stop. Below this threshold the value blinks and a sound is played."), 257 altitudeNote: String(localized: "Altitude: the surface pressure is measured by the housing, nothing to set. Underwater the settings can only be read (Menu → Information).")) 258 .toolbar { 259 ToolbarItem(placement: .confirmationAction) { Button("Done") { showSettings = false } } 260 } 261 } 262 .onChange(of: store.settings) { store.saveSettings() } 263 .preferredColorScheme(.dark) 264 } 265 266 private var termsSheet: some View { 267 NavigationStack { 268 TermsView(terms: DiveSession.terms) { 269 dive.acceptTerms() 270 showTerms = false 271 } 272 .toolbar { 273 ToolbarItem(placement: .cancellationAction) { Button("Cancel") { showTerms = false } } 274 } 275 } 276 .preferredColorScheme(.dark) 277 } 278 }