Skip to content

Commit 57eb6c2

Browse files
committed
Add step reset tracking
1 parent d6a029e commit 57eb6c2

File tree

6 files changed

+68
-14
lines changed

6 files changed

+68
-14
lines changed

InfiniLink.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
E03C30B72CC7417D00DD8363 /* WeatherView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E03C30B62CC7417C00DD8363 /* WeatherView.swift */; };
2323
E04396152D28E59400A13B90 /* DirectionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E04396142D28E59400A13B90 /* DirectionsView.swift */; };
2424
E04396172D2A410100A13B90 /* DFUUpdaterCustom.swift in Sources */ = {isa = PBXBuildFile; fileRef = E04396162D2A410100A13B90 /* DFUUpdaterCustom.swift */; };
25+
E043FFCC2DD19FBD00BBFBBC /* StepSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E043FFCB2DD19FBD00BBFBBC /* StepSettingsView.swift */; };
2526
E05046892D136F1E00FE4BCD /* DebugLogsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E05046882D136F1D00FE4BCD /* DebugLogsView.swift */; };
2627
E05999992CB25C5600D64E0B /* SleepView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E05999982CB25C5600D64E0B /* SleepView.swift */; };
2728
E05999AC2CB2ED7100D64E0B /* repetitionscrolling.ttf in Resources */ = {isa = PBXBuildFile; fileRef = E05999A92CB2ED7100D64E0B /* repetitionscrolling.ttf */; };
@@ -150,6 +151,7 @@
150151
E03C30B62CC7417C00DD8363 /* WeatherView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherView.swift; sourceTree = "<group>"; };
151152
E04396142D28E59400A13B90 /* DirectionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectionsView.swift; sourceTree = "<group>"; };
152153
E04396162D2A410100A13B90 /* DFUUpdaterCustom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DFUUpdaterCustom.swift; sourceTree = "<group>"; };
154+
E043FFCB2DD19FBD00BBFBBC /* StepSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepSettingsView.swift; sourceTree = "<group>"; };
153155
E05046882D136F1D00FE4BCD /* DebugLogsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugLogsView.swift; sourceTree = "<group>"; };
154156
E05999982CB25C5600D64E0B /* SleepView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SleepView.swift; sourceTree = "<group>"; };
155157
E05999A02CB2ED7100D64E0B /* 7-Segment.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "7-Segment.ttf"; sourceTree = "<group>"; };
@@ -476,6 +478,7 @@
476478
E0A7C0752CB17E520042A12D /* MusicSettingsView.swift */,
477479
E0A7C0A42CB2527F0042A12D /* HeartSettingsView.swift */,
478480
E0A7C0792CB1AAAC0042A12D /* BatterySettingsView.swift */,
481+
E043FFCB2DD19FBD00BBFBBC /* StepSettingsView.swift */,
479482
);
480483
path = Settings;
481484
sourceTree = "<group>";
@@ -778,6 +781,7 @@
778781
E0A7C0862CB1B83E0042A12D /* Color+Extension.swift in Sources */,
779782
E05999FE2CB462BF00D64E0B /* ChartManager.swift in Sources */,
780783
E03C30B52CC7032800DD8363 /* ArbitraryNotificationView.swift in Sources */,
784+
E043FFCC2DD19FBD00BBFBBC /* StepSettingsView.swift in Sources */,
781785
264BFE4426BC51CE0050A223 /* ContentView.swift in Sources */,
782786
E09696DD2D2E3D6500CCCBF8 /* AppDetailsView.swift in Sources */,
783787
E05DA4C12D13752C00A49203 /* DebugLogManager.swift in Sources */,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// StepSettingsView.swift
3+
// InfiniLink
4+
//
5+
// Created by Liam Willey on 5/11/25.
6+
//
7+
8+
import SwiftUI
9+
10+
struct StepSettingsView: View {
11+
@AppStorage("addInsteadOfOverwrite") var addInsteadOfOverwrite: Bool = false
12+
13+
var body: some View {
14+
List {
15+
Section {
16+
Toggle("Track Resets", isOn: $addInsteadOfOverwrite)
17+
} footer: {
18+
Text("If the watch reboots, add steps to the saved count instead of resetting it to zero.")
19+
}
20+
}
21+
.navigationTitle("Settings")
22+
}
23+
}
24+
25+
#Preview {
26+
StepSettingsView()
27+
}

InfiniLink/Core/StepsView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ struct StepsView: View {
6666
.alert(isPresented: $showInfoAlert) {
6767
Alert(title: Text("Steps Per Minute"), message: Text("SPM stands for steps per minute and measures how many steps you take for each minute of walking."), dismissButton: .default(Text("OK")))
6868
}
69+
.toolbar {
70+
NavigationLink {
71+
StepSettingsView()
72+
} label: {
73+
Label("Settings", systemImage: "gear")
74+
}
75+
}
6976
}
7077
}
7178

InfiniLink/InfiniLink.xcdatamodeld/InfiniLink.xcdatamodel/contents

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
<entity name="StepCounts" representedClassName="StepCounts" syncable="YES" codeGenerationType="class">
6161
<attribute name="deviceId" optional="YES" attributeType="String"/>
6262
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
63-
<attribute name="steps" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
63+
<attribute name="previousSteps" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
64+
<attribute name="steps" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
6465
<attribute name="timestamp" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
6566
</entity>
6667
<entity name="UserExercise" representedClassName="UserExercise" syncable="YES" codeGenerationType="class">
@@ -73,4 +74,4 @@
7374
<attribute name="steps" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
7475
<relationship name="heartPoints" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="HeartDataPoint" inverseName="exercise" inverseEntity="HeartDataPoint"/>
7576
</entity>
76-
</model>
77+
</model>

InfiniLink/Localizable.xcstrings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,9 @@
783783
},
784784
"Hourly Chimes" : {
785785

786+
},
787+
"If the watch reboots, add steps to the saved count instead of resetting it to zero." : {
788+
786789
},
787790
"If you have an InfiniTime device, you can pair it here." : {
788791
"localizations" : {
@@ -1331,6 +1334,9 @@
13311334
},
13321335
"Total" : {
13331336

1337+
},
1338+
"Track Resets" : {
1339+
13341340
},
13351341
"Units" : {
13361342

InfiniLink/Utils/StepCountManager.swift

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import CoreData
9+
import SwiftUI
910

1011
class StepCountManager: ObservableObject {
1112
static let shared = StepCountManager()
@@ -14,6 +15,8 @@ class StepCountManager: ObservableObject {
1415
let bleManager = BLEManager.shared
1516
let persistenceManager = PersistenceController.shared
1617

18+
@AppStorage("addInsteadOfOverwrite") var addInsteadOfOverwrite: Bool = false
19+
1720
var stepGoal: Int {
1821
return Int(DeviceManager.shared.settings.stepsGoal)
1922
}
@@ -32,28 +35,34 @@ class StepCountManager: ObservableObject {
3235
}
3336
}
3437

35-
private func updateStepCount(_ stepCount: StepCounts, with steps: Int32, isArbitrary: Bool, for date: Date) {
36-
if isArbitrary {
37-
stepCount.steps += steps
38+
private func updateStepCount(_ current: StepCounts, with steps: Int32, isArbitrary: Bool, for date: Date) {
39+
// Last saved step count is current.steps = 978
40+
// We just received steps = 0
41+
if addInsteadOfOverwrite {
42+
// We have to do some math here because otherwise we'll just double the value
43+
if steps <= current.steps { // The watch reset, so add the new steps to the old count
44+
// This is fairly accurate, although about 6 steps get added because the watch tracks around that many before it sends the count (before we can set current.previousSteps)
45+
current.steps += abs(current.previousSteps - steps)
46+
} else {
47+
current.steps += abs(current.steps - steps)
48+
}
49+
} else if isArbitrary {
50+
current.steps += steps
3851
} else {
3952
clearCurrentDaySteps()
40-
stepCount.steps = max(stepCount.steps, steps)
53+
current.steps = steps
4154
}
4255

43-
stepCount.timestamp = date
56+
current.timestamp = date
57+
current.previousSteps = steps
4458

4559
persistenceManager.save()
4660
}
4761

4862
func clearCurrentDaySteps() {
49-
let now = Date()
50-
let existing = chartManager.stepsToday()
51-
52-
if let existing {
63+
if let existing = chartManager.stepsToday(){
5364
existing.steps = 0
54-
existing.timestamp = now
55-
} else {
56-
chartManager.addStepDataPoint(steps: 0, time: now)
65+
existing.timestamp = Date()
5766
}
5867

5968
persistenceManager.save()

0 commit comments

Comments
 (0)