Skip to content

Commit 5e5f2df

Browse files
shaheinmclaude
andauthored
Fix device picker not updating chart data (#109) (#110)
* Fix device picker not updating chart data on multi-computer dives Three issues fixed: 1. Picker showed devices with only fingerprint links (no samples) — selecting them silently fell back to all samples. Now only devices with actual sample data appear in the picker. 2. PPO2Chart used onChange(of: samples.count) which missed device switches with equal sample counts. Changed to samples.cacheKey. 3. Added "All Computers" option to show combined data from all devices. Closes #109 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Fix "All Computers" selection lost on data reload The device picker reset selectedDeviceId on every loadDiveData call when it was nil, but nil now means the user intentionally chose "All Computers". Add a hasPickedDevice flag so the default device is only set on first load, preserving the user's selection across edit sheet dismissals and other refreshes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b8cb7ca commit 5e5f2df

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

Profundum/Profundum/Views/DiveDetailView.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ struct DiveDetailView: View {
1212
@State private var showEditSheet = false
1313
@State private var loadedTeammateIds: [String] = []
1414
@State private var loadedEquipmentIds: [String] = []
15-
@State private var sourceCount: Int = 0
1615
@State private var sourceDeviceMap: [String: String] = [:]
1716
@State private var selectedDeviceId: String?
17+
@State private var hasPickedDevice = false
1818
@State private var surfaceIntervalSec: Int64?
1919
@State private var formulaResults: [(name: String, value: Double)] = []
2020
@State private var errorMessage: String?
@@ -65,6 +65,13 @@ struct DiveDetailView: View {
6565
samples.contains { $0.tankPressure1Bar != nil || $0.tankPressure2Bar != nil }
6666
}
6767

68+
/// Devices that actually have sample data for this dive.
69+
/// Filters out devices linked only via fingerprint (skipped dives).
70+
private var devicesWithSamples: [String: String] {
71+
let sampleDeviceIds = Set(samples.compactMap(\.deviceId))
72+
return sourceDeviceMap.filter { sampleDeviceIds.contains($0.key) }
73+
}
74+
6875
/// Samples filtered to the selected device for chart display.
6976
private var chartSamples: [DiveSample] {
7077
guard let deviceId = selectedDeviceId else { return samples }
@@ -238,10 +245,20 @@ struct DiveDetailView: View {
238245
// Tags row
239246
ScrollView(.horizontal, showsIndicators: false) {
240247
HStack(spacing: 8) {
241-
if sourceCount > 1 {
248+
if devicesWithSamples.count > 1 {
242249
Menu {
250+
Button {
251+
selectedDeviceId = nil
252+
} label: {
253+
if selectedDeviceId == nil {
254+
Label("All Computers", systemImage: "checkmark")
255+
} else {
256+
Text("All Computers")
257+
}
258+
}
259+
Divider()
243260
ForEach(
244-
sourceDeviceMap.sorted(by: { $0.value < $1.value }),
261+
devicesWithSamples.sorted(by: { $0.value < $1.value }),
245262
id: \.key
246263
) { deviceId, name in
247264
Button {
@@ -255,7 +272,10 @@ struct DiveDetailView: View {
255272
}
256273
}
257274
} label: {
258-
Badge(text: "\(sourceCount) computers", color: .purple)
275+
Badge(
276+
text: "\(devicesWithSamples.count) computers",
277+
color: .purple
278+
)
259279
}
260280
.accessibilityLabel("Select source computer for chart")
261281
}
@@ -729,9 +749,9 @@ struct DiveDetailView: View {
729749
loadedTeammateIds = detail.teammateIds
730750
loadedEquipmentIds = detail.equipmentIds
731751
sourceDeviceMap = detail.sourceDeviceMap
732-
sourceCount = detail.sourceDeviceMap.count
733-
if selectedDeviceId == nil {
752+
if !hasPickedDevice {
734753
selectedDeviceId = dive.deviceId
754+
hasPickedDevice = true
735755
}
736756

737757
let diveInput = DiveInput(
@@ -945,7 +965,7 @@ struct PPO2Chart: View {
945965
.onAppear {
946966
chartData = PPO2ChartData(samples: samples)
947967
}
948-
.onChange(of: samples.count) { _, _ in
968+
.onChange(of: samples.cacheKey) { _, _ in
949969
chartData = PPO2ChartData(samples: samples)
950970
}
951971
}

0 commit comments

Comments
 (0)