| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import AppIntents
- import Foundation
- @available(iOS 16.0, *) struct ApplyTempPresetIntent: AppIntent {
- // Title of the action in the Shortcuts app
- static var title: LocalizedStringResource = "Apply a temporary target"
- // Description of the action in the Shortcuts app
- static var description = IntentDescription("Enable a temporary target")
- internal var intentRequest: TempPresetsIntentRequest
- init() {
- intentRequest = TempPresetsIntentRequest()
- }
- @Parameter(title: "Preset") var preset: TempPreset?
- @Parameter(
- title: "Confirm Before applying",
- description: "If toggled, you will need to confirm before applying",
- default: true
- ) var confirmBeforeApplying: Bool
- static var parameterSummary: some ParameterSummary {
- When(\ApplyTempPresetIntent.$confirmBeforeApplying, .equalTo, true, {
- Summary("Applying \(\.$preset)") {
- \.$confirmBeforeApplying
- }
- }, otherwise: {
- Summary("Immediately applying \(\.$preset)") {
- \.$confirmBeforeApplying
- }
- })
- }
- private func decimalToTimeFormattedString(decimal: Decimal) -> String {
- let timeInterval = TimeInterval(decimal * 60) // seconds
- let formatter = DateComponentsFormatter()
- formatter.allowedUnits = [.hour, .minute]
- formatter.unitsStyle = .brief // example: 1h 10 min
- return formatter.string(from: timeInterval) ?? ""
- }
- @MainActor func perform() async throws -> some ProvidesDialog {
- do {
- let presetToApply: TempPreset
- if let preset = preset {
- presetToApply = preset
- } else {
- presetToApply = try await $preset.requestDisambiguation(
- among: intentRequest.fetchAndProcessTempTargets(),
- dialog: "Select Temporary Target"
- )
- }
- let displayName: String = presetToApply.name
- if confirmBeforeApplying {
- try await requestConfirmation(
- result: .result(dialog: "Confirm to apply temporary target '\(displayName)'")
- )
- }
- if await intentRequest.enactTempTarget(presetToApply) {
- return .result(
- dialog: IntentDialog(
- LocalizedStringResource(
- "TempTarget '\(presetToApply.name)' applied",
- table: "ShortcutsDetail"
- )
- )
- )
- } else {
- return .result(
- dialog: IntentDialog(
- LocalizedStringResource(
- "TempTarget '\(presetToApply.name)' failed",
- table: "ShortcutsDetail"
- )
- )
- )
- }
- } catch {
- throw error
- }
- }
- }
|