CarbPresetIntentRequest.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. areFetchedFromRemote: false
  27. )
  28. var resultDisplay: String
  29. resultDisplay = "\(carbs) g carbs"
  30. if quantityFat > 0.0 {
  31. resultDisplay = "\(resultDisplay) and \(quantityFat) g fats"
  32. }
  33. if quantityProtein > 0.0 {
  34. resultDisplay = "\(resultDisplay) and \(quantityProtein) g protein"
  35. }
  36. let dateName = dateAdded.formatted()
  37. resultDisplay = "\(resultDisplay) added at \(dateName)"
  38. return resultDisplay
  39. }
  40. }