ApplyTempPresetIntent.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import AppIntents
  2. import Foundation
  3. @available(iOS 16.0, *) struct ApplyTempPresetIntent: AppIntent {
  4. // Title of the action in the Shortcuts app
  5. static var title: LocalizedStringResource = "Apply a temporary Preset"
  6. // Description of the action in the Shortcuts app
  7. static var description = IntentDescription("Allow to apply a specific temporary preset.")
  8. internal var intentRequest: TempPresetsIntentRequest
  9. init() {
  10. intentRequest = TempPresetsIntentRequest()
  11. }
  12. @Parameter(title: "Preset") var preset: tempPreset?
  13. @Parameter(
  14. title: "Confirm Before applying",
  15. description: "If toggled, you will need to confirm before applying",
  16. default: true
  17. ) var confirmBeforeApplying: Bool
  18. static var parameterSummary: some ParameterSummary {
  19. When(\ApplyTempPresetIntent.$confirmBeforeApplying, .equalTo, true, {
  20. Summary("Applying \(\.$preset)") {
  21. \.$confirmBeforeApplying
  22. }
  23. }, otherwise: {
  24. Summary("Immediately applying \(\.$preset)") {
  25. \.$confirmBeforeApplying
  26. }
  27. })
  28. }
  29. @MainActor func perform() async throws -> some ProvidesDialog {
  30. do {
  31. let presetToApply: tempPreset
  32. if let preset = preset {
  33. presetToApply = preset
  34. } else {
  35. presetToApply = try await $preset.requestDisambiguation(
  36. among: intentRequest.fetchAll(),
  37. dialog: "What temp preset would you like ?"
  38. )
  39. }
  40. let displayName: String = presetToApply.name
  41. if confirmBeforeApplying {
  42. try await requestConfirmation(
  43. result: .result(dialog: "Are you sure to applying the temp target \(displayName) ?")
  44. )
  45. }
  46. // TODO: enact the temp target
  47. let tempTarget = try intentRequest.findTempTarget(presetToApply)
  48. let finalTempTargetApply = try intentRequest.enactTempTarget(tempTarget)
  49. let displayDetail: String =
  50. "the target \(finalTempTargetApply.displayName) is applying during \(finalTempTargetApply.duration) mn"
  51. return .result(
  52. dialog: IntentDialog(stringLiteral: displayDetail)
  53. )
  54. } catch {
  55. throw error
  56. }
  57. }
  58. }