Skip to content

Commit fe52702

Browse files
committed
bugfix: fix distance and kcal calculations being incorrect
1 parent 8c94052 commit fe52702

3 files changed

Lines changed: 84 additions & 16 deletions

File tree

InfiniLink.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@
10811081
CODE_SIGN_STYLE = Automatic;
10821082
DEVELOPMENT_TEAM = Y22PRZL3N4;
10831083
INFOPLIST_FILE = InfiniLinkTests/Info.plist;
1084-
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
1084+
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
10851085
LD_RUNPATH_SEARCH_PATHS = (
10861086
"$(inherited)",
10871087
"@executable_path/Frameworks",
@@ -1102,7 +1102,7 @@
11021102
CODE_SIGN_STYLE = Automatic;
11031103
DEVELOPMENT_TEAM = Y22PRZL3N4;
11041104
INFOPLIST_FILE = InfiniLinkTests/Info.plist;
1105-
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
1105+
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
11061106
LD_RUNPATH_SEARCH_PATHS = (
11071107
"$(inherited)",
11081108
"@executable_path/Frameworks",
@@ -1122,7 +1122,7 @@
11221122
CODE_SIGN_STYLE = Automatic;
11231123
DEVELOPMENT_TEAM = Y22PRZL3N4;
11241124
INFOPLIST_FILE = InfiniLinkUITests/Info.plist;
1125-
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
1125+
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
11261126
LD_RUNPATH_SEARCH_PATHS = (
11271127
"$(inherited)",
11281128
"@executable_path/Frameworks",
@@ -1142,7 +1142,7 @@
11421142
CODE_SIGN_STYLE = Automatic;
11431143
DEVELOPMENT_TEAM = Y22PRZL3N4;
11441144
INFOPLIST_FILE = InfiniLinkUITests/Info.plist;
1145-
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
1145+
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
11461146
LD_RUNPATH_SEARCH_PATHS = (
11471147
"$(inherited)",
11481148
"@executable_path/Frameworks",

InfiniLink/Core/StepsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct StepsView: View {
4545
DetailHeaderSubItemView(title: "Dis",
4646
value: String(format: "%.2f", exerciseCalculator.calculateDistance(steps: steps(for: Date()))),
4747
unit: personalizationController.units == .imperial ? "mi" : "km")
48-
DetailHeaderSubItemView(title: "Kcal", value: String(format: "%.1f", exerciseCalculator.calculateCaloriesBurned(steps: steps(for: Date()))))
48+
DetailHeaderSubItemView(title: "Kcal", value: String(exerciseCalculator.calculateCaloriesBurned(steps: steps(for: Date()))))
4949
}
5050
}
5151
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))

InfiniLink/Utils/FitnessCalculator.swift

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,95 @@
88
import Foundation
99

1010
class FitnessCalculator {
11+
enum Pace: Int {
12+
case verySlowWalk = 0
13+
case slowWalk = 1
14+
case avgWalk = 2
15+
case briskWalk = 3
16+
case jog = 4
17+
case run = 5
18+
case fastRun = 6
19+
case veryFastRun = 7
20+
21+
var metValue: Double {
22+
switch self {
23+
case .verySlowWalk: return 2.0
24+
case .slowWalk: return 2.8
25+
case .avgWalk: return 3.5
26+
case .briskWalk: return 4.3
27+
case .jog: return 7.0
28+
case .run: return 9.8
29+
case .fastRun: return 11.0
30+
case .veryFastRun: return 13.0
31+
}
32+
}
33+
34+
var milesPerHour: Double {
35+
switch self {
36+
case .verySlowWalk: return 1.0
37+
case .slowWalk: return 2.0
38+
case .avgWalk: return 3.0
39+
case .briskWalk: return 4.0
40+
case .jog: return 5.0
41+
case .run: return 6.0
42+
case .fastRun: return 7.5
43+
case .veryFastRun: return 10.0
44+
}
45+
}
46+
}
47+
1148
let personalizationController = PersonalizationController.shared
1249
let bleManager = BLEManager.shared
1350

14-
func calculateDistance(steps: Int) -> Double {
15-
let avgStrideRatio = 0.413
16-
let height = personalizationController.calculatedHeight
17-
let strideLength = height * avgStrideRatio * (personalizationController.gender == .male ? 1.0 : 0.9)
18-
51+
func calculateDistance(steps: Int, pace: FitnessCalculator.Pace = .avgWalk) -> Double {
52+
let avgStrideRatio = personalizationController.gender == .male ? 0.415 : 0.413
53+
let calculatedHeight = personalizationController.calculatedHeight
54+
let height = personalizationController.units == .metric ? (calculatedHeight / 2.54) : (calculatedHeight) // Convert to inches
55+
let baseStrideLength = height * avgStrideRatio
56+
57+
// Adjust stride length based on pace
58+
let strideMultiplier: Double = {
59+
switch pace {
60+
case .verySlowWalk: return 0.85
61+
case .slowWalk: return 0.95
62+
case .avgWalk: return 1.0
63+
case .briskWalk: return 1.1
64+
case .jog: return 1.25
65+
case .run: return 1.4
66+
case .fastRun: return 1.55
67+
case .veryFastRun: return 1.7
68+
}
69+
}()
70+
71+
let strideLength = baseStrideLength * strideMultiplier
1972
var distance = strideLength * Double(steps)
2073

21-
// Unit conversion
2274
if personalizationController.units == .imperial {
23-
distance /= (100.0 * 1609.34)
75+
distance /= 63360 // Convert inches to miles
2476
} else {
25-
distance /= 100000
77+
distance /= 39370 // Convert inches to kilometers
2678
}
27-
79+
2880
return distance
2981
}
3082

31-
func calculateCaloriesBurned(steps: Int) -> Double {
32-
return 0.0
83+
func stepsPerMinute(steps: Int, pace: FitnessCalculator.Pace = .avgWalk) -> Int {
84+
// TODO: update for metric
85+
let distance = calculateDistance(steps: steps, pace: pace)
86+
let timeMinutes = (distance / pace.milesPerHour) * 60.0
87+
88+
let spm = Double(steps) / timeMinutes
89+
return Int(ceil(spm))
90+
}
91+
92+
func calculateCaloriesBurned(steps: Int, pace: FitnessCalculator.Pace = .avgWalk) -> Int {
93+
// TODO: support custom duration
94+
let spm = stepsPerMinute(steps: steps, pace: pace)
95+
let weight = personalizationController.calculatedWeight
96+
let calculatedWeight = personalizationController.units == .metric ? weight : (weight * 0.453592)
97+
let durationInHours = Double(steps) / Double(spm) / 60.0
98+
99+
let caloriesBurned = Int(ceil(pace.metValue * calculatedWeight * durationInHours))
100+
return caloriesBurned
33101
}
34102
}

0 commit comments

Comments
 (0)