Add HealthKit backfill to sync all existing moods

When user enables HealthKit integration, automatically syncs all
previously recorded mood entries to Apple Health. Features:
- Progress tracking with visual indicator in Settings
- Batched saves with throttling to avoid overwhelming HealthKit
- Success/failure logging with EventLogger

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-12-20 00:55:25 -06:00
parent 356ce9ea62
commit 373d481613
2 changed files with 106 additions and 0 deletions

View File

@@ -238,6 +238,8 @@ struct SettingsContentView: View {
// Request write permissions for State of Mind sync
do {
try await HealthKitManager.shared.requestAuthorization()
// Sync all existing moods to HealthKit
await HealthKitManager.shared.syncAllMoods()
} catch {
print("HealthKit write authorization failed: \(error)")
}
@@ -259,6 +261,19 @@ struct SettingsContentView: View {
}
}
.padding()
// Show sync progress
if HealthKitManager.shared.isSyncing {
VStack(spacing: 8) {
ProgressView(value: HealthKitManager.shared.syncProgress)
.tint(.red)
Text("Syncing moods to Health... \(HealthKitManager.shared.syncedCount)/\(HealthKitManager.shared.totalToSync)")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.horizontal)
.padding(.bottom, 8)
}
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
@@ -708,6 +723,8 @@ struct SettingsView: View {
// Request write permissions for State of Mind sync
do {
try await HealthKitManager.shared.requestAuthorization()
// Sync all existing moods to HealthKit
await HealthKitManager.shared.syncAllMoods()
} catch {
print("HealthKit write authorization failed: \(error)")
}
@@ -729,6 +746,19 @@ struct SettingsView: View {
}
}
.padding()
// Show sync progress
if HealthKitManager.shared.isSyncing {
VStack(spacing: 8) {
ProgressView(value: HealthKitManager.shared.syncProgress)
.tint(.red)
Text("Syncing moods to Health... \(HealthKitManager.shared.syncedCount)/\(HealthKitManager.shared.totalToSync)")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.horizontal)
.padding(.bottom, 8)
}
}
.fixedSize(horizontal: false, vertical: true)
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])