From 6db6e6cf612ba3d6f4d0e56db4a80f3785559513 Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Sat, 7 Oct 2017 18:28:23 -0500 Subject: [PATCH] Faster status update --- .../StatusTableViewController.swift | 87 +++++++++---------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/Loop/View Controllers/StatusTableViewController.swift b/Loop/View Controllers/StatusTableViewController.swift index 2e3e889b40..09713870d2 100644 --- a/Loop/View Controllers/StatusTableViewController.swift +++ b/Loop/View Controllers/StatusTableViewController.swift @@ -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 @@ -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 @@ -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 || @@ -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 @@ -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), @@ -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 {