Skip to content
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
14 changes: 4 additions & 10 deletions OmniKit/PumpManager/OmnipodPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,9 @@ extension OmnipodPumpManager {
}

private func basalDeliveryState(for state: OmnipodPumpManagerState, at date: Date = Date()) -> PumpManagerStatus.BasalDeliveryState {
guard let podState = state.podState else {
return .active(.distantPast)
}

switch podCommState(for: state) {
case .fault:
// Treat a non-active (faulted or setup incomplete) pod just like no pod
guard let podState = state.podState, podState.isActive else {
return .active(.distantPast)
default:
break
}

switch state.suspendEngageState {
Expand Down Expand Up @@ -1860,7 +1854,7 @@ extension OmnipodPumpManager: PumpManager {

switch shouldFetchStatus {
case .none:
completion?(lastSync)
completion?(self.lastSync)
return // No active pod
case true?:
log.default("Fetching status because pumpData is too old")
Expand Down Expand Up @@ -2491,7 +2485,7 @@ extension OmnipodPumpManager: PumpManager {
}

func store(doses: [UnfinalizedDose], completion: @escaping (_ error: Error?) -> Void) {
let lastSync = lastSync
let lastSync = self.lastSync

pumpDelegate.notify { (delegate) in
guard let delegate = delegate else {
Expand Down
2 changes: 0 additions & 2 deletions OmniKit/PumpManager/PodCommsSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ public class PodCommsSession {
let status = try getStatus()
if status.podProgressStatus == .basalInitialized {
podState.setupProgress = .initialBasalScheduleSet
podState.finalizedDoses.append(UnfinalizedDose(resumeStartTime: currentDate, scheduledCertainty: .certain, insulinType: podState.insulinType))
return
}
}
Expand All @@ -402,7 +401,6 @@ public class PodCommsSession {
// Set basal schedule
let _ = try setBasalSchedule(schedule: basalSchedule, scheduleOffset: scheduleOffset)
podState.setupProgress = .initialBasalScheduleSet
podState.finalizedDoses.append(UnfinalizedDose(resumeStartTime: currentDate, scheduledCertainty: .certain, insulinType: podState.insulinType))
}

//
Expand Down
26 changes: 16 additions & 10 deletions OmniKit/PumpManager/PodState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
var finalizedDoses: [UnfinalizedDose]

public var dosesToStore: [UnfinalizedDose] {
return finalizedDoses + [unfinalizedTempBasal, unfinalizedSuspend, unfinalizedBolus].compactMap {$0}
/// Also include unfinalized bolus and temp basal doses which are mututable until finalized.
/// Suspends and resumes are now "finalized" upon getting a response confirming delivery state.
return finalizedDoses + [unfinalizedBolus, unfinalizedTempBasal].compactMap {$0}
}

public var suspendState: SuspendState
Expand Down Expand Up @@ -298,10 +300,10 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
}
}
if deliveryStatus.tempBasalRunning && unfinalizedTempBasal == nil { // active temp basal that we aren't tracking
// unfinalizedTempBasal = UnfinalizedDose(tempBasalRate: 0, startTime: Date(), duration: .minutes(30), isHighTemp: false, scheduledCertainty: .certain, insulinType: insulinType)
// unfinalizedTempBasal = UnfinalizedDose(tempBasalRate: 0, startTime: date, duration: .minutes(30), isHighTemp: false, scheduledCertainty: .certain, insulinType: insulinType)
}
if !deliveryStatus.suspended && isSuspended { // active basal that we aren't tracking
let resumeStartTime = Date()
let resumeStartTime = date
suspendState = .resumed(resumeStartTime)
unfinalizedResume = UnfinalizedDose(resumeStartTime: resumeStartTime, scheduledCertainty: .certain, insulinType: insulinType)
}
Expand All @@ -323,14 +325,18 @@ public struct PodState: RawRepresentable, Equatable, CustomDebugStringConvertibl
unfinalizedTempBasal = nil
}

if let suspend = unfinalizedSuspend {
/// Resumes and suspends have no associated delivery amounts to be finalized,
/// but we finalize these "doses" as soon as we have deliveryStatus confirmation
/// so the associated resume and suspend events can be created without delay.

if let resume = unfinalizedResume, suspend.startTime < resume.startTime {
finalizedDoses.append(suspend)
finalizedDoses.append(resume)
unfinalizedSuspend = nil
unfinalizedResume = nil
}
if let resume = unfinalizedResume, !deliveryStatus.suspended {
finalizedDoses.append(resume)
unfinalizedResume = nil
}

if let suspend = unfinalizedSuspend, deliveryStatus.suspended {
finalizedDoses.append(suspend)
unfinalizedSuspend = nil
}
}

Expand Down