AddCarbPresetIntent.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import AppIntents
  2. import Foundation
  3. import Intents
  4. import Swinject
  5. @available(iOS 16.0,*) struct AddCarbPresetIntent: AppIntent {
  6. // Title of the action in the Shortcuts app
  7. static var title: LocalizedStringResource = "Add carbs"
  8. // Description of the action in the Shortcuts app
  9. static var description = IntentDescription("Allow to add carbs in Trio.")
  10. init() {
  11. dateAdded = Date()
  12. }
  13. @Parameter(
  14. title: "Quantity Carbs",
  15. description: "Quantity of carbs in g",
  16. controlStyle: .field,
  17. inclusiveRange: (lowerBound: 0, upperBound: 200),
  18. requestValueDialog: IntentDialog("What is the numeric value of the carb to add")
  19. ) var carbQuantity: Double?
  20. @Parameter(
  21. title: "Quantity fat",
  22. description: "Quantity of fat in g",
  23. default: 0.0,
  24. inclusiveRange: (0, 200)
  25. ) var fatQuantity: Double
  26. @Parameter(
  27. title: "Quantity Protein",
  28. description: "Quantity of Protein in g",
  29. default: 0.0,
  30. inclusiveRange: (0, 200)
  31. ) var proteinQuantity: Double
  32. @Parameter(
  33. title: "Date",
  34. description: "Date of adding"
  35. ) var dateAdded: Date
  36. @Parameter(
  37. title: "Notes",
  38. description: "Emoji or short text"
  39. ) var note: String?
  40. @Parameter(
  41. title: "Confirm Before applying",
  42. description: "If toggled, you will need to confirm before applying",
  43. default: true
  44. ) var confirmBeforeApplying: Bool
  45. static var parameterSummary: some ParameterSummary {
  46. When(\.$confirmBeforeApplying, .equalTo, true, {
  47. Summary("Applying \(\.$carbQuantity) at \(\.$dateAdded)") {
  48. \.$fatQuantity
  49. \.$proteinQuantity
  50. \.$note
  51. \.$confirmBeforeApplying
  52. }
  53. }, otherwise: {
  54. Summary("Immediately applying \(\.$carbQuantity) at \(\.$dateAdded)") {
  55. \.$fatQuantity
  56. \.$proteinQuantity
  57. \.$note
  58. \.$confirmBeforeApplying
  59. }
  60. })
  61. }
  62. @MainActor func perform() async throws -> some ProvidesDialog {
  63. do {
  64. let quantityCarbs: Double
  65. if let cq = carbQuantity {
  66. quantityCarbs = cq
  67. } else {
  68. quantityCarbs = try await $carbQuantity.requestValue("How many carbs ?")
  69. }
  70. let quantityCarbsName = quantityCarbs.toString()
  71. if confirmBeforeApplying {
  72. try await requestConfirmation(
  73. result: .result(dialog: "Are you sure to add \(quantityCarbsName) g of carbs ?")
  74. )
  75. }
  76. let finalQuantityCarbsDisplay = try await CarbPresetIntentRequest().addCarbs(
  77. quantityCarbs,
  78. fatQuantity,
  79. proteinQuantity,
  80. dateAdded,
  81. note
  82. )
  83. return .result(
  84. dialog: IntentDialog(stringLiteral: finalQuantityCarbsDisplay)
  85. )
  86. } catch {
  87. throw error
  88. }
  89. }
  90. }