CarbPresetIntentRequest.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. ) async throws -> String {
  10. guard quantityCarbs >= 0.0 || quantityFat >= 0.0 || quantityProtein >= 0.0 else {
  11. return "no adding carbs in iAPS"
  12. }
  13. let carbs = min(Decimal(quantityCarbs), settingsManager.settings.maxCarbs)
  14. await carbsStorage.storeCarbs(
  15. [CarbsEntry(
  16. id: UUID().uuidString,
  17. createdAt: dateAdded,
  18. actualDate: dateAdded,
  19. carbs: carbs,
  20. fat: Decimal(quantityFat),
  21. protein: Decimal(quantityProtein),
  22. note: "add with shortcuts",
  23. enteredBy: CarbsEntry.manual,
  24. isFPU: false, fpuID: nil
  25. )]
  26. )
  27. var resultDisplay: String
  28. resultDisplay = "\(carbs) g carbs"
  29. if quantityFat > 0.0 {
  30. resultDisplay = "\(resultDisplay) and \(quantityFat) g fats"
  31. }
  32. if quantityProtein > 0.0 {
  33. resultDisplay = "\(resultDisplay) and \(quantityProtein) g protein"
  34. }
  35. let dateName = dateAdded.formatted()
  36. resultDisplay = "\(resultDisplay) added at \(dateName)"
  37. return resultDisplay
  38. }
  39. }