Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update podState, add setStateWithResult to match OmniBLE, from itsmojo #36

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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