38 lines
1.4 KiB
Swift
38 lines
1.4 KiB
Swift
// Allow and Deny on the Smart Stack widget. WidgetKit runs the intent without opening the app, then
|
|
// reloads the widget's timeline. A request someone already answered (409) counts as done.
|
|
//
|
|
// The app target compiles this file too, because WaitingView builds the buttons.
|
|
|
|
import AppIntents
|
|
|
|
struct RespondIntent: AppIntent {
|
|
static let title: LocalizedStringResource = "Answer an approval"
|
|
static let isDiscoverable = false
|
|
|
|
@Parameter(title: "Host") var hostId: String
|
|
@Parameter(title: "Thread") var threadId: String
|
|
@Parameter(title: "Request") var requestId: String
|
|
@Parameter(title: "Decision") var decision: String
|
|
|
|
init() {}
|
|
|
|
init(_ approval: WaitingEntry.Approval, decision: ApprovalDecision) {
|
|
hostId = approval.hostId
|
|
threadId = approval.threadId
|
|
requestId = approval.requestId
|
|
self.decision = decision.rawValue
|
|
}
|
|
|
|
func perform() async throws -> some IntentResult {
|
|
guard let api = SharedState.api else { return .result() }
|
|
do {
|
|
try await api.respond(
|
|
hostId: hostId, threadId: threadId,
|
|
request: .decision(requestId: requestId, decision: ApprovalDecision(rawValue: decision)))
|
|
} catch let error as GatewayError where error.isStale {
|
|
// Answered already, on the watch or elsewhere. The reload shows what is next.
|
|
}
|
|
return .result()
|
|
}
|
|
}
|