Refactor HealthKit authorization and improve error handling#742
Open
theospears wants to merge 5 commits intomasterfrom
Open
Refactor HealthKit authorization and improve error handling#742theospears wants to merge 5 commits intomasterfrom
theospears wants to merge 5 commits intomasterfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors HealthKit authorization flow and improves error handling across the app:
Moved authorization responsibility: HealthKit authorization is now requested in
ConfigureHKMetricViewController.loadPreviewData()instead ofChooseHKMetricViewController.didSelectRowAt(). This ensures authorization happens right before data access, which is more appropriate timing.Simplified ChooseHKMetricViewController: Removed unused dependencies (
healthStoreManager,requestManager,logger,HealthKit,OSLogimports) and simplified the initializer. The view controller now only handles metric selection without managing authorization.Enhanced error handling: Added try-catch blocks around HealthKit operations in
ConfigureHKMetricViewControllerto gracefully handle failures when loading preview data and units, with appropriate logging and fallback UI states.Improved logging: Added detailed logging in
HealthStoreManager.ensureGoalsUpdateRegularly()andGalleryViewController.setupHealthKit()to better track HealthKit setup flow and diagnose issues.Validation
https://claude.ai/code/session_01ChgpxJk7SyruuLWvBz1hzE