AddCarbPresetIntent.swift 3.1 KB

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