Fix permission card flash using PermissionsRequestState enum#16
Open
gsbernstein wants to merge 4 commits into
Open
Fix permission card flash using PermissionsRequestState enum#16gsbernstein wants to merge 4 commits into
gsbernstein wants to merge 4 commits into
Conversation
Persist HealthKit authorization request state across launches and show a loading indicator while the initial authorization check runs, instead of defaulting to the permission card before async HealthKit work completes. Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
Represent loading as nil, needs-permission as false, and ready as true. Removes the separate isCheckingAuthorization flag and UserDefaults persistence. Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
Replace optional Bool with loading, shouldRequest, and hasRequested cases. Document why read grant status is not a separate enum case. Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses a UI flash on cold start where the “HealthKit Access Required” card appeared briefly for users who already granted permissions, by introducing an explicit authorization UI state machine.
Changes:
- Added a
PermissionsRequestStateenum to represent.loading,.shouldRequest, and.hasRequestedstates for the read-authorization flow. - Replaced the previous boolean authorization flag with the new state enum in
HealthKitManager. - Updated
ContentViewto switch between aProgressView, the authorization card, and the main content based onpermissionsRequestState.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Bedtime/Bedtime/Models/HealthKitManager.swift | Introduces PermissionsRequestState and updates authorization/fetch logic to drive UI state explicitly. |
| Bedtime/Bedtime/ContentView.swift | Switches authorization UI rendering from a boolean flag to the new state enum, including a loading indicator. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
80
to
88
| func fetchSleepData() async throws { | ||
| defer { | ||
| if permissionsRequestState == .loading { | ||
| permissionsRequestState = .shouldRequest | ||
| } | ||
| } | ||
| try await requestAuthorization() | ||
| try await loadSleepData() | ||
| } |
Co-authored-by: Greg <gsbernstein@users.noreply.github.com>
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.
Problem
On every cold start, the app briefly showed the "HealthKit Access Required" card even for users who had already granted sleep data permissions.
hasRequestedAuthorizationdefaulted tofalse, which conflated "still checking" with "needs permission."Solution
Introduce
PermissionsRequestStatewith three explicit cases:.loading— startup authorization check in flight (ProgressView).shouldRequest— check finished; show permission card.hasRequested—requestAuthorizationcompleted this session; show main contentfetchSleepData()now captures thrown errors intoerrorMessagebefore rethrowing, so the permission card shows an explanation when startup auth fails (even though.taskusestry?).Why no separate "has permission" case?
HealthKit does not report read authorization status for privacy —
authorizationStatus(for:)is only meaningful for write/share types. For sleep read access, the only signal is whetherrequestAuthorization()completes (returning users reach.hasRequestedsilently without the system sheet).