diff --git a/OmniKit/PumpManager/OmnipodPumpManager.swift b/OmniKit/PumpManager/OmnipodPumpManager.swift index 9a06b12..8e9fd85 100644 --- a/OmniKit/PumpManager/OmnipodPumpManager.swift +++ b/OmniKit/PumpManager/OmnipodPumpManager.swift @@ -146,13 +146,25 @@ public class OmnipodPumpManager: RileyLinkPumpManager { private func setStateWithResult(_ changes: (_ state: inout OmnipodPumpManagerState) -> ReturnType) -> ReturnType { var oldValue: OmnipodPumpManagerState! var returnType: ReturnType! + var shouldNotifyStatusUpdate = false + var oldStatus: PumpManagerStatus? + let newValue = lockedState.mutate { (state) in oldValue = state + let oldStatusEvaluationDate = state.lastStatusChange + let oldHighlight = buildPumpStatusHighlight(for: oldValue, andDate: oldStatusEvaluationDate) + oldStatus = status(for: oldValue, at: oldStatusEvaluationDate) + returnType = changes(&state) - } - guard oldValue != newValue else { - return returnType + let newStatusEvaluationDate = Date() + let newStatus = status(for: state, at: newStatusEvaluationDate) + let newHighlight = buildPumpStatusHighlight(for: state, andDate: newStatusEvaluationDate) + + if oldStatus != newStatus || oldHighlight != newHighlight { + shouldNotifyStatusUpdate = true + state.lastStatusChange = newStatusEvaluationDate + } } if oldValue.podState != newValue.podState { @@ -181,25 +193,19 @@ public class OmnipodPumpManager: RileyLinkPumpManager { } } - // Ideally we ensure that oldValue.rawValue != newValue.rawValue, but the types aren't // defined as equatable pumpDelegate.notify { (delegate) in delegate?.pumpManagerDidUpdateState(self) } - let oldStatus = status(for: oldValue) - let newStatus = status(for: newValue) - - let oldHighlight = buildPumpStatusHighlight(for: oldValue) - let newHighlight = buildPumpStatusHighlight(for: newValue) - - if oldStatus != newStatus || oldHighlight != newHighlight { + if let oldStatus = oldStatus, shouldNotifyStatusUpdate { notifyStatusObservers(oldStatus: oldStatus) } return returnType } + private let lockedState: Locked private let statusObservers = WeakSynchronizedSet() @@ -439,13 +445,13 @@ extension OmnipodPumpManager { } } - private func status(for state: OmnipodPumpManagerState) -> PumpManagerStatus { + private func status(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus { return PumpManagerStatus( timeZone: state.timeZone, device: device(for: state), pumpBatteryChargeRemaining: nil, - basalDeliveryState: basalDeliveryState(for: state), - bolusState: bolusState(for: state), + basalDeliveryState: basalDeliveryState(for: state, at: date), + bolusState: bolusState(for: state, at: date), insulinType: state.insulinType, deliveryIsUncertain: state.podState?.needsCommsRecovery == true ) @@ -477,7 +483,7 @@ extension OmnipodPumpManager { } } - private func basalDeliveryState(for state: OmnipodPumpManagerState) -> PumpManagerStatus.BasalDeliveryState { + private func basalDeliveryState(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BasalDeliveryState { guard let podState = state.podState else { return .active(.distantPast) } @@ -504,7 +510,7 @@ extension OmnipodPumpManager { case .disengaging: return .cancelingTempBasal case .stable: - if let tempBasal = podState.unfinalizedTempBasal { + if let tempBasal = podState.unfinalizedTempBasal, !tempBasal.isFinished(at: date) { return .tempBasal(DoseEntry(tempBasal)) } switch podState.suspendState { @@ -516,7 +522,7 @@ extension OmnipodPumpManager { } } - private func bolusState(for state: OmnipodPumpManagerState) -> PumpManagerStatus.BolusState { + private func bolusState(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BolusState { guard let podState = state.podState else { return .noBolus } @@ -527,7 +533,7 @@ extension OmnipodPumpManager { case .disengaging: return .canceling case .stable: - if let bolus = podState.unfinalizedBolus, !bolus.isFinished() { + if let bolus = podState.unfinalizedBolus, !bolus.isFinished(at: date) { return .inProgress(DoseEntry(bolus)) } } diff --git a/OmniKit/PumpManager/OmnipodPumpManagerState.swift b/OmniKit/PumpManager/OmnipodPumpManagerState.swift index 4d45a7b..507454a 100644 --- a/OmniKit/PumpManager/OmnipodPumpManagerState.swift +++ b/OmniKit/PumpManager/OmnipodPumpManagerState.swift @@ -67,6 +67,8 @@ public struct OmnipodPumpManagerState: RawRepresentable, Equatable { internal var tempBasalEngageState: EngageablePumpState = .stable + internal var lastStatusChange: Date = .distantPast + internal var lastPumpDataReportDate: Date? internal var insulinType: InsulinType?