Skip to content
Merged
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
87 changes: 43 additions & 44 deletions Loop/View Controllers/StatusTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ final class StatusTableViewController: ChartsTableViewController {
charts.maxEndDate = chartStartDate.addingTimeInterval(.hours(totalHours))

let reloadGroup = DispatchGroup()
var dosingEnabled: Bool?
var lastLoopCompleted: Date?
var lastLoopError: Error?
var lastReservoirValue: ReservoirValue?
var lastTempBasal: DoseEntry?
var newRecommendedTempBasal: (recommendation: TempBasalRecommendation, date: Date)?
let bolusState = self.bolusState

Expand All @@ -217,22 +213,6 @@ final class StatusTableViewController: ChartsTableViewController {
self.charts.glucoseUnit = unit
}

if currentContext.contains(.glucose) {
reloadGroup.enter()
self.deviceManager.loopManager.glucoseStore.getGlucoseValues(start: chartStartDate) { (result) -> Void in
switch result {
case .failure(let error):
self.deviceManager.logger.addError(error, fromSource: "GlucoseStore")
retryContext.update(with: .glucose)
self.charts.setGlucoseValues([])
case .success(let values):
self.charts.setGlucoseValues(values)
}

reloadGroup.leave()
}
}

// TODO: Don't always assume currentContext.contains(.status)
reloadGroup.enter()
self.deviceManager.loopManager.getLoopState { (manager, state) -> Void in
Expand All @@ -243,9 +223,33 @@ final class StatusTableViewController: ChartsTableViewController {
retryContext.update(with: .status)
}

lastTempBasal = state.lastTempBasal
lastLoopCompleted = state.lastLoopCompleted
lastLoopError = state.error
/// Update the status HUDs immediately
let netBasal: NetBasal?
let lastLoopCompleted = state.lastLoopCompleted
let lastLoopError = state.error
let dosingEnabled = manager.settings.dosingEnabled

// Net basal rate HUD
let date = state.lastTempBasal?.startDate ?? Date()
if let scheduledBasal = manager.basalRateSchedule?.between(start: date, end: date).first {
netBasal = NetBasal(
lastTempBasal: state.lastTempBasal,
maxBasal: manager.settings.maximumBasalRatePerHour,
scheduledBasal: scheduledBasal
)
} else {
netBasal = nil
}

DispatchQueue.main.async {
self.hudView?.loopCompletionHUD.lastLoopCompleted = lastLoopCompleted
self.hudView?.loopCompletionHUD.dosingEnabled = dosingEnabled
self.lastLoopError = lastLoopError

if let netBasal = netBasal {
self.hudView?.basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percent, at: netBasal.start)
}
}

// Display a recommended basal change only if we haven't completed recently, or we're in open-loop mode
if state.lastLoopCompleted == nil ||
Expand Down Expand Up @@ -277,14 +281,28 @@ final class StatusTableViewController: ChartsTableViewController {
}
}

dosingEnabled = manager.settings.dosingEnabled

reloadGroup.leave()
}

reloadGroup.leave()
}

if currentContext.contains(.glucose) {
reloadGroup.enter()
self.deviceManager.loopManager.glucoseStore.getGlucoseValues(start: chartStartDate) { (result) -> Void in
switch result {
case .failure(let error):
self.deviceManager.logger.addError(error, fromSource: "GlucoseStore")
retryContext.update(with: .glucose)
self.charts.setGlucoseValues([])
case .success(let values):
self.charts.setGlucoseValues(values)
}

reloadGroup.leave()
}
}

if currentContext.contains(.insulin) {
reloadGroup.enter()
deviceManager.loopManager.doseStore.getInsulinOnBoardValues(start: chartStartDate) { (result) -> Void in
Expand Down Expand Up @@ -344,13 +362,6 @@ final class StatusTableViewController: ChartsTableViewController {
reloadGroup.notify(queue: .main) {
self.tableView.beginUpdates()
if let hudView = self.hudView {
// Loop completion HUD
hudView.loopCompletionHUD.lastLoopCompleted = lastLoopCompleted
self.lastLoopError = lastLoopError
if let dosingEnabled = dosingEnabled {
hudView.loopCompletionHUD.dosingEnabled = dosingEnabled
}

// Glucose HUD
if let glucose = self.deviceManager.loopManager.glucoseStore.latestGlucose {
hudView.glucoseHUD.setGlucoseQuantity(glucose.quantity.doubleValue(for: self.charts.glucoseUnit),
Expand All @@ -360,18 +371,6 @@ final class StatusTableViewController: ChartsTableViewController {
)
}

// Net basal rate HUD
let date = lastTempBasal?.startDate ?? Date()
if let scheduledBasal = self.deviceManager.loopManager.basalRateSchedule?.between(start: date, end: date).first {
let netBasal = NetBasal(
lastTempBasal: lastTempBasal,
maxBasal: self.deviceManager.loopManager.settings.maximumBasalRatePerHour,
scheduledBasal: scheduledBasal
)

hudView.basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percent, at: netBasal.start)
}

// Reservoir HUD
if let reservoir = lastReservoirValue {
if let capacity = self.deviceManager.pumpState?.pumpModel?.reservoirCapacity {
Expand Down