ApplyOverridePresetIntent.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import AppIntents
  2. import Foundation
  3. struct ApplyOverridePresetIntent: AppIntent {
  4. // Title of the action in the Shortcuts app
  5. static var title = LocalizedStringResource("Activate an override", table: "ShortcutsDetail")
  6. // Description of the action in the Shortcuts app
  7. static var description = IntentDescription(.init("Activate an override", table: "ShortcutsDetail"))
  8. @Parameter(
  9. title: LocalizedStringResource("Override", table: "ShortcutsDetail"),
  10. description: LocalizedStringResource("Override choice", table: "ShortcutsDetail")
  11. ) var preset: OverridePreset?
  12. @Parameter(
  13. title: LocalizedStringResource("Confirm Before applying", table: "ShortcutsDetail"),
  14. description: LocalizedStringResource("If toggled, you will need to confirm before applying", table: "ShortcutsDetail"),
  15. default: true
  16. ) var confirmBeforeApplying: Bool
  17. static var parameterSummary: some ParameterSummary {
  18. When(\ApplyOverridePresetIntent.$confirmBeforeApplying, .equalTo, true, {
  19. Summary("Applying \(\.$preset) override", table: "ShortcutsDetail") {
  20. \.$confirmBeforeApplying
  21. }
  22. }, otherwise: {
  23. Summary("Immediately applying \(\.$preset) override", table: "ShortcutsDetail") {
  24. \.$confirmBeforeApplying
  25. }
  26. })
  27. }
  28. @MainActor func perform() async throws -> some ProvidesDialog {
  29. do {
  30. let presetToApply: OverridePreset
  31. if let preset = preset {
  32. presetToApply = preset
  33. } else {
  34. presetToApply = try await $preset.requestDisambiguation(
  35. among: await OverridePresetsIntentRequest().fetchAndProcessOverrides(),
  36. dialog: IntentDialog(LocalizedStringResource("Select override", table: "ShortcutsDetail"))
  37. )
  38. }
  39. let displayName: String = presetToApply.name
  40. if confirmBeforeApplying {
  41. try await requestConfirmation(
  42. result: .result(
  43. dialog: IntentDialog(LocalizedStringResource(
  44. "Confirm to apply override '\(displayName)'",
  45. table: "ShortcutsDetail"
  46. ))
  47. )
  48. )
  49. }
  50. if await OverridePresetsIntentRequest().enactOverride(presetToApply) {
  51. return .result(
  52. dialog: IntentDialog(
  53. LocalizedStringResource(
  54. "Override '\(presetToApply.name)' applied",
  55. table: "ShortcutsDetail"
  56. )
  57. )
  58. )
  59. } else {
  60. return .result(
  61. dialog: IntentDialog(
  62. LocalizedStringResource(
  63. "Override '\(presetToApply.name)' failed",
  64. table: "ShortcutsDetail"
  65. )
  66. )
  67. )
  68. }
  69. } catch {
  70. throw error
  71. }
  72. }
  73. }