Skip to content

Commit

Permalink
Merge pull request #36 from loopandlearn/update_pump_manager
Browse files Browse the repository at this point in the history
update podState, add setStateWithResult to match OmniBLE, from itsmojo
  • Loading branch information
marionbarker committed Jul 18, 2024
2 parents a80e38b + 5f6c868 commit f55c080
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
42 changes: 24 additions & 18 deletions OmniKit/PumpManager/OmnipodPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,25 @@ public class OmnipodPumpManager: RileyLinkPumpManager {
private func setStateWithResult<ReturnType>(_ 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 {
Expand Down Expand Up @@ -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<OmnipodPumpManagerState>

private let statusObservers = WeakSynchronizedSet<PumpManagerStatusObserver>()
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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))
}
}
Expand Down
2 changes: 2 additions & 0 deletions OmniKit/PumpManager/OmnipodPumpManagerState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit f55c080

Please sign in to comment.