Some checks failed
CI / test (push) Has been cancelled
Bring the SwiftUI app in line with the Apple-native mock and keep pending approvals actionable from Live Activities and watch complications.
64 lines
2.1 KiB
Swift
64 lines
2.1 KiB
Swift
import Foundation
|
|
|
|
#if canImport(ActivityKit) && os(iOS)
|
|
import ActivityKit
|
|
|
|
enum ApprovalActivityController {
|
|
static func sync(requests: [ApprovalRequest], profile: MemberProfile) async {
|
|
let pendingRequest = requests.first(where: { $0.status == .pending })
|
|
|
|
guard let pendingRequest else {
|
|
await endAll()
|
|
return
|
|
}
|
|
|
|
let payload = pendingRequest.activityPayload(handle: profile.handle)
|
|
let contentState = ApprovalActivityAttributes.ContentState(
|
|
requestID: payload.requestID,
|
|
title: payload.title,
|
|
appName: payload.appName,
|
|
source: payload.source,
|
|
handle: payload.handle,
|
|
location: payload.location
|
|
)
|
|
let content = ActivityContent(state: contentState, staleDate: pendingRequest.activityExpiryDate)
|
|
|
|
if let currentActivity = Activity<ApprovalActivityAttributes>.activities.first(where: { $0.attributes.requestID == payload.requestID }) {
|
|
await currentActivity.update(content)
|
|
} else {
|
|
do {
|
|
_ = try Activity.request(
|
|
attributes: ApprovalActivityAttributes(requestID: payload.requestID, createdAt: payload.createdAt),
|
|
content: content
|
|
)
|
|
} catch {
|
|
}
|
|
}
|
|
|
|
for activity in Activity<ApprovalActivityAttributes>.activities where activity.attributes.requestID != payload.requestID {
|
|
await activity.end(nil, dismissalPolicy: .immediate)
|
|
}
|
|
}
|
|
|
|
static func sync(requests: [ApprovalRequest], profile: MemberProfile?) async {
|
|
guard let profile else {
|
|
await endAll()
|
|
return
|
|
}
|
|
|
|
await sync(requests: requests, profile: profile)
|
|
}
|
|
|
|
static func endAll() async {
|
|
for activity in Activity<ApprovalActivityAttributes>.activities {
|
|
await activity.end(nil, dismissalPolicy: .immediate)
|
|
}
|
|
}
|
|
}
|
|
#else
|
|
enum ApprovalActivityController {
|
|
static func sync(requests: [ApprovalRequest], profile: MemberProfile?) async {}
|
|
static func endAll() async {}
|
|
}
|
|
#endif
|