mycode/myApp/Keging/Shared/KegelEngine.swift
songyc macbook 43448b35f3 feat(keging): 1.0 전 기능 구현 — iOS 타이머·진동 패턴·통계·다이나믹 아일랜드·단축어·워치 앱
- 세트 엔진(수축→이완×횟수, 중단=미기록) iOS·워치 공용, 완주만 records.json 저장
- 첫 화면 시작 버튼 + 세팅(시간·횟수·진동 패턴)/설정(테마) 시트, 아래 스와이프 통계
- 통계: 하루(시간대별)/주간/월간 꺾은선, 달력 기준↔롤링(지난 7일·30일) 전환
- Live Activity(수축/이완·n/총·카운트다운), 백그라운드 무음 오디오로 타이머·진동 유지
- 단축어 '케겔 운동 시작', 워치 앱(즉시 시작·워치 전용 진동·physical-therapy 세션·기록 폰 병합)
- 하루 다님 팔레트 계승(라이트/다크/시스템), 앱 아이콘 SVG→PNG 등록
- 검증: Debug·Release·워치 빌드 그린, 시뮬 QA 10장(26·18.5·워치 46/40mm), 완주·기록 경로 확인

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UPmp29DQHY6Ahfss5SRopT
2026-08-29 10:08:20 +09:00

104 lines
3.2 KiB
Swift

//
// KegelEngine.swift
// Keging
//
// iOS· . onPhaseChange(·Live Activity),
// onSessionEnd( + ). .
//
import Foundation
import Combine
nonisolated enum KegelPhase: String, Codable, Hashable {
case contract, relax
var label: String { self == .contract ? "수축" : "이완" }
var symbolName: String {
self == .contract ? "arrow.down.right.and.arrow.up.left" : "arrow.up.left.and.arrow.down.right"
}
}
@MainActor
final class KegelEngine: ObservableObject {
static let shared = KegelEngine()
enum State: Equatable {
case idle
case running(phase: KegelPhase, rep: Int, phaseStart: Date, phaseEnd: Date)
case finished(reps: Int)
}
@Published private(set) var state: State = .idle
private(set) var config = WorkoutConfig()
private(set) var startedAt = Date()
private var timer: Timer?
/// (phase, rep, phaseEnd)
var onPhaseChange: ((KegelPhase, Int, Date) -> Void)?
/// (completed, )
var onSessionEnd: ((Bool, SessionRecord?) -> Void)?
var isRunning: Bool { if case .running = state { true } else { false } }
private init() {}
func start(config: WorkoutConfig) {
guard !isRunning else { return }
self.config = config
startedAt = Date()
enterPhase(.contract, rep: 1)
}
/// ( )
func stop() {
guard isRunning else { return }
timer?.invalidate()
timer = nil
state = .idle
onSessionEnd?(false, nil)
}
///
func acknowledgeFinish() {
if case .finished = state { state = .idle }
}
private func enterPhase(_ phase: KegelPhase, rep: Int) {
let seconds = phase == .contract ? config.contractSeconds : config.relaxSeconds
let now = Date()
let end = now.addingTimeInterval(Double(max(1, seconds)))
state = .running(phase: phase, rep: rep, phaseStart: now, phaseEnd: end)
onPhaseChange?(phase, rep, end)
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: Double(max(1, seconds)), repeats: false) { [weak self] _ in
guard let self else { return }
Task { @MainActor in self.advance(from: phase, rep: rep) }
}
}
private func advance(from phase: KegelPhase, rep: Int) {
guard case .running = state else { return }
if phase == .contract {
enterPhase(.relax, rep: rep)
} else if rep < config.reps {
enterPhase(.contract, rep: rep + 1)
} else {
complete()
}
}
private func complete() {
timer?.invalidate()
timer = nil
state = .finished(reps: config.reps)
let record = SessionRecord(
startedAt: startedAt,
reps: config.reps,
contractSeconds: config.contractSeconds,
relaxSeconds: config.relaxSeconds
)
RecordStore.shared.append(record)
onSessionEnd?(true, record)
}
}