Skip to content

Commit

Permalink
bugfix: fix distance and kcal calculations being incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcharger committed Mar 7, 2025
1 parent 8c94052 commit fe52702
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
8 changes: 4 additions & 4 deletions InfiniLink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = Y22PRZL3N4;
INFOPLIST_FILE = InfiniLinkTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -1102,7 +1102,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = Y22PRZL3N4;
INFOPLIST_FILE = InfiniLinkTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -1122,7 +1122,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = Y22PRZL3N4;
INFOPLIST_FILE = InfiniLinkUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -1142,7 +1142,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = Y22PRZL3N4;
INFOPLIST_FILE = InfiniLinkUITests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
2 changes: 1 addition & 1 deletion InfiniLink/Core/StepsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct StepsView: View {
DetailHeaderSubItemView(title: "Dis",
value: String(format: "%.2f", exerciseCalculator.calculateDistance(steps: steps(for: Date()))),
unit: personalizationController.units == .imperial ? "mi" : "km")
DetailHeaderSubItemView(title: "Kcal", value: String(format: "%.1f", exerciseCalculator.calculateCaloriesBurned(steps: steps(for: Date()))))
DetailHeaderSubItemView(title: "Kcal", value: String(exerciseCalculator.calculateCaloriesBurned(steps: steps(for: Date()))))
}
}
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
Expand Down
90 changes: 79 additions & 11 deletions InfiniLink/Utils/FitnessCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,95 @@
import Foundation

class FitnessCalculator {
enum Pace: Int {
case verySlowWalk = 0
case slowWalk = 1
case avgWalk = 2
case briskWalk = 3
case jog = 4
case run = 5
case fastRun = 6
case veryFastRun = 7

var metValue: Double {
switch self {
case .verySlowWalk: return 2.0
case .slowWalk: return 2.8
case .avgWalk: return 3.5
case .briskWalk: return 4.3
case .jog: return 7.0
case .run: return 9.8
case .fastRun: return 11.0
case .veryFastRun: return 13.0
}
}

var milesPerHour: Double {
switch self {
case .verySlowWalk: return 1.0
case .slowWalk: return 2.0
case .avgWalk: return 3.0
case .briskWalk: return 4.0
case .jog: return 5.0
case .run: return 6.0
case .fastRun: return 7.5
case .veryFastRun: return 10.0
}
}
}

let personalizationController = PersonalizationController.shared
let bleManager = BLEManager.shared

func calculateDistance(steps: Int) -> Double {
let avgStrideRatio = 0.413
let height = personalizationController.calculatedHeight
let strideLength = height * avgStrideRatio * (personalizationController.gender == .male ? 1.0 : 0.9)

func calculateDistance(steps: Int, pace: FitnessCalculator.Pace = .avgWalk) -> Double {
let avgStrideRatio = personalizationController.gender == .male ? 0.415 : 0.413
let calculatedHeight = personalizationController.calculatedHeight
let height = personalizationController.units == .metric ? (calculatedHeight / 2.54) : (calculatedHeight) // Convert to inches
let baseStrideLength = height * avgStrideRatio

// Adjust stride length based on pace
let strideMultiplier: Double = {
switch pace {
case .verySlowWalk: return 0.85
case .slowWalk: return 0.95
case .avgWalk: return 1.0
case .briskWalk: return 1.1
case .jog: return 1.25
case .run: return 1.4
case .fastRun: return 1.55
case .veryFastRun: return 1.7
}
}()

let strideLength = baseStrideLength * strideMultiplier
var distance = strideLength * Double(steps)

// Unit conversion
if personalizationController.units == .imperial {
distance /= (100.0 * 1609.34)
distance /= 63360 // Convert inches to miles
} else {
distance /= 100000
distance /= 39370 // Convert inches to kilometers
}

return distance
}

func calculateCaloriesBurned(steps: Int) -> Double {
return 0.0
func stepsPerMinute(steps: Int, pace: FitnessCalculator.Pace = .avgWalk) -> Int {
// TODO: update for metric
let distance = calculateDistance(steps: steps, pace: pace)
let timeMinutes = (distance / pace.milesPerHour) * 60.0

let spm = Double(steps) / timeMinutes
return Int(ceil(spm))
}

func calculateCaloriesBurned(steps: Int, pace: FitnessCalculator.Pace = .avgWalk) -> Int {
// TODO: support custom duration
let spm = stepsPerMinute(steps: steps, pace: pace)
let weight = personalizationController.calculatedWeight
let calculatedWeight = personalizationController.units == .metric ? weight : (weight * 0.453592)
let durationInHours = Double(steps) / Double(spm) / 60.0

let caloriesBurned = Int(ceil(pace.metValue * calculatedWeight * durationInHours))
return caloriesBurned
}
}

0 comments on commit fe52702

Please sign in to comment.