|
|
@@ -456,9 +456,7 @@ extension BaseTidepoolManager {
|
|
|
else {
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- // Calculate the exact insulin delivered based on the duration in seconds
|
|
|
- let preciseDurationSeconds = TimeInterval(duration * 60) // Convert duration from minutes to seconds
|
|
|
+ let value = (Decimal(duration) / 60.0) * amount
|
|
|
|
|
|
// Find the corresponding temp basal entry in existingTempBasalEntries
|
|
|
if let matchingEntryIndex = existingTempBasalEntries.firstIndex(where: { $0.timestamp == event.timestamp }) {
|
|
|
@@ -466,36 +464,28 @@ extension BaseTidepoolManager {
|
|
|
let predecessorIndex = matchingEntryIndex - 1
|
|
|
if predecessorIndex >= 0 {
|
|
|
let predecessorEntry = existingTempBasalEntries[predecessorIndex]
|
|
|
-
|
|
|
if let predecessorTimestamp = predecessorEntry.timestamp,
|
|
|
let predecessorEntrySyncIdentifier = predecessorEntry.id
|
|
|
{
|
|
|
- let predecessorDurationSeconds = TimeInterval(predecessorEntry.tempBasal?.duration ?? 0) * 60
|
|
|
- let predecessorEndDate = predecessorTimestamp.addingTimeInterval(predecessorDurationSeconds)
|
|
|
+ let predecessorEndDate = predecessorTimestamp
|
|
|
+ .addingTimeInterval(TimeInterval(
|
|
|
+ Int(predecessorEntry.tempBasal?.duration ?? 0) *
|
|
|
+ 60
|
|
|
+ )) // parse duration to minutes
|
|
|
|
|
|
// If the predecessor's end date is later than the current event's start date, adjust it
|
|
|
if predecessorEndDate > event.timestamp {
|
|
|
let adjustedEndDate = event.timestamp
|
|
|
let adjustedDuration = adjustedEndDate.timeIntervalSince(predecessorTimestamp)
|
|
|
-
|
|
|
- // Use precise duration in hours and round the basal rate
|
|
|
- let adjustedDurationHours = adjustedDuration / 3600
|
|
|
- let originalRate = Double(truncating: predecessorEntry.tempBasal?.rate ?? 0)
|
|
|
-
|
|
|
- // Round the rate to a supported basal rate using pumpManager's rounding function
|
|
|
- let roundedRate = self.apsManager.pumpManager?
|
|
|
- .roundToSupportedBasalRate(unitsPerHour: originalRate) ?? originalRate
|
|
|
-
|
|
|
- // Recalculate the delivered units using the rounded rate
|
|
|
- let adjustedDeliveredUnits = adjustedDurationHours * roundedRate
|
|
|
+ let adjustedDeliveredUnits = (adjustedDuration / 3600) *
|
|
|
+ Double(truncating: predecessorEntry.tempBasal?.rate ?? 0)
|
|
|
|
|
|
// Create updated predecessor dose entry
|
|
|
let updatedPredecessorEntry = DoseEntry(
|
|
|
type: .tempBasal,
|
|
|
startDate: predecessorTimestamp,
|
|
|
endDate: adjustedEndDate,
|
|
|
- value: self.apsManager.pumpManager?
|
|
|
- .roundToSupportedBolusVolume(units: adjustedDeliveredUnits) ?? adjustedDeliveredUnits,
|
|
|
+ value: adjustedDeliveredUnits,
|
|
|
unit: .units,
|
|
|
deliveredUnits: adjustedDeliveredUnits,
|
|
|
syncIdentifier: predecessorEntrySyncIdentifier,
|
|
|
@@ -504,7 +494,6 @@ extension BaseTidepoolManager {
|
|
|
manuallyEntered: false,
|
|
|
isMutable: false
|
|
|
)
|
|
|
-
|
|
|
// Add the updated predecessor entry to the result
|
|
|
insulinDoseEvents.append(updatedPredecessorEntry)
|
|
|
}
|
|
|
@@ -512,25 +501,14 @@ extension BaseTidepoolManager {
|
|
|
}
|
|
|
|
|
|
// Create a new dose entry for the current event
|
|
|
- let currentEndDate = event.timestamp.addingTimeInterval(preciseDurationSeconds)
|
|
|
-
|
|
|
- // Round the basal rate for the current event as well
|
|
|
- let roundedRate = self.apsManager.pumpManager?
|
|
|
- .roundToSupportedBasalRate(unitsPerHour: Double(amount)) ?? Double(amount)
|
|
|
-
|
|
|
- let deliveredAmount = self.apsManager.pumpManager?
|
|
|
- .roundToSupportedBolusVolume(units: Double(roundedRate) * (preciseDurationSeconds / 3600)) ??
|
|
|
- Double(roundedRate) * (preciseDurationSeconds / 3600)
|
|
|
-
|
|
|
+ let currentEndDate = event.timestamp.addingTimeInterval(TimeInterval(minutes: Double(duration)))
|
|
|
let newDoseEntry = DoseEntry(
|
|
|
type: .tempBasal,
|
|
|
startDate: event.timestamp,
|
|
|
endDate: currentEndDate,
|
|
|
- value: self.apsManager.pumpManager?
|
|
|
- .roundToSupportedBolusVolume(units: deliveredAmount) ?? deliveredAmount,
|
|
|
+ value: Double(value),
|
|
|
unit: .units,
|
|
|
- deliveredUnits: self.apsManager.pumpManager?
|
|
|
- .roundToSupportedBolusVolume(units: deliveredAmount) ?? deliveredAmount,
|
|
|
+ deliveredUnits: Double(value),
|
|
|
syncIdentifier: event.id,
|
|
|
scheduledBasalRate: HKQuantity(
|
|
|
unit: .internationalUnitsPerHour,
|
|
|
@@ -541,7 +519,6 @@ extension BaseTidepoolManager {
|
|
|
manuallyEntered: false,
|
|
|
isMutable: false
|
|
|
)
|
|
|
-
|
|
|
// Add the new event entry to the result
|
|
|
insulinDoseEvents.append(newDoseEntry)
|
|
|
}
|