|
@@ -23,37 +23,59 @@ extension AddCarbs {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- let interval = settings.settings.minuteInterval
|
|
|
|
|
- let timeCap = settings.settings.timeCap * (60 / Decimal(interval))
|
|
|
|
|
- let adjustment = settings.settings.individualAdjustmentFactor
|
|
|
|
|
- let delay = settings.settings.delay
|
|
|
|
|
|
|
+ if useFPU {
|
|
|
|
|
+ // ----------- FPU ------------------------------------------------
|
|
|
|
|
+ let interval = settings.settings.minuteInterval // Interval betwwen carbs
|
|
|
|
|
+ let timeCap = settings.settings.timeCap // Max Duration
|
|
|
|
|
+ let adjustment = settings.settings.individualAdjustmentFactor
|
|
|
|
|
+ let delay = settings.settings.delay // Tme before first future carb entry
|
|
|
|
|
|
|
|
- // Convert fat and protein to carb equivalents and store as future carbs
|
|
|
|
|
- let fpucarb = 0.4 * protein + 0.9 * fat
|
|
|
|
|
- let fpus = (fat * 9.0 + protein * 4.0) / 100.0
|
|
|
|
|
- var counter: Decimal = (fpus * 2) - 1.0
|
|
|
|
|
- counter = max(timeCap, counter)
|
|
|
|
|
- var roundedCounter: Decimal = 0
|
|
|
|
|
- NSDecimalRound(&roundedCounter, &counter, 0, .up)
|
|
|
|
|
- let carbequiv = (fpucarb / roundedCounter) * adjustment
|
|
|
|
|
- let firstDate = date.addingTimeInterval(delay.minutes.timeInterval)
|
|
|
|
|
- var previousDate = date
|
|
|
|
|
|
|
+ let kcal = protein * 4 + fat * 9
|
|
|
|
|
+ let carbEquivalents = (kcal / 10) * adjustment
|
|
|
|
|
+ let fpus = carbEquivalents / 10
|
|
|
|
|
|
|
|
- while counter > 0, carbequiv > 0 {
|
|
|
|
|
- var useDate = date + 1 * Double(interval * 60)
|
|
|
|
|
- // Fix Interval and Delay
|
|
|
|
|
- useDate = max(previousDate.addingTimeInterval(interval.minutes.timeInterval), useDate, firstDate)
|
|
|
|
|
- if useDate > previousDate {
|
|
|
|
|
|
|
+ // Duration in hours used for extended boluses with Warsaw Method. Here used for total duration of the computed carbquivalents instead, excluding the configurable delay.
|
|
|
|
|
+ var computedDuration = 0
|
|
|
|
|
+ switch fpus {
|
|
|
|
|
+ case ..<2:
|
|
|
|
|
+ computedDuration = 3
|
|
|
|
|
+ case 2 ... 3:
|
|
|
|
|
+ computedDuration = 4
|
|
|
|
|
+ case 3 ... 4:
|
|
|
|
|
+ computedDuration = 5
|
|
|
|
|
+ default:
|
|
|
|
|
+ computedDuration = timeCap
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Size of each created carb equivalent if 60 minutes interval
|
|
|
|
|
+ var carbPortions: Decimal = carbEquivalents / Decimal(computedDuration)
|
|
|
|
|
+ // Adjust for interval setting other than 60 minutes
|
|
|
|
|
+ carbPortions /= Decimal(60 / interval)
|
|
|
|
|
+ // Number of equivalents
|
|
|
|
|
+ var numberOfPortions = carbEquivalents / carbPortions
|
|
|
|
|
+ // Only use delay in first loop
|
|
|
|
|
+ var firstIndex = true
|
|
|
|
|
+ // New date for each carb equivalent
|
|
|
|
|
+ var useDate = Date()
|
|
|
|
|
+
|
|
|
|
|
+ // Loop and save all carb entries
|
|
|
|
|
+ while carbEquivalents > 0, numberOfPortions > 0 {
|
|
|
|
|
+ if firstIndex {
|
|
|
|
|
+ useDate = date.addingTimeInterval(delay.minutes.timeInterval)
|
|
|
|
|
+ firstIndex = false
|
|
|
|
|
+ } else { useDate = date.addingTimeInterval(interval.minutes.timeInterval) }
|
|
|
carbsStorage.storeCarbs([
|
|
carbsStorage.storeCarbs([
|
|
|
CarbsEntry(
|
|
CarbsEntry(
|
|
|
- id: UUID().uuidString, createdAt: useDate, carbs: carbequiv,
|
|
|
|
|
|
|
+ id: UUID().uuidString, createdAt: useDate, carbs: carbPortions,
|
|
|
enteredBy: CarbsEntry.manual
|
|
enteredBy: CarbsEntry.manual
|
|
|
)
|
|
)
|
|
|
])
|
|
])
|
|
|
|
|
+ numberOfPortions -= 1
|
|
|
|
|
+ date = useDate // Update date
|
|
|
}
|
|
}
|
|
|
- previousDate = useDate
|
|
|
|
|
- counter -= 1
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ // ------------------------- END OF TPU -----------------------------------------------
|
|
|
|
|
+
|
|
|
// Store the real carbs
|
|
// Store the real carbs
|
|
|
if carbs > 0 {
|
|
if carbs > 0 {
|
|
|
carbsStorage
|
|
carbsStorage
|