AddCarbsStateModel.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 equivalent: Decimal = carbEquivalents / Decimal(computedDuration)
  45. // Adjust for interval setting other than 60 minutes
  46. equivalent /= Decimal(60 / interval)
  47. // Number of equivalents
  48. var numberOfEquivalents = carbEquivalents / equivalent
  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, numberOfEquivalents > 0 {
  55. if firstIndex {
  56. useDate = useDate.addingTimeInterval(delay.minutes.timeInterval)
  57. firstIndex = false
  58. } else { useDate = useDate.addingTimeInterval(interval.minutes.timeInterval) }
  59. carbsStorage.storeCarbs([
  60. CarbsEntry(
  61. id: UUID().uuidString, createdAt: useDate, carbs: equivalent,
  62. enteredBy: CarbsEntry.manual
  63. )
  64. ])
  65. numberOfEquivalents -= 1
  66. }
  67. }
  68. // ------------------------- END OF TPU -----------------------------------------------
  69. // Store the real carbs
  70. if carbs > 0 {
  71. carbsStorage
  72. .storeCarbs([CarbsEntry(id: UUID().uuidString, createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)])
  73. }
  74. if settingsManager.settings.skipBolusScreenAfterCarbs {
  75. apsManager.determineBasalSync()
  76. showModal(for: nil)
  77. } else {
  78. showModal(for: .bolus(waitForSuggestion: true))
  79. }
  80. }
  81. }
  82. }