BolusIntentRequest.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Combine
  2. import CoreData
  3. import Foundation
  4. @available(iOS 16.0,*) final class BolusIntentRequest: BaseIntentsRequest {
  5. private var suggestion: Determination? {
  6. fileStorage.retrieve(OpenAPS.Enact.suggested, as: Determination.self)
  7. }
  8. func bolus(_ bolusAmount: Double) async throws -> LocalizedStringResource {
  9. var bolusQuantity: Decimal = 0
  10. switch settingsManager.settings.bolusShortcut {
  11. //Block boluses if they are disabled
  12. case .notAllowed:
  13. return LocalizedStringResource(
  14. "Bolusing is not allowed with shortcuts.",
  15. table: "ShortcutsDetail"
  16. )
  17. //Block any bolus attempted if it is larger than the max bolus in settings
  18. case .limitBolusMax:
  19. if Decimal(bolusAmount) > settingsManager.pumpSettings.maxBolus {
  20. return LocalizedStringResource(
  21. "The bolus cannot be larger than the pump setting max bolus.",
  22. table: "ShortcutsDetail"
  23. )
  24. } else {
  25. bolusQuantity = apsManager.roundBolus(amount: Decimal(bolusAmount))
  26. }
  27. //Block any bolus attempted if it is larger than the max bolus in settings
  28. case .limitInsulinSuggestion:
  29. let insulinSuggestion = suggestion?.insulinForManualBolus ?? 0
  30. if Decimal(bolusAmount) > insulinSuggestion {
  31. return LocalizedStringResource(
  32. "The bolus cannot be larger than the suggested insulin.",
  33. table: "ShortcutsDetail"
  34. )
  35. } else {
  36. bolusQuantity = apsManager
  37. .roundBolus(amount: Decimal(bolusAmount))
  38. }
  39. }
  40. await apsManager.enactBolus(amount: Double(bolusQuantity), isSMB: false)
  41. return LocalizedStringResource(
  42. "A bolus command of \(bolusQuantity.formatted()) U of insulin was sent",
  43. table: "ShortcutsDetail"
  44. )
  45. }
  46. private var glucoseFormatter: NumberFormatter {
  47. let formatter = NumberFormatter()
  48. formatter.numberStyle = .decimal
  49. formatter.maximumFractionDigits = 0
  50. if settingsManager.settings.units == .mmolL {
  51. formatter.minimumFractionDigits = 1
  52. formatter.maximumFractionDigits = 1
  53. }
  54. formatter.roundingMode = .halfUp
  55. return formatter
  56. }
  57. }