Skip to content

Commit b9d43ee

Browse files
committed
bugfixes: fix incorrect streak display behavior and improve performance
1 parent 06bae41 commit b9d43ee

File tree

9 files changed

+62
-44
lines changed

9 files changed

+62
-44
lines changed

InfiniLink/BLE/DeviceManager.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,8 @@ class DeviceManager: ObservableObject {
154154
infineatWatchFace.showSideCover = settings.watchFaceInfineat.showSideCover
155155
device.watchFaceInfineat = infineatWatchFace
156156

157-
Task {
158-
await persistenceController.save()
159-
160-
getSettings()
161-
}
157+
persistenceController.save()
158+
getSettings()
162159
}
163160

164161
func updateName(name: String, for device: Device) {
@@ -167,9 +164,7 @@ class DeviceManager: ObservableObject {
167164

168165
device.name = name
169166

170-
Task {
171-
await persistenceController.save()
172-
}
167+
persistenceController.save()
173168
}
174169

175170
func getName(for uuid: String) -> String {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct StepChartView: View {
3737

3838
var filledData: [StepChartDataPoint] = []
3939

40-
let rawPoints = chartManager.stepPoints().compactMap { record -> StepChartDataPoint? in
40+
let rawPoints = chartManager.stepPoints(predicate: chartManager.weekPredicate).compactMap { record -> StepChartDataPoint? in
4141
guard let timestamp = record.timestamp else { return nil }
4242
// Return each step point as a chart point
4343
return StepChartDataPoint(date: calendar.startOfDay(for: timestamp), steps: Int(record.steps))
@@ -59,9 +59,8 @@ struct StepChartView: View {
5959
}
6060

6161
var streak: Int {
62-
return chartManager.stepPoints().reversed().prefix(7).filter({
63-
return $0.steps >= deviceManager.settings.stepsGoal
64-
}).count
62+
let points = chartManager.stepPoints(predicate: chartManager.weekPredicate)
63+
return points.filter { $0.steps >= deviceManager.settings.stepsGoal }.count
6564
}
6665
var earliestDate: Date {
6766
stepChartPoints().compactMap({ $0.date }).min() ?? Date()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct StepCalendarView: View {
3535
.frame(maxWidth: .infinity)
3636
}
3737
}
38-
// FIXME: poor performance
38+
let stepPoints = chartManager.stepPoints(predicate: chartManager.allTimePredicate)
3939
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: weekdays.count), spacing: 14) {
4040
ForEach(fetchDates(), id: \.id) { value in
4141
ZStack {
@@ -47,7 +47,7 @@ struct StepCalendarView: View {
4747
.font(.system(size: 16).weight(.medium))
4848
.opacity(value.day == -1 ? 0 : 1)
4949
if deviceManager.settings.stepsGoal > 0 && value.day != -1 {
50-
let progress = min(Double(chartManager.stepPoints().first(where: { Calendar.current.isDate(value.date, equalTo: $0.timestamp!, toGranularity: .day)})?.steps ?? 0) / Double(deviceManager.settings.stepsGoal), 1)
50+
let progress = min(Double(stepPoints.first(where: { Calendar.current.isDate(value.date, equalTo: $0.timestamp!, toGranularity: .day)})?.steps ?? 0) / Double(deviceManager.settings.stepsGoal), 1)
5151
PieSlice(progress: progress)
5252
.fill(Color.blue.opacity(0.8))
5353
label

InfiniLink/Core/DeviceView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ struct DeviceView: View {
241241
notificationManager.setWaterRemindersPerDay()
242242
remindersManager.requestAccess()
243243
remindersManager.fetchAllItems()
244-
downloadManager.updateAvailable = downloadManager.checkForUpdates(currentVersion: deviceManager.firmware)
245244
}
246245
.toolbar {
247246
ToolbarItem(placement: .topBarLeading) {

InfiniLink/Core/StepsView.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ struct StepsView: View {
2020
let exerciseCalculator = FitnessCalculator()
2121

2222
func steps() -> Int {
23-
for stepCount in chartManager.stepPoints() {
24-
if Calendar.current.isDate(stepCount.timestamp!, inSameDayAs: Date()) {
25-
return Int(stepCount.steps)
26-
}
23+
let stepCount = chartManager.stepsToday()
24+
if let stepCount = stepCount {
25+
return Int(stepCount.steps)
2726
}
2827
return 0
2928
}

InfiniLink/InfiniLinkApp.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import CoreData
1111
@main
1212
struct InfiniLink: App {
1313
let persistenceController = PersistenceController.shared
14+
let downloadManager = DownloadManager.shared
15+
let notificationManager = NotificationManager.shared
16+
let deviceManager = DeviceManager.shared
1417

1518
@AppStorage("colorScheme") var colorScheme = "system"
1619

@@ -20,6 +23,8 @@ struct InfiniLink: App {
2023
NotificationCenter.default.addObserver(forName: .EKEventStoreChanged, object: nil, queue: .main) { _ in
2124
RemindersManager.shared.fetchAllItems()
2225
}
26+
27+
downloadManager.updateAvailable = downloadManager.checkForUpdates(currentVersion: deviceManager.firmware)
2328
}
2429

2530
var body: some Scene {

InfiniLink/Utils/ChartManager.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ class ChartManager: ObservableObject {
1313
let persistenceController = PersistenceController.shared
1414
let bleManager = BLEManager.shared
1515

16+
private let predicateString = "deviceId == %@ AND timestamp >= %@"
17+
private let calendar = Calendar.current
18+
19+
var weekPredicate: NSPredicate {
20+
let deviceId = bleManager.pairedDeviceID ?? ""
21+
// Get the days of the current week, not just -7 days from now
22+
let startOfWeek = calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))!
23+
24+
return NSPredicate(format: predicateString, deviceId, startOfWeek as NSDate)
25+
}
26+
var dayPredicate: NSPredicate {
27+
let deviceId = bleManager.pairedDeviceID ?? ""
28+
let startOfDay = calendar.startOfDay(for: Date())
29+
30+
return NSPredicate(format: predicateString, deviceId, startOfDay as NSDate)
31+
}
32+
var allTimePredicate: NSPredicate {
33+
let deviceId = bleManager.pairedDeviceID ?? ""
34+
return NSPredicate(format: "deviceId == %@", deviceId)
35+
}
36+
1637
@AppStorage("heartRateChartDataSelection") var heartRateChartDataSelection = 0
1738
@AppStorage("stepChartDataSelection") var stepChartDataSelection = 0
1839

@@ -80,11 +101,13 @@ class ChartManager: ObservableObject {
80101
}
81102
}
82103

83-
func stepPoints() -> [StepCounts] {
84-
guard let deviceId = bleManager.pairedDeviceID else { return [] }
85-
104+
func stepsToday() -> StepCounts? {
105+
return stepPoints().first
106+
}
107+
108+
func stepPoints(predicate: NSPredicate? = nil) -> [StepCounts] {
86109
let fetchRequest: NSFetchRequest<StepCounts> = StepCounts.fetchRequest()
87-
fetchRequest.predicate = NSPredicate(format: "deviceId == %@", deviceId)
110+
fetchRequest.predicate = predicate ?? dayPredicate
88111

89112
do {
90113
return try persistenceController.container.viewContext.fetch(fetchRequest)

InfiniLink/Utils/PersistenceController.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ struct PersistenceController {
2525
container.viewContext.automaticallyMergesChangesFromParent = true
2626
}
2727

28-
func save() async {
29-
do {
30-
try await container.viewContext.perform {
31-
try container.viewContext.save()
28+
func save() {
29+
Task {
30+
do {
31+
try await container.viewContext.perform {
32+
try container.viewContext.save()
33+
}
34+
} catch {
35+
log("Unresolved error saving context: \(error.localizedDescription)", caller: "PersistenceController")
3236
}
33-
} catch {
34-
log("Unresolved error saving context: \(error.localizedDescription)", caller: "PersistenceController")
3537
}
3638
}
3739
}

InfiniLink/Utils/StepCountManager.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class StepCountManager: ObservableObject {
2323

2424
// The following two functions need to use the viewContext to save because the objects they're updating were fetched on that context
2525
func setStepCount(steps: Int32, isArbitrary: Bool, for date: Date) {
26-
let existingCounts = chartManager.stepPoints()
26+
let existing = chartManager.stepsToday()
2727

28-
if let existingCount = existingCounts.first(where: { Calendar.current.isDate($0.timestamp!, inSameDayAs: date) }) {
29-
updateStepCount(existingCount, with: steps, isArbitrary: isArbitrary, for: date)
28+
if let existing {
29+
updateStepCount(existing, with: steps, isArbitrary: isArbitrary, for: date)
3030
} else {
3131
chartManager.addStepDataPoint(steps: steps, time: date)
3232
}
@@ -42,24 +42,20 @@ class StepCountManager: ObservableObject {
4242

4343
stepCount.timestamp = date
4444

45-
Task {
46-
await persistenceManager.save()
47-
}
45+
persistenceManager.save()
4846
}
4947

5048
func clearCurrentDaySteps() {
51-
let today = Date()
52-
let existingCounts = chartManager.stepPoints()
49+
let now = Date()
50+
let existing = chartManager.stepsToday()
5351

54-
if let currentDayCount = existingCounts.first(where: { Calendar.current.isDate($0.timestamp!, inSameDayAs: today) }) {
55-
currentDayCount.steps = 0
56-
currentDayCount.timestamp = today
52+
if let existing {
53+
existing.steps = 0
54+
existing.timestamp = now
5755
} else {
58-
chartManager.addStepDataPoint(steps: 0, time: today)
56+
chartManager.addStepDataPoint(steps: 0, time: now)
5957
}
6058

61-
Task {
62-
await persistenceManager.save()
63-
}
59+
persistenceManager.save()
6460
}
6561
}

0 commit comments

Comments
 (0)