BolusIntentRequest.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Combine
  2. import CoreData
  3. import Foundation
  4. final class BolusIntentRequest: BaseIntentsRequest {
  5. func bolus(_ bolusAmount: Double) async throws -> String {
  6. var bolusQuantity: Decimal = 0
  7. switch settingsManager.settings.bolusShortcut {
  8. // Block boluses if they are disabled
  9. case .notAllowed:
  10. return String(
  11. localized:
  12. "Bolusing via Shortcuts is disabled in Trio settings."
  13. )
  14. // Block any bolus attempted if it is larger than the max bolus in settings
  15. case .limitBolusMax:
  16. if Decimal(bolusAmount) > settingsManager.pumpSettings.maxBolus {
  17. return String(
  18. localized:
  19. "The bolus cannot be larger than the pump setting max bolus (\(settingsManager.pumpSettings.maxBolus.description))."
  20. )
  21. } else {
  22. bolusQuantity = apsManager.roundBolus(amount: Decimal(bolusAmount))
  23. }
  24. await apsManager.enactBolus(amount: Double(bolusQuantity), isSMB: false, callback: nil)
  25. return String(
  26. localized:
  27. "A bolus command of \(bolusQuantity.formatted()) U of insulin was sent."
  28. )
  29. }
  30. }
  31. func bolusExternal(_ bolusAmount: Double) async throws -> String {
  32. var bolusQuantity: Decimal = 0
  33. var maxExternal: Decimal { settingsManager.pumpSettings.maxBolus * 3 }
  34. if Decimal(bolusAmount) > maxExternal {
  35. return String(
  36. localized:
  37. "The external bolus cannot be larger than 3 x the pump setting max bolus (\(settingsManager.pumpSettings.maxBolus.description))."
  38. )
  39. } else {
  40. bolusQuantity = apsManager.roundBolus(amount: Decimal(bolusAmount))
  41. await pumpHistoryStorage.storeExternalInsulinEvent(amount: bolusQuantity, timestamp: Date())
  42. // perform determine basal sync
  43. try await apsManager.determineBasalSync()
  44. return String(
  45. localized:
  46. "An external bolus of \(bolusQuantity.formatted()) U of insulin was recorded."
  47. )
  48. }
  49. }
  50. }