AddCarbsStateModel.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Group and Identify all FPUs together
  54. let fpuID = UUID().uuidString
  55. // Create an array of all future carb equivalents.
  56. var futureCarbArray = [CarbsEntry]()
  57. while carbEquivalents > 0, numberOfEquivalents > 0 {
  58. if firstIndex {
  59. useDate = useDate.addingTimeInterval(delay.minutes.timeInterval)
  60. firstIndex = false
  61. } else { useDate = useDate.addingTimeInterval(interval.minutes.timeInterval) }
  62. let eachCarbEntry = CarbsEntry(
  63. id: UUID().uuidString, createdAt: useDate, carbs: equivalent, enteredBy: CarbsEntry.manual, isFPU: true,
  64. fpuID: fpuID
  65. )
  66. futureCarbArray.append(eachCarbEntry)
  67. numberOfEquivalents -= 1
  68. }
  69. // Save the array
  70. if carbEquivalents > 0 {
  71. carbsStorage.storeCarbs(futureCarbArray)
  72. }
  73. } // ------------------------- END OF TPU ----------------------------------------
  74. // Store the real carbs
  75. if carbs > 0 {
  76. carbsStorage
  77. .storeCarbs([CarbsEntry(
  78. id: UUID().uuidString,
  79. createdAt: date,
  80. carbs: carbs,
  81. enteredBy: CarbsEntry.manual,
  82. isFPU: false, fpuID: nil
  83. )])
  84. }
  85. if settingsManager.settings.skipBolusScreenAfterCarbs {
  86. apsManager.determineBasalSync()
  87. showModal(for: nil)
  88. } else {
  89. showModal(for: .bolus(waitForSuggestion: true))
  90. }
  91. }
  92. }
  93. }