DiveDetailView.swift (9261B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import SwiftUI 3 import Charts 4 import RemoraDeco 5 import RemoraUI 6 7 /// Dive summary: the profile first, tap it to browse it with the crown. 8 struct DiveDetailView: View { 9 let dive: DiveLog 10 @AppStorage(PhoneLink.deliveredKey) private var delivered = "" 11 12 var body: some View { 13 List { 14 Section { 15 NavigationLink { ProfileView(dive: dive) } label: { 16 VStack(alignment: .leading, spacing: 4) { 17 ProfileChart(dive: dive, cursor: nil).frame(height: 70) 18 Text("Tap to browse the profile").font(.caption2).foregroundStyle(.secondary) 19 } 20 } 21 } 22 23 if dive.source == .remora { 24 Section { 25 if delivered.contains(dive.id.uuidString) { 26 Label("On the iPhone", systemImage: "checkmark.circle.fill").foregroundStyle(.green) 27 } else { 28 Label("Waiting for the iPhone", systemImage: "iphone.slash").foregroundStyle(.orange) 29 } 30 } footer: { 31 Text("From there, it goes to Sub Rascasse.") 32 } 33 } 34 35 Section { 36 row("Max depth", "\(Format.depth(dive.maxDepth)) m") 37 row("Duration", Format.clock(dive.duration)) 38 row("Average depth", "\(Format.depth(dive.averageDepth)) m") 39 if let t = dive.minTemperature { row("Coldest water", "\(Int(t.rounded())) °C") } 40 row("Gas", dive.gas.description) 41 row("Mandatory stops", dive.hadDecompression ? String(localized: "Yes") : String(localized: "No")) 42 if let si = dive.surfaceIntervalBefore { row("Surface interval", Format.clock(si * 60)) } 43 } 44 45 Section(dive.events.isEmpty ? "No alarms" : "Alarms") { 46 ForEach(Array(dive.events.enumerated()), id: \.offset) { _, event in 47 HStack { 48 Image(systemName: event.warning.isViolation ? "exclamationmark.triangle.fill" : "exclamationmark.circle") 49 .foregroundStyle(event.warning.color) 50 Text(event.warning.label).font(.footnote) 51 Spacer() 52 Text(Format.clock(event.time)).font(.footnote).foregroundStyle(.secondary).monospacedDigit() 53 } 54 } 55 } 56 57 if let marks = dive.marks, !marks.isEmpty { 58 Section("Marks") { 59 ForEach(Array(marks.enumerated()), id: \.offset) { i, mark in 60 HStack { 61 Image(systemName: "mappin").foregroundStyle(.cyan) 62 Text("\(i + 1) · \(Format.depth(mark.depth)) m").font(.footnote) 63 if let heading = mark.heading { 64 Text("\(Int(heading.rounded()) % 360)°").font(.footnote).foregroundStyle(.secondary) 65 } 66 Spacer() 67 Text(Format.clock(mark.time)).font(.footnote).foregroundStyle(.secondary).monospacedDigit() 68 } 69 } 70 } 71 } 72 73 Section { 74 row("Start", Format.dayTime(dive.start)) 75 if let latitude = dive.latitude, let longitude = dive.longitude { 76 row("Position", String(format: "%.5f, %.5f", latitude, longitude)) 77 } 78 if let site = dive.site { row("Site", site) } 79 if let gf = dive.gradientFactors { row("Conservatism", gf.description) } 80 row("Water", dive.water.label) 81 if let model = dive.computerModel { row("Computer", model) } 82 } 83 } 84 .navigationTitle("Dive \(dive.number)") 85 } 86 87 private func row(_ title: LocalizedStringKey, _ value: String) -> some View { 88 VStack(alignment: .leading, spacing: 0) { 89 Text(title).font(.caption2).foregroundStyle(.secondary) 90 Text(value).font(.body) 91 } 92 } 93 } 94 95 /// Full-screen profile: the crown moves the cursor along the dive. 96 struct ProfileView: View { 97 let dive: DiveLog 98 @State private var position = 0.0 99 @FocusState private var focused: Bool 100 101 var body: some View { 102 let sample = dive.samples.isEmpty ? nil : dive.samples[min(dive.samples.count - 1, Int(position.rounded()))] 103 VStack(alignment: .leading, spacing: 2) { 104 if let sample { 105 HStack(alignment: .firstTextBaseline) { 106 Text("\(Format.depth(sample.depth)) m").font(.system(size: 22, weight: .semibold, design: .rounded)) 107 Spacer() 108 Text(Format.clock(sample.time)).font(.system(size: 17, design: .rounded)).foregroundStyle(.secondary) 109 } 110 .monospacedDigit() 111 Text(detail(sample)).font(.caption2).foregroundStyle(detailColor(sample)).lineLimit(1) 112 } 113 ProfileChart(dive: dive, cursor: sample) 114 } 115 .padding(.horizontal, 4) 116 .focusable() 117 .focused($focused) 118 .digitalCrownRotation($position, from: 0, through: Double(max(1, dive.samples.count - 1)), 119 by: 1, sensitivity: .high, isContinuous: false, isHapticFeedbackEnabled: false) 120 .onAppear { 121 focused = true 122 // Open on the deepest point. 123 if let deepest = dive.samples.indices.max(by: { dive.samples[$0].depth < dive.samples[$1].depth }) { 124 position = Double(deepest) 125 } 126 } 127 .navigationTitle("Profile") 128 } 129 130 private func event(at sample: DiveSample) -> DiveEvent? { 131 dive.events.last { abs($0.time - sample.time) <= 5 } 132 } 133 134 private func detail(_ s: DiveSample) -> String { 135 if let e = event(at: s) { return e.warning.label } 136 var parts: [String] = [] 137 if let t = s.temperature { parts.append("\(Int(t.rounded())) °C") } 138 if let stop = s.stopDepth, let minutes = s.stopMinutes { 139 parts.append(String(localized: "stop \(Int(stop)) m · \(minutes) min")) 140 } else if dive.source != .subsurface || s.noDecompressionLimit != nil { 141 parts.append("NDL \(Format.ndl(s.noDecompressionLimit)) min") 142 } 143 return parts.joined(separator: " · ") 144 } 145 146 private func detailColor(_ s: DiveSample) -> Color { 147 if let e = event(at: s) { return e.warning.color } 148 return s.stopDepth != nil ? .orange : .secondary 149 } 150 } 151 152 /// The chart: depth (downwards), decompression ceiling, alarms. 153 struct ProfileChart: View { 154 let dive: DiveLog 155 let cursor: DiveSample? 156 157 /// At most ~150 points for the plot. 158 private var points: [DiveSample] { 159 let step = max(1, dive.samples.count / 150) 160 var out = stride(from: 0, to: dive.samples.count, by: step).map { dive.samples[$0] } 161 if let last = dive.samples.last, out.last != last { out.append(last) } 162 return out 163 } 164 165 private func depth(at time: Double) -> Double { 166 dive.samples.min { abs($0.time - time) < abs($1.time - time) }?.depth ?? 0 167 } 168 169 var body: some View { 170 Chart { 171 ForEach(points, id: \.time) { s in 172 AreaMark(x: .value("Time", s.time / 60), y: .value("Depth", s.depth)) 173 .foregroundStyle(.linearGradient(colors: [.cyan.opacity(0.25), .blue.opacity(0.6)], 174 startPoint: .top, endPoint: .bottom)) 175 LineMark(x: .value("Time", s.time / 60), y: .value("Depth", s.depth), 176 series: .value("Series", "depth")) 177 .foregroundStyle(.cyan) 178 .lineStyle(StrokeStyle(lineWidth: 1.5)) 179 } 180 if dive.hadDecompression { 181 ForEach(points, id: \.time) { s in 182 LineMark(x: .value("Time", s.time / 60), y: .value("Ceiling", s.ceiling), 183 series: .value("Series", "ceiling")) 184 .foregroundStyle(.orange) 185 .lineStyle(StrokeStyle(lineWidth: 1, dash: [3, 2])) 186 } 187 } 188 ForEach(Array(dive.events.enumerated()), id: \.offset) { _, e in 189 PointMark(x: .value("Time", e.time / 60), y: .value("Depth", depth(at: e.time))) 190 .foregroundStyle(e.warning.color) 191 .symbolSize(30) 192 } 193 if let cursor { 194 RuleMark(x: .value("Time", cursor.time / 60)).foregroundStyle(.white.opacity(0.7)) 195 PointMark(x: .value("Time", cursor.time / 60), y: .value("Depth", cursor.depth)) 196 .foregroundStyle(.white) 197 } 198 } 199 .chartYScale(domain: .automatic(includesZero: true, reversed: true)) 200 .chartXAxis { AxisMarks(values: .automatic(desiredCount: 3)) { AxisValueLabel().font(.system(size: 9)) } } 201 .chartYAxis { AxisMarks(values: .automatic(desiredCount: 3)) { AxisGridLine(); AxisValueLabel().font(.system(size: 9)) } } 202 } 203 }