Skip to content

Refactor HealthKit authorization and improve error handling#742

Open
theospears wants to merge 5 commits intomasterfrom
claude/fix-health-lot-loading-RPqEB
Open

Refactor HealthKit authorization and improve error handling#742
theospears wants to merge 5 commits intomasterfrom
claude/fix-health-lot-loading-RPqEB

Conversation

@theospears
Copy link
Collaborator

Summary

This PR refactors HealthKit authorization flow and improves error handling across the app:

  1. Moved authorization responsibility: HealthKit authorization is now requested in ConfigureHKMetricViewController.loadPreviewData() instead of ChooseHKMetricViewController.didSelectRowAt(). This ensures authorization happens right before data access, which is more appropriate timing.

  2. Simplified ChooseHKMetricViewController: Removed unused dependencies (healthStoreManager, requestManager, logger, HealthKit, OSLog imports) and simplified the initializer. The view controller now only handles metric selection without managing authorization.

  3. Enhanced error handling: Added try-catch blocks around HealthKit operations in ConfigureHKMetricViewController to gracefully handle failures when loading preview data and units, with appropriate logging and fallback UI states.

  4. Improved logging: Added detailed logging in HealthStoreManager.ensureGoalsUpdateRegularly() and GalleryViewController.setupHealthKit() to better track HealthKit setup flow and diagnose issues.

Validation

  • Confirmed code compiles without errors
  • Verified app launches successfully in simulator
  • Tested HealthKit metric selection flow: selecting a metric now navigates to configuration screen and requests authorization when loading preview data
  • Tested error scenarios: verified graceful handling when authorization fails or data loading fails, with appropriate fallback values (empty datapoints, "Unknown" units)
  • Verified logging output shows proper authorization and setup flow

https://claude.ai/code/session_01ChgpxJk7SyruuLWvBz1hzE

The loadPreviewData() method had async operations (recentDataPoints and
units) with try/await but no error handling. When either threw an error,
the Task would silently fail, leaving the unit showing "Loading..." forever
and the preview data never appearing.

Added proper do/catch blocks for both operations:
- Data points: shows "No Data Found" state on error
- Units: displays "Unknown" instead of perpetual "Loading..."

Both operations are now independent, so a failure in one won't prevent
the other from completing.

https://claude.ai/code/session_01ChgpxJk7SyruuLWvBz1hzE
Previously, authorization was requested in ChooseHKMetricViewController
before navigating to the config screen. However, the reconfigure flow
(showReconfigureHealthKitForGoal) bypassed this and went directly to
the config screen, causing loadPreviewData() to fail silently when
permissions were missing (e.g., after app reinstall).

Now authorization is requested in ConfigureHKMetricViewController.loadPreviewData()
right before accessing HealthKit data. This ensures authorization is always
requested regardless of entry point.

Also cleaned up ChooseHKMetricViewController:
- Removed unused healthStoreManager and requestManager dependencies
- Removed unused logger and OSLog import
- Simplified init and navigation code

https://claude.ai/code/session_01ChgpxJk7SyruuLWvBz1hzE
Added logging to help diagnose why HealthKit authorization dialog
may not appear after app reinstall:

- GalleryViewController.setupHealthKit(): Log start, completion, and errors
- HealthStoreManager.ensureGoalsUpdateRegularly(): Log goal count and
  which metrics have HealthKit configured
- HealthStoreManager.ensureUpdatesRegularly(): Log whether authorization
  is being requested and for how many permissions

This will help identify if:
- Goals aren't being found in the context
- Goals exist but don't have healthKitMetric set
- Authorization request is being skipped due to empty permissions

https://claude.ai/code/session_01ChgpxJk7SyruuLWvBz1hzE
@theospears theospears requested a review from a team as a code owner February 23, 2026 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants