- CHHapticEngine을 공유 AVAudioSession에 부착(기본 생성 시 독자 오디오 정책이 백그라운드 무음 루프를 끊던 것이 DI 멈춤 원인) + 오디오 키퍼 하드닝(루트 변경· 미디어 서버 리셋·ensureAlive) + 운동 중 2초 하트비트(오디오·엔진·LA 자가 치유) - Live Activity: 실제 phaseStart 사용·동일 상태 no-op·staleDate 8초·isStale 흐림 - 워치 세팅 동기화 3중화: 변경 push + reachability 재푸시 + 워치 앱 열릴 때 sendMessage pull - 완주 진동 신설: 폰 2.4초 연속(백그라운드 6방 체인, 오디오 정지 4초 지연), 워치 retry×2+success - 통계 하루 탭 맨 위 '오늘의 타임라인'(24시간 축 점 그래프, 시각 라벨·지금 룰) - 폰 칼로리 공식을 애플워치 '기타' 운동 규칙(빠르게 걷기 상당)에 정합: MET 4.0+강도×0.15 - 도움말 4건 갱신·en/ja 번역·CFBundleName 보충, CLAUDE.md 현행화 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UPmp29DQHY6Ahfss5SRopT
103 lines
3.5 KiB
Swift
103 lines
3.5 KiB
Swift
//
|
|
// WorkoutCoordinator.swift
|
|
// Keging
|
|
//
|
|
// 엔진 ↔ 진동·Live Activity·백그라운드 오디오·화면 꺼짐 방지 배선 (iOS).
|
|
// 운동 중에는 2초 하트비트로 오디오 생존·엔진 재동기화·Live Activity를 지속 점검한다
|
|
// (국면 경계 타이머가 한 번 놓쳐도 2초 안에 자가 치유 — 실기기 DI 멈춤의 3중 방어).
|
|
//
|
|
|
|
import UIKit
|
|
|
|
@MainActor
|
|
final class WorkoutCoordinator {
|
|
static let shared = WorkoutCoordinator()
|
|
|
|
private var wired = false
|
|
private var heartbeat: Timer?
|
|
/// 완주 진동(백그라운드 체인 ~2.7초)이 끝나기 전에 오디오 세션을 닫지 않기 위한 지연 정지
|
|
private var pendingAudioStop: Task<Void, Never>?
|
|
|
|
private init() {}
|
|
|
|
func wire() {
|
|
guard !wired else { return }
|
|
wired = true
|
|
|
|
LiveActivityManager.shared.cleanupStaleActivities()
|
|
|
|
let engine = KegelEngine.shared
|
|
engine.onPhaseChange = { [weak engine] phase, rep, phaseStart, phaseEnd in
|
|
guard let engine else { return }
|
|
let config = engine.config
|
|
switch phase {
|
|
case .prepare: break // 준비는 무진동 — 첫 수축 진동이 시작 신호
|
|
case .contract: Haptics.play(config.contractPattern)
|
|
case .relax: Haptics.play(config.relaxPattern)
|
|
}
|
|
LiveActivityManager.shared.sync(
|
|
phase: phase, rep: rep, totalReps: config.reps,
|
|
phaseStart: phaseStart, phaseEnd: phaseEnd
|
|
)
|
|
}
|
|
engine.onSessionEnd = { [weak self] completed, record in
|
|
self?.stopHeartbeat()
|
|
LiveActivityManager.shared.end()
|
|
UIApplication.shared.isIdleTimerDisabled = false
|
|
if completed {
|
|
Haptics.playFinish()
|
|
self?.stopAudioSoon()
|
|
} else {
|
|
BackgroundAudioKeeper.shared.stop()
|
|
}
|
|
if completed, let record {
|
|
let config = SettingsStore.shared.config
|
|
HealthRecorder.shared.save(
|
|
record: record,
|
|
effortScore: config.effortScore,
|
|
workoutType: config.healthWorkoutType
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func startWorkout() {
|
|
guard !KegelEngine.shared.isRunning else { return }
|
|
pendingAudioStop?.cancel()
|
|
pendingAudioStop = nil
|
|
HealthRecorder.shared.requestAuthorizationIfNeeded()
|
|
BackgroundAudioKeeper.shared.start()
|
|
UIApplication.shared.isIdleTimerDisabled = true
|
|
KegelEngine.shared.start(config: SettingsStore.shared.config)
|
|
startHeartbeat()
|
|
}
|
|
|
|
// MARK: 하트비트
|
|
|
|
private func startHeartbeat() {
|
|
heartbeat?.invalidate()
|
|
heartbeat = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { _ in
|
|
Task { @MainActor in
|
|
guard KegelEngine.shared.isRunning else { return }
|
|
BackgroundAudioKeeper.shared.ensureAlive()
|
|
KegelEngine.shared.resyncAfterWake()
|
|
LiveActivityManager.shared.syncCurrent()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func stopHeartbeat() {
|
|
heartbeat?.invalidate()
|
|
heartbeat = nil
|
|
}
|
|
|
|
private func stopAudioSoon() {
|
|
pendingAudioStop?.cancel()
|
|
pendingAudioStop = Task { @MainActor in
|
|
try? await Task.sleep(for: .seconds(4))
|
|
guard !Task.isCancelled, !KegelEngine.shared.isRunning else { return }
|
|
BackgroundAudioKeeper.shared.stop()
|
|
}
|
|
}
|
|
}
|