fix: align HealthKit sleep aggregation with Apple Health totals - #37
Conversation
…alculateUnionMinutes - Add // MARK: - Private Helpers for file organization - Use named tuple elements (start/end) for readability - Guard against inverted start/end intervals from HealthKit
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b08327613f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let groupedSleep = Dictionary(grouping: samples) { sample -> Date in | ||
| let calendar = Calendar.current | ||
| return calendar.startOfDay(for: sample.startDate) | ||
| Calendar.current.startOfDay(for: sample.endDate) |
There was a problem hiding this comment.
Preserve night-of date when grouping sleep sessions
Grouping by sample.endDate shifts overnight sessions to the wake day, but the rest of the app still treats SleepData.date as the “night of” date (see SleepData docs and same-day manual conflict logic in mergeSleepData). In practice, a manual entry logged for the bedtime date (for example, May 18) will no longer conflict with HealthKit data for the same night now dated May 19, so both entries can appear and distort recent-sleep summaries.
Useful? React with 👍 / 👎.
Motivation
startDateand summed raw sample durations, which can double-count overlapping intervals and split overnight sessions across two dates.Description
endDateso overnight sessions map to the same date Apple Health uses, inHealthApp/HealthApp/Managers/HealthKitManager.swift.calculateUnionMinutes(samples:)and using it for total sleep and in‑bed time.deep,REM,core,awake) so stage totals are consistent with merged intervals.source: .appleHealthfor producedSleepDataentries.Testing
DO NOT run xcodebuildunless explicitly requested).Codex Task