DiveListView.swift (13698B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import SubsurfaceLog 3 import SwiftUI 4 5 struct DiveListView: View { 6 @Environment(AppModel.self) private var model 7 @State private var search = "" 8 @State private var adding = false 9 @State private var importing = false 10 @State private var downloading = false 11 @State private var path: [String] = [] 12 @State private var appliedLaunchOptions = false 13 // Merging: the list turns into a selection, then the selected dives become one. 14 @State private var selecting = false 15 @State private var selection = Set<String>() 16 @State private var confirmingMerge = false 17 @State private var error: String? 18 19 var body: some View { 20 NavigationStack(path: $path) { 21 let sections = model.sections(matching: search) 22 // A plain list: a list with a selection binding swallows the taps on its links, even outside edit mode. 23 List { 24 SyncBanner() 25 ForEach(sections) { section in 26 Section { 27 ForEach(section.dives) { dive in 28 if selecting { 29 // Selection mode: no link, a check mark, and only what tells the dives apart. 30 Button { 31 if selection.contains(dive.id) { selection.remove(dive.id) } else { selection.insert(dive.id) } 32 } label: { 33 HStack(spacing: 12) { 34 Image(systemName: selection.contains(dive.id) ? "checkmark.circle.fill" : "circle") 35 .font(.title3) 36 .foregroundStyle(selection.contains(dive.id) ? Color.accentColor : Color.secondary) 37 DiveRow(dive: dive, compact: true) 38 } 39 .contentShape(Rectangle()) 40 } 41 .buttonStyle(.plain) 42 .listRowBackground(selection.contains(dive.id) ? Color.accentColor.opacity(0.12) : nil) 43 } else { 44 NavigationLink(value: dive.id) { DiveRow(dive: dive) } 45 } 46 } 47 } header: { 48 HStack { 49 Text(section.title) 50 Spacer() 51 Text(section.subtitle.isEmpty ? countLabel(section.dives.count) : "\(section.subtitle) · \(countLabel(section.dives.count))") 52 .foregroundStyle(.secondary) 53 } 54 } 55 } 56 } 57 .listStyle(.insetGrouped) 58 .overlay { 59 if model.logbook.dives.isEmpty { 60 ContentUnavailableView { 61 Label("Empty logbook", systemImage: "water.waves") 62 } description: { 63 Text(model.syncState == .syncing 64 ? String(localized: "Downloading the logbook…") 65 : String(localized: "Add a dive with the + button, or pull to sync.")) 66 } 67 } else if sections.isEmpty { 68 ContentUnavailableView.search(text: search) 69 } 70 } 71 .navigationTitle(selecting 72 ? (selection.count == 1 ? String(localized: "1 dive selected") : String(localized: "\(selection.count) dives selected")) 73 : String(localized: "Dives")) 74 .navigationBarTitleDisplayMode(selecting ? .inline : .automatic) 75 .toolbar(selecting ? .hidden : .automatic, for: .tabBar) 76 .navigationDestination(for: String.self) { DiveDetailView(diveID: $0) } 77 .searchable(text: $search, placement: .navigationBarDrawer(displayMode: .always), prompt: "Site, buddy, tag, notes") 78 .refreshable { await model.sync() } 79 .toolbar { 80 ToolbarItem(placement: .topBarLeading) { 81 if model.syncState == .syncing { ProgressView() } 82 } 83 ToolbarItem(placement: .topBarTrailing) { 84 if selecting { 85 Button("Done") { stopSelecting() } 86 } else { 87 Menu { 88 Button { adding = true } label: { Label("New dive", systemImage: "square.and.pencil") } 89 Button { downloading = true } label: { Label("Download from a dive computer…", systemImage: "gauge.with.dots.needle.bottom.50percent") } 90 Button { importing = true } label: { Label("Import a file…", systemImage: "square.and.arrow.down") } 91 Divider() 92 Button { selecting = true } label: { Label("Merge dives…", systemImage: "arrow.triangle.merge") } 93 .disabled(model.logbook.dives.count < 2) 94 } label: { 95 Label("Add", systemImage: "plus") 96 } 97 } 98 } 99 if selecting { 100 ToolbarItem(placement: .bottomBar) { 101 Button("Merge") { confirmingMerge = true } 102 .disabled(selection.count < 2) 103 } 104 } 105 } 106 .confirmationDialog(String(localized: "Merge \(selection.count) dives into one?"), isPresented: $confirmingMerge, titleVisibility: .visible) { 107 Button("Merge") { 108 Task { 109 do { 110 try await model.mergeDives(ids: selection) 111 stopSelecting() 112 } catch { 113 self.error = AppModel.describe(error) 114 } 115 } 116 } 117 } message: { 118 Text("The earliest dive keeps its date, number, site and notes; the others fill in what it lacks and add their dive computers. The dives numbered after them move down so that no number is left empty. The merge will be sent to the Subsurface cloud at the next sync.") 119 } 120 .alert("Action failed", isPresented: Binding(get: { error != nil }, set: { if !$0 { error = nil } })) { 121 Button("OK", role: .cancel) {} 122 } message: { 123 Text(error ?? "") 124 } 125 .onChange(of: model.mergeRequest) { 126 guard let id = model.mergeRequest else { return } 127 model.mergeRequest = nil 128 path = [] 129 selection = [id] 130 selecting = true 131 } 132 .sheet(isPresented: $adding) { NewDiveView() } 133 .task(id: model.logbook.dives.count) { await model.refreshPhotoPresence() } 134 .modifier(FileImport(isPresented: $importing)) 135 .modifier(DiveComputerImport(isPresented: $downloading)) 136 .onChange(of: model.logbook.dives.count) { 137 // Screenshot option: open a dive that has just been imported. 138 if path.isEmpty, let number = LaunchOptions.value("-showDive").flatMap({ Int($0) }), 139 let dive = model.logbook.dives.first(where: { $0.number == number }) { 140 path = [dive.id] 141 } 142 } 143 .onAppear { 144 guard !appliedLaunchOptions else { return } 145 appliedLaunchOptions = true 146 if let number = LaunchOptions.value("-showDive").flatMap({ Int($0) }), 147 let dive = model.logbook.dives.first(where: { $0.number == number }) { 148 path = [dive.id] 149 } 150 if LaunchOptions.flag("-newDive") { adding = true } 151 // Screenshot option: the selection that precedes a merge, "-selectDives 12,13". 152 if let numbers = LaunchOptions.value("-selectDives")?.split(separator: ",").compactMap({ Int($0) }) { 153 selecting = true 154 selection = Set(model.logbook.dives.filter { numbers.contains($0.number) }.map(\.id)) 155 if LaunchOptions.flag("-merge") { confirmingMerge = true } 156 // "-mergeNow" goes through with it (the simulator cannot be tapped from a script). 157 if LaunchOptions.flag("-mergeNow") { 158 Task { 159 try? await model.mergeDives(ids: selection) 160 stopSelecting() 161 } 162 } 163 } 164 } 165 } 166 } 167 168 private func stopSelecting() { 169 selecting = false 170 selection = [] 171 } 172 173 private func countLabel(_ count: Int) -> String { 174 count == 1 ? String(localized: "1 dive") : String(localized: "\(count) dives") 175 } 176 } 177 178 /// Sync status, shown at the top of the list when there is something to say. 179 struct SyncBanner: View { 180 @Environment(AppModel.self) private var model 181 182 var body: some View { 183 if model.isDemo { 184 Section { 185 Label("Demo logbook: nothing is uploaded.", systemImage: "sparkles") 186 .font(.footnote) 187 .foregroundStyle(.secondary) 188 } 189 } else if case .failed(let message) = model.syncState { 190 Section { 191 Label(message, systemImage: "exclamationmark.icloud") 192 .font(.footnote) 193 .foregroundStyle(.red) 194 if !model.pending.isEmpty { pendingLabel } 195 } 196 } else if !model.pending.isEmpty { 197 Section { pendingLabel } 198 } 199 ForEach(model.notices, id: \.self) { notice in 200 Section { 201 Label(notice, systemImage: "info.circle") 202 .font(.footnote) 203 .foregroundStyle(.orange) 204 } 205 } 206 } 207 208 private var pendingLabel: some View { 209 Label(model.pending.count == 1 ? String(localized: "1 change to upload") : String(localized: "\(model.pending.count) changes to upload"), 210 systemImage: "arrow.up.circle") 211 .font(.footnote) 212 .foregroundStyle(.secondary) 213 } 214 } 215 216 struct DiveRow: View { 217 @Environment(AppModel.self) private var model 218 let dive: Dive 219 /// Leaves out the photo badge and the rating (the list in selection mode has less room). 220 var compact = false 221 222 var body: some View { 223 HStack(alignment: .center, spacing: 12) { 224 Text(dive.number > 0 ? "\(dive.number)" : "–") 225 .font(.footnote.monospacedDigit().weight(.semibold)) 226 .foregroundStyle(.white) 227 .frame(minWidth: 34, minHeight: 34) 228 .background(Color.accentColor.gradient, in: RoundedRectangle(cornerRadius: 9, style: .continuous)) 229 VStack(alignment: .leading, spacing: 3) { 230 HStack(spacing: 6) { 231 Text(model.siteName(of: dive)) 232 .font(.body.weight(.medium)) 233 .lineLimit(1) 234 if !compact, let presence = model.photoPresence[dive.id] { 235 HStack(spacing: 3) { 236 if presence.photos { Image(systemName: "camera.fill") } 237 if presence.videos { Image(systemName: "video.fill") } 238 } 239 .font(.caption2) 240 .foregroundStyle(.pink) 241 .accessibilityLabel(presence.photos && presence.videos 242 ? String(localized: "With photos and videos") 243 : (presence.videos ? String(localized: "With videos") : String(localized: "With photos"))) 244 } 245 } 246 HStack(spacing: 6) { 247 Text(verbatim: "\(DiveDate.short(dive)) · \(DiveDate.time(dive))") 248 .lineLimit(1) 249 .fixedSize() 250 if dive.isPlanned { 251 Text("Plan") 252 .font(.caption2.weight(.semibold)) 253 .padding(.horizontal, 5) 254 .padding(.vertical, 1) 255 .background(Color.accentColor.opacity(0.15), in: Capsule()) 256 .foregroundStyle(Color.accentColor) 257 } 258 if !compact, dive.rating > 0 { 259 Text(String(repeating: "★", count: dive.rating)) 260 .foregroundStyle(.yellow) 261 .lineLimit(1) 262 .fixedSize() 263 } 264 } 265 .font(.caption) 266 .foregroundStyle(.secondary) 267 } 268 // The text column takes whatever the figures on the right leave. 269 .frame(maxWidth: .infinity, alignment: .leading) 270 VStack(alignment: .trailing, spacing: 3) { 271 Text(model.units.depth(dive.maxDepth)) 272 .font(.callout.monospacedDigit().weight(.semibold)) 273 Text(Units.duration(dive.durationSeconds)) 274 .font(.caption.monospacedDigit()) 275 .foregroundStyle(.secondary) 276 } 277 .fixedSize() 278 } 279 .padding(.vertical, 2) 280 .opacity(dive.invalid ? 0.5 : 1) 281 } 282 }