Skip to content

Fixes active insulin (IOB) reporting. #2320

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ extension LoopDataManager {
}
}
updateGroup.enter()
doseStore.insulinOnBoard(at: now()) { result in
doseStore.insulinOnBoard(at: now(), basalDosingEnd: nil) { result in
switch result {
case .failure(let error):
warnings.append(.fetchDataWarning(.insulinOnBoard(error: error)))
Expand Down Expand Up @@ -2129,7 +2129,7 @@ extension LoopDataManager {

var activeInsulin: Double? = nil
let semaphore = DispatchSemaphore(value: 0)
doseStore.insulinOnBoard(at: Date()) { (result) in
doseStore.insulinOnBoard(at: Date(), basalDosingEnd: nil) { (result) in
if case .success(let iobValue) = result {
activeInsulin = iobValue.value
dosingDecision.insulinOnBoard = iobValue
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/Store Protocols/DoseStoreProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protocol DoseStoreProtocol: AnyObject {
func addDoses(_ doses: [DoseEntry], from device: HKDevice?, completion: @escaping (_ error: Error?) -> Void)

// MARK: IOB and insulin effect
func insulinOnBoard(at date: Date, completion: @escaping (_ result: DoseStoreResult<InsulinValue>) -> Void)
func insulinOnBoard(at date: Date, basalDosingEnd: Date?, completion: @escaping (_ result: DoseStoreResult<InsulinValue>) -> Void)

func getGlucoseEffects(start: Date, end: Date?, basalDosingEnd: Date?, completion: @escaping (_ result: DoseStoreResult<[GlucoseEffect]>) -> Void)

Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/WatchDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ final class WatchDataManager: NSObject {

var insulinOnBoard: InsulinValue?
updateGroup.enter()
self.deviceManager.doseStore.insulinOnBoard(at: Date()) { (result) in
self.deviceManager.doseStore.insulinOnBoard(at: Date(), basalDosingEnd: Date()) { (result) in
switch result {
case .success(let iobValue):
context.iob = iobValue.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public final class InsulinDeliveryTableViewController: UITableViewController {

private func updateIOB() {
if case .display = state {
doseStore?.insulinOnBoard(at: Date()) { (result) -> Void in
doseStore?.insulinOnBoard(at: Date(), basalDosingEnd: Date()) { (result) -> Void in
DispatchQueue.main.async {
switch result {
case .failure:
Expand Down
20 changes: 15 additions & 5 deletions Loop/View Controllers/StatusTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ final class StatusTableViewController: LoopChartsTableViewController {
var glucoseSamples: [StoredGlucoseSample]?
var predictedGlucoseValues: [GlucoseValue]?
var iobValues: [InsulinValue]?
var currentIOBValue: Double?
var doseEntries: [DoseEntry]?
var totalDelivery: Double?
var cobValues: [CarbValue]?
Expand Down Expand Up @@ -543,6 +544,17 @@ final class StatusTableViewController: LoopChartsTableViewController {
workoutMode = deviceManager.loopManager.settings.nonPreMealOverrideEnabled()
}

deviceManager.doseStore.insulinOnBoard(at: Date(), basalDosingEnd: Date()) { (result) -> Void in
switch result {
case .failure(let error):
self.log.error("DoseStore failed to get current insulin on board value: %{public}@", String(describing: error))
retryContext.update(with: .insulin)
currentIOBValue = nil
case .success(let currentIOB):
currentIOBValue = currentIOB.value
}
}

reloadGroup.notify(queue: .main) {
/// Update the chart data

Expand Down Expand Up @@ -579,11 +591,9 @@ final class StatusTableViewController: LoopChartsTableViewController {
charts.setIOBValues(iobValues)
}

// Show the larger of the value either before or after the current date
if let maxValue = charts.iob.iobPoints.allElementsAdjacent(to: Date()).max(by: {
return $0.y.scalar < $1.y.scalar
}) {
self.currentIOBDescription = String(describing: maxValue.y)
// Current IOB
if let currentIOBValue = currentIOBValue {
self.currentIOBDescription = String(format: "%.2f U", currentIOBValue)
} else {
self.currentIOBDescription = nil
}
Expand Down
2 changes: 1 addition & 1 deletion LoopTests/Mock Stores/MockDoseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MockDoseStore: DoseStoreProtocol {
completion(nil, nil, false, nil)
}

func insulinOnBoard(at date: Date, completion: @escaping (DoseStoreResult<InsulinValue>) -> Void) {
func insulinOnBoard(at date: Date, basalDosingEnd: Date? = nil, completion: @escaping (DoseStoreResult<InsulinValue>) -> Void) {
completion(.success(.init(startDate: scenario.currentDate, value: 9.5)))
}

Expand Down