AddCarbsStateModel.swift 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import SwiftUI
  2. extension AddCarbs {
  3. final class StateModel: BaseStateModel<Provider> {
  4. @Injected() var carbsStorage: CarbsStorage!
  5. @Injected() var apsManager: APSManager!
  6. @Injected() var settings: SettingsManager!
  7. @Published var carbs: Decimal = 0
  8. @Published var date = Date()
  9. @Published var protein: Decimal = 0
  10. @Published var fat: Decimal = 0
  11. @Published var carbsRequired: Decimal?
  12. @Published var useFPU: Bool = false
  13. override func subscribe() {
  14. carbsRequired = provider.suggestion?.carbsReq
  15. useFPU = settingsManager.settings.useFPUconversion
  16. }
  17. func add() {
  18. guard carbs > 0 || fat > 0 || protein > 0 else {
  19. showModal(for: nil)
  20. return
  21. }
  22. if useFPU {
  23. // ----------- FPU ------------------------------------------------
  24. let interval = settings.settings.minuteInterval // Interval betwwen carbs
  25. let timeCap = settings.settings.timeCap // Max Duration
  26. let adjustment = settings.settings.individualAdjustmentFactor
  27. let delay = settings.settings.delay // Tme before first future carb entry
  28. let kcal = protein * 4 + fat * 9
  29. let carbEquivalents = (kcal / 10) * adjustment
  30. let fpus = carbEquivalents / 10
  31. // Duration in hours used for extended boluses with Warsaw Method. Here used for total duration of the computed carbquivalents instead, excluding the configurable delay.
  32. var computedDuration = 0
  33. switch fpus {
  34. case ..<2:
  35. computedDuration = 3
  36. case 2 ... 3:
  37. computedDuration = 4
  38. case 3 ... 4:
  39. computedDuration = 5
  40. default:
  41. computedDuration = timeCap
  42. }
  43. // Size of each created carb equivalent if 60 minutes interval
  44. var carbPortions: Decimal = carbEquivalents / Decimal(computedDuration)
  45. // Adjust for interval setting other than 60 minutes
  46. carbPortions /= Decimal(60 / interval)
  47. // Number of equivalents
  48. var numberOfPortions = carbEquivalents / carbPortions
  49. // Only use delay in first loop
  50. var firstIndex = true
  51. // New date for each carb equivalent
  52. var useDate = Date()
  53. // Loop and save all carb entries
  54. while carbEquivalents > 0, numberOfPortions > 0 {
  55. if firstIndex {
  56. useDate = date.addingTimeInterval(delay.minutes.timeInterval)
  57. firstIndex = false
  58. } else { useDate = date.addingTimeInterval(interval.minutes.timeInterval) }
  59. carbsStorage.storeCarbs([
  60. CarbsEntry(
  61. id: UUID().uuidString, createdAt: useDate, carbs: carbPortions,
  62. enteredBy: CarbsEntry.manual
  63. )
  64. ])
  65. numberOfPortions -= 1
  66. date = useDate // Update date
  67. }
  68. }
  69. // ------------------------- END OF TPU -----------------------------------------------
  70. // Store the real carbs
  71. if carbs > 0 {
  72. carbsStorage
  73. .storeCarbs([CarbsEntry(id: UUID().uuidString, createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)])
  74. }
  75. if settingsManager.settings.skipBolusScreenAfterCarbs {
  76. apsManager.determineBasalSync()
  77. showModal(for: nil)
  78. } else {
  79. showModal(for: .bolus(waitForSuggestion: true))
  80. }
  81. }
  82. }
  83. }