Skip to content

fix: align HealthKit sleep aggregation with Apple Health totals - #37

Merged
bisonbet merged 2 commits into
mainfrom
codex/fix-sleep-data-import-from-apple-health
May 19, 2026
Merged

fix: align HealthKit sleep aggregation with Apple Health totals#37
bisonbet merged 2 commits into
mainfrom
codex/fix-sleep-data-import-from-apple-health

Conversation

@bisonbet

Copy link
Copy Markdown
Owner

Motivation

  • Apple Health anchors nightly sleep summaries to the sleep session end (wake-day) and HealthKit samples can overlap, causing inflated or misattributed totals.
  • The existing logic grouped by startDate and summed raw sample durations, which can double-count overlapping intervals and split overnight sessions across two dates.

Description

  • Changed sleep-night grouping to anchor by each sample's endDate so overnight sessions map to the same date Apple Health uses, in HealthApp/HealthApp/Managers/HealthKitManager.swift.
  • Replaced naive duration summation with a union/merge of sample intervals to compute totals without double-counting by adding a helper calculateUnionMinutes(samples:) and using it for total sleep and in‑bed time.
  • Applied the union-based computation to iOS 16+ sleep stage calculations (deep, REM, core, awake) so stage totals are consistent with merged intervals.
  • Preserved start/end time selection from the earliest start and latest end of the relevant samples while setting source: .appleHealth for produced SleepData entries.

Testing

  • No automated build or test runs were executed for this change (per repository guideline DO NOT run xcodebuild unless explicitly requested).

Codex Task

…alculateUnionMinutes

- Add // MARK: - Private Helpers for file organization
- Use named tuple elements (start/end) for readability
- Guard against inverted start/end intervals from HealthKit
@bisonbet
bisonbet merged commit 49e3948 into main May 19, 2026
1 check failed
@bisonbet
bisonbet deleted the codex/fix-sleep-data-import-from-apple-health branch May 19, 2026 22:48

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines 559 to +560
let groupedSleep = Dictionary(grouping: samples) { sample -> Date in
let calendar = Calendar.current
return calendar.startOfDay(for: sample.startDate)
Calendar.current.startOfDay(for: sample.endDate)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant