Proper 1.0 release for iOS. Pending App Store submission.
This commit is contained in:
50
ios/OpenClimb.xcodeproj/LiveActivityManager.swift
Normal file
50
ios/OpenClimb.xcodeproj/LiveActivityManager.swift
Normal file
@@ -0,0 +1,50 @@
|
||||
import ActivityKit
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
final class LiveActivityManager {
|
||||
static let shared = LiveActivityManager()
|
||||
private init() {}
|
||||
|
||||
private var currentActivity: Activity<SessionActivityAttributes>?
|
||||
|
||||
/// Call this when a ClimbSession starts to begin a Live Activity
|
||||
func startLiveActivity(for session: ClimbSession, gymName: String) async {
|
||||
await endLiveActivity()
|
||||
let attributes = SessionActivityAttributes(
|
||||
gymName: gymName, startTime: session.startTime ?? session.date)
|
||||
let initialContentState = SessionActivityAttributes.ContentState(
|
||||
elapsed: 0,
|
||||
totalAttempts: 0,
|
||||
completedProblems: 0
|
||||
)
|
||||
do {
|
||||
currentActivity = try Activity<SessionActivityAttributes>.request(
|
||||
attributes: attributes,
|
||||
contentState: initialContentState,
|
||||
pushType: nil
|
||||
)
|
||||
} catch {
|
||||
print("Failed to start live activity: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Call this to update the Live Activity with new session progress
|
||||
func updateLiveActivity(elapsed: TimeInterval, totalAttempts: Int, completedProblems: Int) async
|
||||
{
|
||||
guard let currentActivity else { return }
|
||||
let updatedContentState = SessionActivityAttributes.ContentState(
|
||||
elapsed: elapsed,
|
||||
totalAttempts: totalAttempts,
|
||||
completedProblems: completedProblems
|
||||
)
|
||||
await currentActivity.update(using: updatedContentState, alertConfiguration: nil)
|
||||
}
|
||||
|
||||
/// Call this when a ClimbSession ends to end the Live Activity
|
||||
func endLiveActivity() async {
|
||||
guard let currentActivity else { return }
|
||||
await currentActivity.end(using: nil, dismissalPolicy: .immediate)
|
||||
self.currentActivity = nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user