Skip to content

Commit b376f5a

Browse files
committed
Improve performance and fix fitness calculation bug
1 parent c2ab347 commit b376f5a

5 files changed

Lines changed: 13 additions & 9 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

InfiniLink/BLE/DeviceManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class DeviceManager: ObservableObject {
178178

179179
func removeDevice(_ device: Device) {
180180
let objectID = device.objectID
181-
let context = persistenceController.container.newBackgroundContext()
181+
let context = persistenceController.container.viewContext
182182

183183
context.perform {
184184
do {

InfiniLink/Core/Components/Charts/Steps/StepChartView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct StepChartView: View {
173173
let stepsRemaining = stepCountManager.stepGoal - steps
174174
let distanceRemaining = fitnessCalculator.calculateDistance(steps: stepsRemaining)
175175
let caloriesRemaining = fitnessCalculator.calculateCaloriesBurned(steps: stepsRemaining)
176-
let timeRemaining = fitnessCalculator.secondsFormatted(seconds: Int(fitnessCalculator.secondsForDistance(distance: distanceRemaining)), full: true)
176+
let timeRemaining = fitnessCalculator.secondsFormatted(seconds: fitnessCalculator.secondsForDistance(distance: distanceRemaining), full: true)
177177

178178
if stepsRemaining <= 1000 {
179179
Text("You're almost there! A quick \(String(format: "%.1f", distanceRemaining)) \(personalizationController.units == .imperial ? "mile" : "km") walk should get you to your goal. It should only take you about \(timeRemaining).")

InfiniLink/Utils/FitnessCalculator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class FitnessCalculator {
126126
}
127127

128128
func secondsForDistance(distance: Double, pace: Pace = .avgWalk) -> Int {
129-
let speed: Double = personalizationController.units == .imperial ? pace.milesPerHour : (pace.milesPerHour * 1.60934)
129+
let speed: Double = personalizationController.units == .imperial ? pace.milesPerHour : (pace.kmPerHour * 1.60934)
130130

131131
return Int(ceil((distance / speed) * 60 * 60))
132132
}

InfiniLink/Utils/PersistenceController.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ struct PersistenceController {
2424
}
2525
container.viewContext.automaticallyMergesChangesFromParent = true
2626
}
27-
27+
}
28+
29+
extension PersistenceController {
2830
func save() {
29-
Task {
31+
guard container.viewContext.hasChanges else { return }
32+
33+
container.viewContext.perform {
3034
do {
31-
try await container.viewContext.perform {
32-
try container.viewContext.save()
33-
}
35+
try self.container.viewContext.save()
3436
} catch {
35-
log("Unresolved error saving context: \(error.localizedDescription)", caller: "PersistenceController")
37+
// Handle the error appropriately. However, it's useful to use
38+
// `fatalError(_:file:line:)` during development.
39+
log("Failed to save view context: \(error.localizedDescription)", caller: "PersistenceController")
3640
}
3741
}
3842
}

0 commit comments

Comments
 (0)