Fix HealthKit authorization and sync issues
- Consolidate permissions into single dialog (1 write + 5 read types) - Add retry logic to wait for iOS authorization status update - Show real-time sync status feedback in settings - Reduce batch size from 100 to 50 for smoother progress updates Fixes: toggle appearing to do nothing, only one permission showing, past moods not syncing to State of Mind 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -206,6 +206,8 @@ struct SettingsContentView: View {
|
||||
|
||||
// MARK: - Health Kit Toggle
|
||||
|
||||
@ObservedObject private var healthKitManager = HealthKitManager.shared
|
||||
|
||||
private var healthKitToggle: some View {
|
||||
VStack(spacing: 0) {
|
||||
HStack(spacing: 12) {
|
||||
@@ -231,17 +233,20 @@ struct SettingsContentView: View {
|
||||
set: { newValue in
|
||||
if newValue {
|
||||
Task {
|
||||
// Request read permissions for health insights
|
||||
let readSuccess = await healthService.requestAuthorization()
|
||||
// Request write permissions for State of Mind sync
|
||||
// Request all permissions in a single dialog
|
||||
do {
|
||||
try await HealthKitManager.shared.requestAuthorization()
|
||||
// Sync all existing moods to HealthKit
|
||||
await HealthKitManager.shared.syncAllMoods()
|
||||
let authorized = try await HealthKitManager.shared.requestAllPermissions()
|
||||
healthService.isEnabled = true
|
||||
healthService.isAuthorized = true
|
||||
|
||||
if authorized {
|
||||
// Sync all existing moods to HealthKit
|
||||
await HealthKitManager.shared.syncAllMoods()
|
||||
} else {
|
||||
EventLogger.log(event: "healthkit_state_of_mind_not_authorized")
|
||||
}
|
||||
} catch {
|
||||
print("HealthKit write authorization failed: \(error)")
|
||||
}
|
||||
if !readSuccess {
|
||||
print("HealthKit authorization failed: \(error)")
|
||||
EventLogger.log(event: "healthkit_enable_failed")
|
||||
}
|
||||
}
|
||||
@@ -260,14 +265,16 @@ struct SettingsContentView: View {
|
||||
}
|
||||
.padding()
|
||||
|
||||
// Show sync progress
|
||||
if HealthKitManager.shared.isSyncing {
|
||||
// Show sync progress or status
|
||||
if healthKitManager.isSyncing || !healthKitManager.syncStatus.isEmpty {
|
||||
VStack(spacing: 4) {
|
||||
ProgressView(value: HealthKitManager.shared.syncProgress)
|
||||
.tint(.red)
|
||||
Text("Syncing moods to Health... \(HealthKitManager.shared.syncedCount)/\(HealthKitManager.shared.totalToSync)")
|
||||
if healthKitManager.isSyncing {
|
||||
ProgressView(value: healthKitManager.syncProgress)
|
||||
.tint(.red)
|
||||
}
|
||||
Text(healthKitManager.syncStatus)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.foregroundStyle(healthKitManager.syncStatus.contains("✓") ? .green : .secondary)
|
||||
}
|
||||
.padding(.horizontal)
|
||||
.padding(.bottom, 12)
|
||||
@@ -683,6 +690,8 @@ struct SettingsView: View {
|
||||
|
||||
// MARK: - Health Kit Toggle
|
||||
|
||||
@ObservedObject private var healthKitManager = HealthKitManager.shared
|
||||
|
||||
private var healthKitToggle: some View {
|
||||
VStack(spacing: 0) {
|
||||
HStack(spacing: 12) {
|
||||
@@ -708,17 +717,20 @@ struct SettingsView: View {
|
||||
set: { newValue in
|
||||
if newValue {
|
||||
Task {
|
||||
// Request read permissions for health insights
|
||||
let readSuccess = await healthService.requestAuthorization()
|
||||
// Request write permissions for State of Mind sync
|
||||
// Request all permissions in a single dialog
|
||||
do {
|
||||
try await HealthKitManager.shared.requestAuthorization()
|
||||
// Sync all existing moods to HealthKit
|
||||
await HealthKitManager.shared.syncAllMoods()
|
||||
let authorized = try await HealthKitManager.shared.requestAllPermissions()
|
||||
healthService.isEnabled = true
|
||||
healthService.isAuthorized = true
|
||||
|
||||
if authorized {
|
||||
// Sync all existing moods to HealthKit
|
||||
await HealthKitManager.shared.syncAllMoods()
|
||||
} else {
|
||||
EventLogger.log(event: "healthkit_state_of_mind_not_authorized")
|
||||
}
|
||||
} catch {
|
||||
print("HealthKit write authorization failed: \(error)")
|
||||
}
|
||||
if !readSuccess {
|
||||
print("HealthKit authorization failed: \(error)")
|
||||
EventLogger.log(event: "healthkit_enable_failed")
|
||||
}
|
||||
}
|
||||
@@ -737,14 +749,16 @@ struct SettingsView: View {
|
||||
}
|
||||
.padding()
|
||||
|
||||
// Show sync progress
|
||||
if HealthKitManager.shared.isSyncing {
|
||||
// Show sync progress or status
|
||||
if healthKitManager.isSyncing || !healthKitManager.syncStatus.isEmpty {
|
||||
VStack(spacing: 4) {
|
||||
ProgressView(value: HealthKitManager.shared.syncProgress)
|
||||
.tint(.red)
|
||||
Text("Syncing moods to Health... \(HealthKitManager.shared.syncedCount)/\(HealthKitManager.shared.totalToSync)")
|
||||
if healthKitManager.isSyncing {
|
||||
ProgressView(value: healthKitManager.syncProgress)
|
||||
.tint(.red)
|
||||
}
|
||||
Text(healthKitManager.syncStatus)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.foregroundStyle(healthKitManager.syncStatus.contains("✓") ? .green : .secondary)
|
||||
}
|
||||
.padding(.horizontal)
|
||||
.padding(.bottom, 12)
|
||||
|
||||
Reference in New Issue
Block a user