You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LeCountdown/LeCountdown/Intent/StartTimerIntent.swift

60 lines
1.8 KiB

//
// LaunchTimer.swift
// LeCountdown
//
// Created by Laurent Morvillier on 07/03/2023.
//
import Foundation
import AppIntents
import SwiftUI
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
struct StartTimerIntent: AudioStartingIntent {
static let intentClassName = "StartTimerIntent"
static var title: LocalizedStringResource = "Launch Timer"
static var description = IntentDescription("Launch timers and stopwatches")
@Parameter(title: "Timer")
var timer: TimerIdentifierAppEntity?
static var parameterSummary: some ParameterSummary {
Summary("")
}
static var openAppWhenRun: Bool = true
func perform() async throws -> some IntentResult {
let timerIdentifier: TimerIdentifierAppEntity
if let timer {
timerIdentifier = timer
} else {
timerIdentifier = try await $timer.requestDisambiguation(among: timerEntities())
}
if let abstractTimer = IntentDataProvider.main.timer(id: timerIdentifier.id) {
do {
let _ = try await TimerRouter.performAction(timer: abstractTimer)
print("perform() success !")
return .result(value: 1)
} catch {
Logger.error(error)
// let dialog: IntentDialog = "\(error.localizedDescription)"
return .result(value: 0)
}
}
return .result(value: 0)
}
func timerEntities() -> [TimerIdentifierAppEntity] {
do {
let timers: [AbstractTimer] = try IntentDataProvider.main.timers()
return timers.map { TimerIdentifierAppEntity(id: $0.stringId, displayString: $0.displayName) }
} catch {
return []
}
}
}