Skip to content

Commit d4b6420

Browse files
authored
[PAL-360] refactoring CancelTempBasalFailedError when .maximumBasalRateChanged (#614)
* refactoring CancelTempBasalFailedError when .maximumBasalRateChanged * clean up * response to PR comment
1 parent 27ea94b commit d4b6420

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Loop/Managers/DeviceDataManager.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Combine
1616

1717
protocol LoopControl {
1818
var lastLoopCompleted: Date? { get }
19-
func cancelActiveTempBasal(for reason: CancelActiveTempBasalReason) async
19+
func cancelActiveTempBasal(for reason: CancelActiveTempBasalReason) async throws
2020
func loop() async
2121
}
2222

@@ -440,7 +440,7 @@ final class DeviceDataManager {
440440
}
441441

442442
// Cancel active high temp basal
443-
await loopControl.cancelActiveTempBasal(for: .unreliableCGMData)
443+
try? await loopControl.cancelActiveTempBasal(for: .unreliableCGMData)
444444
}
445445

446446
private func processCGMReadingResult(_ manager: CGMManager, readingResult: CGMReadingResult) async {
@@ -1277,7 +1277,7 @@ extension GlucoseStore : CGMStalenessMonitorDelegate { }
12771277

12781278

12791279
//MARK: TherapySettingsViewModelDelegate
1280-
struct CancelTempBasalFailedError: LocalizedError {
1280+
struct CancelTempBasalFailedMaximumBasalRateChangedError: LocalizedError {
12811281
let reason: Error?
12821282

12831283
var errorDescription: String? {
@@ -1317,19 +1317,16 @@ extension DeviceDataManager: TherapySettingsViewModelDelegate {
13171317

13181318
func syncDeliveryLimits(deliveryLimits: DeliveryLimits) async throws -> DeliveryLimits
13191319
{
1320-
do {
1321-
// FIRST we need to check to make sure if we have to cancel temp basal first
1322-
if let maxRate = deliveryLimits.maximumBasalRate?.doubleValue(for: .internationalUnitsPerHour),
1323-
case .tempBasal(let dose) = basalDeliveryState,
1324-
dose.unitsPerHour > maxRate
1325-
{
1326-
// Temp basal is higher than proposed rate, so should cancel
1327-
await self.loopControl.cancelActiveTempBasal(for: .maximumBasalRateChanged)
1328-
}
1329-
return try await pumpManager?.syncDeliveryLimits(limits: deliveryLimits) ?? deliveryLimits
1330-
} catch {
1331-
throw CancelTempBasalFailedError(reason: error)
1320+
// FIRST we need to check to make sure if we have to cancel temp basal first
1321+
if let maxRate = deliveryLimits.maximumBasalRate?.doubleValue(for: .internationalUnitsPerHour),
1322+
case .tempBasal(let dose) = basalDeliveryState,
1323+
dose.unitsPerHour > maxRate
1324+
{
1325+
// Temp basal is higher than proposed rate, so should cancel
1326+
try await self.loopControl.cancelActiveTempBasal(for: .maximumBasalRateChanged)
13321327
}
1328+
1329+
return try await pumpManager?.syncDeliveryLimits(limits: deliveryLimits) ?? deliveryLimits
13331330
}
13341331

13351332
func saveCompletion(therapySettings: TherapySettings) {

Loop/Managers/LoopDataManager.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ final class LoopDataManager {
213213
if !$0 {
214214
self.temporaryPresetsManager.clearOverride(matching: .preMeal)
215215
Task {
216-
await self.cancelActiveTempBasal(for: .automaticDosingDisabled)
216+
try? await self.cancelActiveTempBasal(for: .automaticDosingDisabled)
217217
}
218218
} else {
219219
Task {
@@ -408,7 +408,7 @@ final class LoopDataManager {
408408
}
409409

410410
/// Cancel the active temp basal if it was automatically issued
411-
func cancelActiveTempBasal(for reason: CancelActiveTempBasalReason) async {
411+
func cancelActiveTempBasal(for reason: CancelActiveTempBasalReason) async throws {
412412
guard case .tempBasal(let dose) = deliveryDelegate?.basalDeliveryState, (dose.automatic ?? true) else { return }
413413

414414
logger.default("Cancelling active temp basal for reason: %{public}@", String(describing: reason))
@@ -423,6 +423,11 @@ final class LoopDataManager {
423423
try await deliveryDelegate?.enact(recommendation)
424424
} catch {
425425
dosingDecision.appendError(error as? LoopError ?? .unknownError(error))
426+
if reason == .maximumBasalRateChanged {
427+
throw CancelTempBasalFailedMaximumBasalRateChangedError(reason: error)
428+
} else {
429+
throw error
430+
}
426431
}
427432

428433
await dosingDecisionStore.storeDosingDecision(dosingDecision)

0 commit comments

Comments
 (0)