AddCarbsStateModel.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import CoreData
  2. import SwiftUI
  3. extension AddCarbs {
  4. final class StateModel: BaseStateModel<Provider> {
  5. @Injected() var carbsStorage: CarbsStorage!
  6. @Injected() var apsManager: APSManager!
  7. @Injected() var settings: SettingsManager!
  8. @Published var carbs: Decimal = 0
  9. @Published var date = Date()
  10. @Published var protein: Decimal = 0
  11. @Published var fat: Decimal = 0
  12. @Published var carbsRequired: Decimal?
  13. @Published var useFPU: Bool = true
  14. @Published var dish: String = ""
  15. @Published var selection: Presets?
  16. @Published var summation: [String] = []
  17. let coredataContext = CoreDataStack.shared.persistentContainer.viewContext
  18. // @Environment(\.managedObjectContext) var moc
  19. override func subscribe() {
  20. carbsRequired = provider.suggestion?.carbsReq
  21. useFPU = settingsManager.settings.useFPUconversion
  22. }
  23. func add() {
  24. guard carbs > 0 || fat > 0 || protein > 0 else {
  25. showModal(for: nil)
  26. return
  27. }
  28. if useFPU {
  29. // -------------------------- FPU--------------------------------------
  30. let interval = settings.settings.minuteInterval // Interval betwwen carbs
  31. let timeCap = settings.settings.timeCap // Max Duration
  32. let adjustment = settings.settings.individualAdjustmentFactor
  33. let delay = settings.settings.delay // Tme before first future carb entry
  34. let kcal = protein * 4 + fat * 9
  35. let carbEquivalents = (kcal / 10) * adjustment
  36. let fpus = carbEquivalents / 10
  37. // Duration in hours used for extended boluses with Warsaw Method. Here used for total duration of the computed carbquivalents instead, excluding the configurable delay.
  38. var computedDuration = 0
  39. switch fpus {
  40. case ..<2:
  41. computedDuration = 3
  42. case 2 ..< 3:
  43. computedDuration = 4
  44. case 3 ..< 4:
  45. computedDuration = 5
  46. default:
  47. computedDuration = timeCap
  48. }
  49. // Size of each created carb equivalent if 60 minutes interval
  50. var equivalent: Decimal = carbEquivalents / Decimal(computedDuration)
  51. // Adjust for interval setting other than 60 minutes
  52. equivalent /= Decimal(60 / interval)
  53. // Round to 1 fraction digit
  54. // equivalent = Decimal(round(Double(equivalent * 10) / 10))
  55. let roundedEquivalent: Double = round(Double(equivalent * 10)) / 10
  56. equivalent = Decimal(roundedEquivalent)
  57. // Number of equivalents
  58. var numberOfEquivalents = carbEquivalents / equivalent
  59. // Only use delay in first loop
  60. var firstIndex = true
  61. // New date for each carb equivalent
  62. var useDate = date
  63. // Group and Identify all FPUs together
  64. let fpuID = UUID().uuidString
  65. // Create an array of all future carb equivalents.
  66. var futureCarbArray = [CarbsEntry]()
  67. while carbEquivalents > 0, numberOfEquivalents > 0 {
  68. if firstIndex {
  69. useDate = useDate.addingTimeInterval(delay.minutes.timeInterval)
  70. firstIndex = false
  71. } else { useDate = useDate.addingTimeInterval(interval.minutes.timeInterval) }
  72. let eachCarbEntry = CarbsEntry(
  73. id: UUID().uuidString, createdAt: useDate, carbs: equivalent, enteredBy: CarbsEntry.manual, isFPU: true,
  74. fpuID: fpuID
  75. )
  76. futureCarbArray.append(eachCarbEntry)
  77. numberOfEquivalents -= 1
  78. }
  79. // Save the array
  80. if carbEquivalents > 0 {
  81. carbsStorage.storeCarbs(futureCarbArray)
  82. }
  83. } // ------------------------- END OF TPU ----------------------------------------
  84. // Store the real carbs
  85. if carbs > 0 {
  86. carbsStorage
  87. .storeCarbs([CarbsEntry(
  88. id: UUID().uuidString,
  89. createdAt: date,
  90. carbs: carbs,
  91. enteredBy: CarbsEntry.manual,
  92. isFPU: false, fpuID: nil
  93. )])
  94. }
  95. if settingsManager.settings.skipBolusScreenAfterCarbs {
  96. apsManager.determineBasalSync()
  97. showModal(for: nil)
  98. } else {
  99. showModal(for: .bolus(waitForSuggestion: true))
  100. }
  101. }
  102. func deletePreset() {
  103. if selection != nil {
  104. try? coredataContext.delete(selection!)
  105. try? coredataContext.save()
  106. carbs = 0
  107. fat = 0
  108. protein = 0
  109. }
  110. selection = nil
  111. }
  112. func removePresetFromNewMeal() {
  113. let a = summation.firstIndex(where: { $0 == selection?.dish! })
  114. if a != nil, summation[a ?? 0] != "" {
  115. summation.remove(at: a!)
  116. }
  117. if (selection?.carbs ?? 0) as Decimal == carbs, (selection?.fat ?? 0) as Decimal == fat,
  118. (selection?.protein ?? 0) as Decimal == protein
  119. {
  120. carbs = 0
  121. fat = 0
  122. protein = 0
  123. }
  124. }
  125. func addPresetToNewMeal() {
  126. let test: String = selection?.dish ?? "dontAdd"
  127. if test != "dontAdd" {
  128. summation.append(test)
  129. }
  130. }
  131. func fullMeal() -> [String] {
  132. let filteredArray = summation.filter { !$0.isEmpty }
  133. return filteredArray
  134. }
  135. }
  136. }