Add HealthBridge iOS app for intelligent health data synchronization
Complete implementation of a SwiftUI iOS app that serves as a "Single Source of Truth" for health data. The app reads from all Apple Health sources, detects conflicts between devices, merges data using configurable strategies, and writes cleaned data back. Features: - Phase 1: HealthKit integration with automatic source discovery - Phase 2: DataReader with conflict detection (time-window based) - Phase 3: RuleEngine with 8 merge strategies (exclusive, priority, higher wins, etc.) - Phase 4: MergeEngine for conflict resolution + DataWriter for HealthKit writes - Phase 5: SwiftUI UI for dashboard, conflicts, rules, and sources management - Phase 6: Background sync with configurable intervals and push notifications - Phase 7: Complete rule editor and polished UI components Supported data types: - Steps, Heart Rate, Blood Pressure, SpO2, Sleep - Distance, Floors Climbed, Active Energy, HRV, Respiratory Rate Architecture: SourceManager -> DataReader -> RuleEngine -> MergeEngine -> DataWriter
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import SwiftUI
|
||||
import HealthKit
|
||||
import BackgroundTasks
|
||||
|
||||
@main
|
||||
struct HealthBridgeApp: App {
|
||||
@StateObject private var appState = AppState()
|
||||
@StateObject private var healthKitManager = HealthKitManager.shared
|
||||
@StateObject private var syncCoordinator = SyncCoordinator.shared
|
||||
|
||||
init() {
|
||||
registerBackgroundTasks()
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.environmentObject(appState)
|
||||
.environmentObject(healthKitManager)
|
||||
.environmentObject(syncCoordinator)
|
||||
.onAppear {
|
||||
Task {
|
||||
await requestHealthKitAuthorization()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func registerBackgroundTasks() {
|
||||
BGTaskScheduler.shared.register(
|
||||
forTaskWithIdentifier: "com.healthbridge.sync",
|
||||
using: nil
|
||||
) { task in
|
||||
guard let bgTask = task as? BGAppRefreshTask else { return }
|
||||
handleBackgroundSync(task: bgTask)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleBackgroundSync(task: BGAppRefreshTask) {
|
||||
scheduleNextBackgroundSync()
|
||||
|
||||
let syncTask = Task {
|
||||
do {
|
||||
try await syncCoordinator.performSync()
|
||||
task.setTaskCompleted(success: true)
|
||||
} catch {
|
||||
task.setTaskCompleted(success: false)
|
||||
}
|
||||
}
|
||||
|
||||
task.expirationHandler = {
|
||||
syncTask.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private func scheduleNextBackgroundSync() {
|
||||
let request = BGAppRefreshTaskRequest(identifier: "com.healthbridge.sync")
|
||||
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // 15 min
|
||||
|
||||
do {
|
||||
try BGTaskScheduler.shared.submit(request)
|
||||
} catch {
|
||||
print("Failed to schedule background sync: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
private func requestHealthKitAuthorization() async {
|
||||
do {
|
||||
try await healthKitManager.requestAuthorization()
|
||||
} catch {
|
||||
print("HealthKit authorization failed: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - App State
|
||||
@MainActor
|
||||
class AppState: ObservableObject {
|
||||
@Published var selectedTab: Tab = .dashboard
|
||||
@Published var showingConflictDetail: Conflict?
|
||||
@Published var isLoading = false
|
||||
@Published var lastSyncDate: Date?
|
||||
@Published var pendingConflicts: [Conflict] = []
|
||||
|
||||
enum Tab {
|
||||
case dashboard
|
||||
case conflicts
|
||||
case rules
|
||||
case sources
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user