- HealthWorkoutType(WorkoutConfig 필드, 워치 자동 동기화) — 폰·워치 저장 모두 반영 - 설정 → 애플 건강에 '운동 유형' 피커, 푸터·도움말 문구 유형 중립으로 갱신 - 신규 문자열 번역(en/ja), 옛 문구 stale 2건 정리 — 전 카탈로그 missing·stale 0 - 하루 다님 연계 조사 결과 CLAUDE.md 기록(종목 다짐·타임테이블=자동 집계, 운동 시간 지표=워치 실행분만 운동 링 경유 반영) - 검증: Debug·Release·워치 빌드 그린, 설정 화면 QA Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UPmp29DQHY6Ahfss5SRopT
54 lines
2.2 KiB
Swift
54 lines
2.2 KiB
Swift
//
|
|
// HealthRecorder.swift
|
|
// Keging
|
|
//
|
|
// 완주한 세트를 애플 건강에 '기타' 운동으로 기록 (iOS 측 — 사후 저장).
|
|
// 운동 강도(workout effort score, 1~10)도 설정값대로 함께 저장.
|
|
// 권한 거부·미응답이면 조용히 건너뛴다 — 앱 자체 기록·통계에는 영향 없음.
|
|
//
|
|
|
|
import HealthKit
|
|
|
|
@MainActor
|
|
final class HealthRecorder {
|
|
static let shared = HealthRecorder()
|
|
|
|
private let store = HKHealthStore()
|
|
private var authRequested = false
|
|
|
|
private init() {}
|
|
|
|
private static var shareTypes: Set<HKSampleType> {
|
|
[HKObjectType.workoutType(), HKQuantityType(.workoutEffortScore)]
|
|
}
|
|
|
|
/// 운동 시작 시 호출 — 첫 실행에서만 권한 시트가 뜬다 (운동 진행에는 영향 없음)
|
|
func requestAuthorizationIfNeeded() {
|
|
guard HKHealthStore.isHealthDataAvailable(), !authRequested else { return }
|
|
authRequested = true
|
|
store.requestAuthorization(toShare: Self.shareTypes, read: nil) { _, _ in }
|
|
}
|
|
|
|
/// 완주한 세트 저장 — 시작 시각부터 세트 길이만큼, 설정한 유형(기타/코어 트레이닝)으로
|
|
func save(record: SessionRecord, effortScore: Int, workoutType: HealthWorkoutType) {
|
|
guard HKHealthStore.isHealthDataAvailable() else { return }
|
|
let start = record.startedAt
|
|
let end = start.addingTimeInterval(Double(record.reps * (record.contractSeconds + record.relaxSeconds)))
|
|
let configuration = HKWorkoutConfiguration()
|
|
configuration.activityType = workoutType.activityType
|
|
let builder = HKWorkoutBuilder(healthStore: store, configuration: configuration, device: .local())
|
|
Task { @MainActor in
|
|
do {
|
|
try await HealthKitCalls.beginCollection(builder, at: start)
|
|
try await HealthKitCalls.endCollection(builder, at: end)
|
|
let workout = try await HealthKitCalls.finishWorkout(builder)
|
|
if effortScore > 0, let workout {
|
|
try? await HealthKitCalls.relateEffort(store: store, workout: workout, score: effortScore)
|
|
}
|
|
} catch {
|
|
// 권한 없음 등 — 무시
|
|
}
|
|
}
|
|
}
|
|
}
|