Skip to content
Merged
14 changes: 7 additions & 7 deletions OmniBLE.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -746,32 +746,32 @@
8475311E26ED838A009FD801 /* Views */ = {
isa = PBXGroup;
children = (
C1C001C327A2351D00533D35 /* OmniBLEReservoirView.swift */,
C1C001C227A2351D00533D35 /* OmniBLEReservoirView.xib */,
C1F67EB227975E710017487F /* DesignElements */,
C1F67E7327975B830017487F /* AttachPodView.swift */,
C1F67E7A27975B830017487F /* BasalStateView.swift */,
C1ED1E7127BAE44E00FED71C /* BeepPreferenceSelectionView.swift */,
C1F67E8027975B830017487F /* CheckInsertedCannulaView.swift */,
C1F67E7727975B830017487F /* OmniBLESettingsView.swift */,
C1F67E7927975B830017487F /* DeactivatePodView.swift */,
C1F67E8227975B830017487F /* DeliveryUncertaintyRecoveryView.swift */,
C1F67EB227975E710017487F /* DesignElements */,
C1F67E7B27975B830017487F /* ExpirationReminderPickerView.swift */,
C1F67E7827975B830017487F /* ExpirationReminderSetupView.swift */,
C1F67E7D27975B830017487F /* HUDAssets.xcassets */,
C1F67E8527975B830017487F /* InsertCannulaView.swift */,
C187C190278FCEC9006E3557 /* InsulinTypeConfirmation.swift */,
C1F67E8627975B830017487F /* LowReservoirReminderEditView.swift */,
C1F67E7C27975B830017487F /* LowReservoirReminderSetupView.swift */,
C1DBD512282FF79D009FCF74 /* ManualTempBasalEntryView.swift */,
C1F67E8127975B830017487F /* NotificationSettingsView.swift */,
C1C001C327A2351D00533D35 /* OmniBLEReservoirView.swift */,
C1C001C227A2351D00533D35 /* OmniBLEReservoirView.xib */,
C1F67E7727975B830017487F /* OmniBLESettingsView.swift */,
C1F67E7F27975B830017487F /* PairPodView.swift */,
C1F67E8A27975B830017487F /* PodDetailsView.swift */,
C1F67E8827975B830017487F /* PodSetupView.swift */,
C1F67E7427975B830017487F /* ScheduledExpirationReminderEditView.swift */,
C1F67E8727975B830017487F /* SetupCompleteView.swift */,
C1F67E7627975B830017487F /* TimeView.swift */,
C1F67E8427975B830017487F /* UncertaintyRecoveredView.swift */,
C187C190278FCEC9006E3557 /* InsulinTypeConfirmation.swift */,
C1ED1E7127BAE44E00FED71C /* BeepPreferenceSelectionView.swift */,
C1DBD512282FF79D009FCF74 /* ManualTempBasalEntryView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down
31 changes: 27 additions & 4 deletions OmniBLE/PumpManager/OmniBLEPumpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ extension OmniBLEPumpManager {
} else if !podState.isSetupComplete {
return .activating
}
return .deactivating
return .deactivating // Can't be reached and thus will never be returned
}

public var podCommState: PodCommState {
Expand Down Expand Up @@ -591,7 +591,7 @@ extension OmniBLEPumpManager {
switch podCommState(for: state) {
case .activating:
return PumpStatusHighlight(
localizedMessage: LocalizedString("Finish Pairing", comment: "Status highlight that when pod is activating."),
localizedMessage: LocalizedString("Finish Setup", comment: "Status highlight that when pod is activating."),
imageName: "exclamationmark.circle.fill",
state: .warning)
case .deactivating:
Expand Down Expand Up @@ -702,6 +702,25 @@ extension OmniBLEPumpManager {
}
}

// Reset all the per pod state kept in pump manager state which doesn't span pods
fileprivate func resetPerPodPumpManagerState() {

// Reset any residual per pod slot based pump manager alerts
// (i.e., all but timeOffsetChangeDetected which isn't actually used)
let podAlerts = state.activeAlerts.filter { $0 != .timeOffsetChangeDetected }
for alert in podAlerts {
self.retractAlert(alert: alert)
}

self.setState { (state) in
// Reset alertsWithPendingAcknowledgment which are all pod slot based
state.alertsWithPendingAcknowledgment = []

// Reset other miscellaneous state variables that are actually per pod
state.podAttachmentConfirmed = false
state.acknowledgedTimeOffsetAlert = false
}
}

// MARK: - Pod comms

Expand Down Expand Up @@ -729,13 +748,14 @@ extension OmniBLEPumpManager {

self.podComms.forgetPod()

self.resetPerPodPumpManagerState()

if let dosesToStore = state.podState?.dosesToStore {
store(doses: dosesToStore, completion: { error in
self.setState({ (state) in
if error != nil {
state.unstoredDoses.append(contentsOf: dosesToStore)
}
state.alertsWithPendingAcknowledgment = []
})
self.prepForNewPod()
completion()
Expand All @@ -746,7 +766,6 @@ extension OmniBLEPumpManager {
}
}


// MARK: Testing

#if targetEnvironment(simulator)
Expand All @@ -770,6 +789,8 @@ extension OmniBLEPumpManager {
self.podComms.delegate = self
self.podComms.messageLogger = self

self.resetPerPodPumpManagerState()

setState({ (state) in
state.updatePodStateFromPodComms(podState)
state.scheduledExpirationReminderOffset = state.defaultExpirationReminderOffset
Expand Down Expand Up @@ -867,6 +888,8 @@ extension OmniBLEPumpManager {
self.podComms.pairAndSetupPod(timeZone: .currentFixed, insulinType: insulinType, messageLogger: self)
{ (result) in

self.resetPerPodPumpManagerState()

// Calls completion
primeSession(result)
}
Expand Down
6 changes: 3 additions & 3 deletions OmniBLE/PumpManager/PodCommsSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ public class PodCommsSession {

public func insertCannula(optionalAlerts: [PodAlert] = []) throws -> TimeInterval {
let cannulaInsertionUnits = Pod.cannulaInsertionUnits + Pod.cannulaInsertionUnitsExtra
let insertionWait: TimeInterval = .seconds(cannulaInsertionUnits / Pod.primeDeliveryRate)

guard let activatedAt = podState.activatedAt else {
throw PodCommsError.noPodPaired
Expand All @@ -429,7 +428,8 @@ public class PodCommsSession {
if status.podProgressStatus == .insertingCannula {
podState.setupProgress = .cannulaInserting
podState.updateFromStatusResponse(status, at: currentDate)
return insertionWait // Not sure when it started, wait full time to be sure
// return a non-zero wait time based on the bolus not yet delivered
return (status.bolusNotDelivered / Pod.primeDeliveryRate) + 1
}
if status.podProgressStatus.readyForDelivery {
markSetupProgressCompleted(statusResponse: status)
Expand Down Expand Up @@ -458,7 +458,7 @@ public class PodCommsSession {
podState.updateFromStatusResponse(status2, at: currentDate)

podState.setupProgress = .cannulaInserting
return insertionWait
return status2.bolusNotDelivered / Pod.primeDeliveryRate // seconds for the cannula insert bolus to finish
}

public func checkInsertionCompleted() throws {
Expand Down
4 changes: 4 additions & 0 deletions OmniBLE/PumpManager/PodState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public enum SetupProgress: Int {
public var needsCannulaInsertion: Bool {
return self.rawValue < SetupProgress.completed.rawValue
}

public var cannulaInsertionSuccessfullyStarted: Bool {
return self.rawValue > SetupProgress.startingInsertCannula.rawValue
}
}

// TODO: Mutating functions aren't guaranteed to synchronize read/write calls.
Expand Down
16 changes: 8 additions & 8 deletions OmniBLE/PumpManagerUI/ViewControllers/DashUICoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum DashUIScreen {
case expirationReminderSetup
case lowReservoirReminderSetup
case insulinTypeSelection
case pairPod
case pairAndPrime
case insertCannula
case confirmAttachment
case checkInsertedCannula
Expand All @@ -38,8 +38,8 @@ enum DashUIScreen {
case .lowReservoirReminderSetup:
return .insulinTypeSelection
case .insulinTypeSelection:
return .pairPod
case .pairPod:
return .pairAndPrime
case .pairAndPrime:
return .confirmAttachment
case .confirmAttachment:
return .insertCannula
Expand All @@ -54,7 +54,7 @@ enum DashUIScreen {
case .uncertaintyRecovered:
return nil
case .deactivate:
return .pairPod
return .pairAndPrime
case .settings:
return nil
}
Expand Down Expand Up @@ -171,7 +171,7 @@ class DashUICoordinator: UINavigationController, PumpManagerOnboarding, Completi
}
let view = OmniBLESettingsView(viewModel: viewModel, supportedInsulinTypes: allowedInsulinTypes)
return hostingController(rootView: view)
case .pairPod:
case .pairAndPrime:
pumpManagerOnboardingDelegate?.pumpManagerOnboarding(didCreatePumpManager: pumpManager)

let viewModel = PairPodViewModel(podPairer: pumpManager)
Expand All @@ -185,7 +185,7 @@ class DashUICoordinator: UINavigationController, PumpManagerOnboarding, Completi
viewModel.didRequestDeactivation = { [weak self] in
self?.navigateTo(.deactivate)
}

let view = hostingController(rootView: PairPodView(viewModel: viewModel).onAppear(perform: {UIApplication.shared.isIdleTimerDisabled = true}), onDisappear: {UIApplication.shared.isIdleTimerDisabled = false})
view.navigationItem.title = LocalizedString("Pair Pod", comment: "Title for pod pairing screen")
view.navigationItem.backButtonDisplayMode = .generic
Expand Down Expand Up @@ -351,13 +351,13 @@ class DashUICoordinator: UINavigationController, PumpManagerOnboarding, Completi
if pumpManager.podAttachmentConfirmed {
return .insertCannula
} else {
return .confirmAttachment
return .pairAndPrime // need to finish the priming
}
} else if !pumpManager.isOnboarded {
if !pumpManager.initialConfigurationCompleted {
return .firstRunScreen
}
return .pairPod
return .pairAndPrime // pair and prime a new pod
} else {
return .settings
}
Expand Down
40 changes: 14 additions & 26 deletions OmniBLE/PumpManagerUI/ViewModels/InsertCannulaViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ import LoopKitUI
public protocol CannulaInserter {
func insertCannula(completion: @escaping (Result<TimeInterval,OmniBLEPumpManagerError>) -> ())
func checkCannulaInsertionFinished(completion: @escaping (OmniBLEPumpManagerError?) -> Void)
var cannulaInsertionSuccessfullyStarted: Bool { get }
}

extension OmniBLEPumpManager: CannulaInserter { }
extension OmniBLEPumpManager: CannulaInserter {
public var cannulaInsertionSuccessfullyStarted: Bool {
return state.podState?.setupProgress.cannulaInsertionSuccessfullyStarted == true
}
}

class InsertCannulaViewModel: ObservableObject, Identifiable {

Expand All @@ -28,9 +33,9 @@ class InsertCannulaViewModel: ObservableObject, Identifiable {

var actionButtonAccessibilityLabel: String {
switch self {
case .ready, .startingInsertion:
case .ready:
return LocalizedString("Slide Button to insert Cannula", comment: "Insert cannula slider button accessibility label while ready to pair")
case .inserting:
case .inserting, .startingInsertion:
return LocalizedString("Inserting. Please wait.", comment: "Insert cannula action button accessibility label while pairing")
case .checkingInsertion:
return LocalizedString("Checking Insertion", comment: "Insert cannula action button accessibility label checking insertion")
Expand Down Expand Up @@ -141,22 +146,15 @@ class InsertCannulaViewModel: ObservableObject, Identifiable {

init(cannulaInserter: CannulaInserter) {
self.cannulaInserter = cannulaInserter

// If resuming, don't wait for the button action
if cannulaInserter.cannulaInsertionSuccessfullyStarted {
insertCannula()
}
}

// private func handleEvent(_ event: ActivationStep2Event) {
// switch event {
// case .insertingCannula:
// let finishTime = TimeInterval(Pod.estimatedCannulaInsertionDuration)
// state = .inserting(finishTime: CACurrentMediaTime() + finishTime)
// case .step2Completed:
// state = .finished
// default:
// break
// }
// }

private func checkCannulaInsertionFinished() {
state = .startingInsertion
state = .checkingInsertion
cannulaInserter.checkCannulaInsertionFinished() { (error) in
DispatchQueue.main.async {
if let error = error {
Expand All @@ -170,7 +168,6 @@ class InsertCannulaViewModel: ObservableObject, Identifiable {

private func insertCannula() {
state = .startingInsertion

cannulaInserter.insertCannula { (result) in
DispatchQueue.main.async {
switch(result) {
Expand All @@ -188,14 +185,6 @@ class InsertCannulaViewModel: ObservableObject, Identifiable {
self.state = .error(error)
}
}


// switch status {
// case .error(let error):
// self.state = .error(error)
// case .event(let event):
// self.handleEvent(event)
// }
}
}

Expand All @@ -213,7 +202,6 @@ class InsertCannulaViewModel: ObservableObject, Identifiable {
insertCannula()
}
}

}

public extension OmniBLEPumpManagerError {
Expand Down
Loading