- 메인: 텍스트 한 줄 요약 → 수축·이완·횟수 카드 3장(아이콘+큰 숫자, 탭=세팅) - 운동 화면: 국면 색 전환(수축=그린·이완=앰버, 카운터 색 연동) + 세트 진행 링 + 큰 현재 횟수 카운터(numericText 전환) - 도움말 취소선 원인 = SwiftUI Text 마크다운의 ~쌍 해석 — 사용자 문구의 ASCII ~를 전각 ~로 전면 교체(4곳), CLAUDE.md에 금지 규칙 기록 - 신규 QA 인자 -noHealth(권한 시트가 캡처 가리는 것 방지) - 검증: Debug·Release 빌드 그린, 화면 QA(메인 카드·수축/이완 국면·도움말), 카탈로그 0/0 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UPmp29DQHY6Ahfss5SRopT
58 lines
2.4 KiB
Swift
58 lines
2.4 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() {
|
|
#if DEBUG
|
|
// 화면 QA에서 권한 시트가 캡처를 가리는 것 방지
|
|
if CommandLine.arguments.contains("-noHealth") { return }
|
|
#endif
|
|
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 {
|
|
// 권한 없음 등 — 무시
|
|
}
|
|
}
|
|
}
|
|
}
|