|
|
@@ -62,6 +62,8 @@ extension Bolus {
|
|
|
@Published var useFattyMealCorrectionFactor: Bool = false
|
|
|
@Published var eventualBG: Int = 0
|
|
|
|
|
|
+ @Published var currentBasal: Decimal = 0
|
|
|
+
|
|
|
@Published var meal: [CarbsEntry]?
|
|
|
@Published var carbs: Decimal = 0
|
|
|
@Published var fat: Decimal = 0
|
|
|
@@ -95,6 +97,42 @@ extension Bolus {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func getCurrentBasal() {
|
|
|
+ let basalEntries = provider.getProfile()
|
|
|
+
|
|
|
+ let dateFormatter = DateFormatter()
|
|
|
+ dateFormatter.dateFormat = "HH:mm:ss"
|
|
|
+ let currentTime = dateFormatter.string(from: Date())
|
|
|
+
|
|
|
+ // loop throug entries and get current basal entry
|
|
|
+ for (index, entry) in basalEntries.enumerated() {
|
|
|
+ if let entryStartTimeDate = dateFormatter.date(from: entry.start) {
|
|
|
+ var entryEndTimeDate: Date
|
|
|
+
|
|
|
+ if index < basalEntries.count - 1 {
|
|
|
+ let nextEntry = basalEntries[index + 1]
|
|
|
+ if let nextEntryStartTimeDate = dateFormatter.date(from: nextEntry.start) {
|
|
|
+ let timeDifference = nextEntryStartTimeDate.timeIntervalSince(entryStartTimeDate)
|
|
|
+ entryEndTimeDate = entryStartTimeDate.addingTimeInterval(timeDifference)
|
|
|
+ } else {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ entryEndTimeDate = Date()
|
|
|
+ }
|
|
|
+ // if currenTime is between start and end of basal entry -> basal = currentBasal
|
|
|
+ if let currentTimeDate = dateFormatter.date(from: currentTime) {
|
|
|
+ if currentTimeDate >= entryStartTimeDate, currentTimeDate <= entryEndTimeDate {
|
|
|
+ if let basal = entry.rate as? Decimal {
|
|
|
+ currentBasal = basal
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
func getDeltaBG() {
|
|
|
let glucose = provider.fetchGlucose()
|
|
|
guard glucose.count >= 3 else { return }
|
|
|
@@ -214,6 +252,7 @@ extension Bolus {
|
|
|
.roundBolus(amount: max(self.insulinRecommended, 0))
|
|
|
|
|
|
if self.useCalc {
|
|
|
+ self.getCurrentBasal()
|
|
|
self.getDeltaBG()
|
|
|
self.insulinCalculated = self.calculateInsulin()
|
|
|
}
|