CarbPresetIntentRequest.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import CoreData
  2. import Foundation
  3. @available(iOS 16.0,*) final class CarbPresetIntentRequest: BaseIntentsRequest {
  4. func addCarbs(
  5. _ quantityCarbs: Double,
  6. _ quantityFat: Double,
  7. _ quantityProtein: Double,
  8. _ dateAdded: Date,
  9. _ note: String?
  10. ) async throws -> String {
  11. guard quantityCarbs >= 0.0 || quantityFat >= 0.0 || quantityProtein >= 0.0 else {
  12. return "not adding carbs in Trio"
  13. }
  14. let carbs = min(Decimal(quantityCarbs), settingsManager.settings.maxCarbs)
  15. await carbsStorage.storeCarbs(
  16. [CarbsEntry(
  17. id: UUID().uuidString,
  18. createdAt: dateAdded,
  19. actualDate: dateAdded,
  20. carbs: carbs,
  21. fat: Decimal(quantityFat),
  22. protein: Decimal(quantityProtein),
  23. note: (note?.isEmpty ?? true) ? "Via Shortcut" : note!,
  24. enteredBy: CarbsEntry.manual,
  25. isFPU: false, fpuID: nil
  26. )],
  27. areFetchedFromRemote: false
  28. )
  29. var resultDisplay: String
  30. resultDisplay = "\(carbs) g carbs"
  31. if quantityFat > 0.0 {
  32. resultDisplay = "\(resultDisplay) and \(quantityFat) g fats"
  33. }
  34. if quantityProtein > 0.0 {
  35. resultDisplay = "\(resultDisplay) and \(quantityProtein) g protein"
  36. }
  37. let dateName = dateAdded.formatted()
  38. resultDisplay = "\(resultDisplay) added at \(dateName)"
  39. return resultDisplay
  40. }
  41. }