Skip to content

Commit

Permalink
[PAL-458] Adding the check for rapidly rising glucose (#622)
Browse files Browse the repository at this point in the history
* Adding the check for rapidly rising glucose

* matching the previous implementation

* clean up
  • Loading branch information
nhamming authored Mar 21, 2024
1 parent d5140bf commit 72fca58
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,10 @@ extension LoopDataManager: CarbEntryViewModelDelegate {
var defaultAbsorptionTimes: DefaultAbsorptionTimes {
LoopCoreConstants.defaultCarbAbsorptionTimes
}


func getGlucoseSamples(start: Date?, end: Date?) async throws -> [StoredGlucoseSample] {
try await glucoseStore.getGlucoseSamples(start: start, end: end)
}
}

extension LoopDataManager: ManualDoseViewModelDelegate {
Expand Down
47 changes: 46 additions & 1 deletion Loop/View Models/CarbEntryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import LoopCore
protocol CarbEntryViewModelDelegate: AnyObject, BolusEntryViewModelDelegate {
var defaultAbsorptionTimes: DefaultAbsorptionTimes { get }
func scheduleOverrideEnabled(at date: Date) -> Bool
func getGlucoseSamples(start: Date?, end: Date?) async throws -> [StoredGlucoseSample]
}

final class CarbEntryViewModel: ObservableObject {
Expand All @@ -38,11 +39,14 @@ final class CarbEntryViewModel: ObservableObject {
return 1
case .overrideInProgress:
return 2
case .glucoseRisingRapidly:
return 3
}
}

case entryIsMissedMeal
case overrideInProgress
case glucoseRisingRapidly
}

@Published var alert: CarbEntryViewModel.Alert?
Expand Down Expand Up @@ -284,12 +288,14 @@ final class CarbEntryViewModel: ObservableObject {
}

private func observeLoopUpdates() {
self.checkIfOverrideEnabled()
checkIfOverrideEnabled()
checkGlucoseRisingRapidly()
NotificationCenter.default
.publisher(for: .LoopDataUpdated)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
self?.checkIfOverrideEnabled()
self?.checkGlucoseRisingRapidly()
}
.store(in: &cancellables)
}
Expand All @@ -309,6 +315,45 @@ final class CarbEntryViewModel: ObservableObject {
}
}

private func checkGlucoseRisingRapidly() {
guard let delegate else {
warnings.remove(.glucoseRisingRapidly)
return
}

let now = Date()
let startDate = now.addingTimeInterval(-LoopConstants.missedMealWarningGlucoseRecencyWindow)

Task { @MainActor in
let glucoseSamples = try? await delegate.getGlucoseSamples(start: startDate, end: nil)
guard let glucoseSamples else {
warnings.remove(.glucoseRisingRapidly)
return
}

let filteredGlucoseSamples = glucoseSamples.filterDateRange(startDate, now)
guard let startSample = filteredGlucoseSamples.first, let endSample = filteredGlucoseSamples.last else {
warnings.remove(.glucoseRisingRapidly)
return
}

let duration = endSample.startDate.timeIntervalSince(startSample.startDate)
guard duration >= LoopConstants.missedMealWarningVelocitySampleMinDuration else {
warnings.remove(.glucoseRisingRapidly)
return
}

let delta = endSample.quantity.doubleValue(for: .milligramsPerDeciliter) - startSample.quantity.doubleValue(for: .milligramsPerDeciliter)
let velocity = delta / duration.minutes // Unit = mg/dL/m

if velocity > LoopConstants.missedMealWarningGlucoseRiseThreshold {
warnings.insert(.glucoseRisingRapidly)
} else {
warnings.remove(.glucoseRisingRapidly)
}
}
}

private func observeAbsorptionTimeChange() {
$absorptionTime
.receive(on: RunLoop.main)
Expand Down
4 changes: 4 additions & 0 deletions Loop/Views/CarbEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ extension CarbEntryView {
return .critical
case .overrideInProgress:
return .warning
case .glucoseRisingRapidly:
return .critical
}
}

Expand All @@ -176,6 +178,8 @@ extension CarbEntryView {
return NSLocalizedString("Loop has detected an missed meal and estimated its size. Edit the carb amount to match the amount of any carbs you may have eaten.", comment: "Warning displayed when user is adding a meal from an missed meal notification")
case .overrideInProgress:
return NSLocalizedString("An active override is modifying your carb ratio and insulin sensitivity. If you don't want this to affect your bolus calculation and projected glucose, consider turning off the override.", comment: "Warning to ensure the carb entry is accurate during an override")
case .glucoseRisingRapidly:
return NSLocalizedString("Your glucose is rapidly rising. Check that any carbs you've eaten were logged. If you logged carbs, check that the time you entered lines up with when you started eating.", comment: "Warning to ensure the carb entry is accurate")
}
}

Expand Down

0 comments on commit 72fca58

Please sign in to comment.