// // HealthKitCalls.swift // Keging // // HealthKit completion API의 async 래퍼 — iOS(사후 빌더)·워치(라이브 빌더) 공용 // import HealthKit nonisolated enum HealthKitCalls { static func beginCollection(_ builder: HKWorkoutBuilder, at date: Date) async throws { try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in builder.beginCollection(withStart: date) { _, error in if let error { continuation.resume(throwing: error) } else { continuation.resume() } } } } static func endCollection(_ builder: HKWorkoutBuilder, at date: Date) async throws { try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in builder.endCollection(withEnd: date) { _, error in if let error { continuation.resume(throwing: error) } else { continuation.resume() } } } } static func finishWorkout(_ builder: HKWorkoutBuilder) async throws -> HKWorkout? { try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in builder.finishWorkout { workout, error in if let error { continuation.resume(throwing: error) } else { continuation.resume(returning: workout) } } } } /// 운동 강도(1~10) 샘플을 운동에 연결 — 애플 운동 앱의 강도 기록과 같은 데이터 @available(iOS 18.0, watchOS 11.0, *) static func relateEffort(store: HKHealthStore, workout: HKWorkout, score: Int) async throws { let sample = HKQuantitySample( type: HKQuantityType(.workoutEffortScore), quantity: HKQuantity(unit: .appleEffortScore(), doubleValue: Double(min(max(score, 1), 10))), start: workout.startDate, end: workout.endDate ) try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in store.relateWorkoutEffortSample(sample, with: workout, activity: nil) { _, error in if let error { continuation.resume(throwing: error) } else { continuation.resume() } } } } }