- 장시간 측정 알림: 측정이 설정한 시간(1/2/3/6/12시간, 기본 끔)을 넘기면 로컬 알림으로 종료를 잊었음을 알려준다. LiveActivityManager.sync 길목에서 진행 세션과 예약을 동기화하므로 앱·위젯·시리·워치 어느 경로로 시작/종료/수정해도 예약이 따라가고, 세션이 끝나거나 설정이 바뀌면 기존 예약이 제거된다 (식별자에 행동·시작시각·설정시간 포함). 설정 탭 '측정' 섹션에서 켜는 순간 권한 요청. - Live Activity 타이머 상한 30일 → 1년 (상한 도달로 타이머가 멈춰 보이던 엣지 제거) - 메모 시트(측정 종료/횟수 기록) 저장 시 명시적 context.save — autosave 의존 제거 - 검증용 -longSessionAlertTestSeconds(시간 대신 N초 발화)·-alertDump (예약 상태를 Documents/session-alerts.txt로) 추가. 시뮬레이터에서 예약 생성(진행 세션 2건)과 설정 해제 시 제거를 덤프로 확인 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013yKDMhuF39GVYy3FMcGHh7
91 lines
3.5 KiB
Swift
91 lines
3.5 KiB
Swift
//
|
|
// LiveActivityManager.swift
|
|
// Haru_Danim
|
|
//
|
|
// 추적 상태를 다이나믹 아일랜드/잠금화면 Live Activity에 반영 (CLAUDE.md §4.1, §4.2)
|
|
//
|
|
|
|
import Foundation
|
|
import ActivityKit
|
|
import SwiftData
|
|
|
|
enum LiveActivityManager {
|
|
/// 백그라운드(워치 명령으로 깨어난 상태 등)에서는 시스템이 Live Activity '시작'을
|
|
/// 허용하지 않으므로, 실패를 기억해 두고 앱이 포그라운드로 돌아올 때 재시도한다
|
|
static var pendingStartRetry = false
|
|
|
|
/// 현재 진행 중인 세션들을 읽어 Live Activity를 시작/갱신/종료한다.
|
|
/// 세션 시작·종료·수정 후마다 호출.
|
|
static func sync(context: ModelContext) {
|
|
let descriptor = FetchDescriptor<TimeSession>(
|
|
predicate: #Predicate { $0.endAt == nil },
|
|
sortBy: [SortDescriptor(\.startAt, order: .forward)]
|
|
)
|
|
let running = (try? context.fetch(descriptor)) ?? []
|
|
|
|
// 장시간 측정 알림 예약도 같은 길목에서 동기화 (Live Activity 허용 여부와 무관)
|
|
SessionAlertManager.sync(running: running)
|
|
|
|
guard ActivityAuthorizationInfo().areActivitiesEnabled else { return }
|
|
|
|
let mode = LiveActivityMode(
|
|
rawValue: AppGroup.defaults.string(forKey: SettingsKeys.liveActivityMode)
|
|
?? UserDefaults.standard.string(forKey: SettingsKeys.liveActivityMode) ?? ""
|
|
) ?? .latest
|
|
let representative = mode == .earliest ? running.first : running.last
|
|
|
|
guard let representative, let action = representative.action else {
|
|
pendingStartRetry = false
|
|
endAll()
|
|
return
|
|
}
|
|
|
|
let state = TrackingActivityAttributes.ContentState(
|
|
actionName: action.name,
|
|
symbolName: action.symbolName,
|
|
colorHex: action.sortedTags.first?.colorHex ?? "#2F6B4F",
|
|
startedAt: representative.startAt,
|
|
extraCount: max(running.count - 1, 0)
|
|
)
|
|
let content = ActivityContent(state: state, staleDate: nil)
|
|
|
|
Task {
|
|
let activities = Activity<TrackingActivityAttributes>.activities
|
|
if let activity = activities.first {
|
|
await activity.update(content)
|
|
pendingStartRetry = false
|
|
// 혹시 중복 생성된 것이 있으면 정리
|
|
for extra in activities.dropFirst() {
|
|
await extra.end(nil, dismissalPolicy: .immediate)
|
|
}
|
|
} else {
|
|
do {
|
|
_ = try Activity<TrackingActivityAttributes>.request(
|
|
attributes: TrackingActivityAttributes(),
|
|
content: content
|
|
)
|
|
pendingStartRetry = false
|
|
} catch {
|
|
// 워치 명령 등 백그라운드 컨텍스트에서는 시작이 거부된다(visibility)
|
|
// → 포그라운드 복귀 시 syncIfRetryNeeded가 다시 시도
|
|
pendingStartRetry = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 앱이 포그라운드로 돌아왔을 때, 백그라운드에서 거부됐던 시작을 재시도
|
|
static func syncIfRetryNeeded(context: ModelContext) {
|
|
guard pendingStartRetry else { return }
|
|
sync(context: context)
|
|
}
|
|
|
|
static func endAll() {
|
|
Task {
|
|
for activity in Activity<TrackingActivityAttributes>.activities {
|
|
await activity.end(nil, dismissalPolicy: .immediate)
|
|
}
|
|
}
|
|
}
|
|
}
|