Skip to content
Merged
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: 4 additions & 0 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@
A9F703752489C9A000C98AD8 /* GlucoseStore+SimulatedCoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9F703742489C9A000C98AD8 /* GlucoseStore+SimulatedCoreData.swift */; };
A9F703772489D8AA00C98AD8 /* PersistentDeviceLog+SimulatedCoreData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9F703762489D8AA00C98AD8 /* PersistentDeviceLog+SimulatedCoreData.swift */; };
A9FB75F1252BE320004C7D3F /* BolusDosingDecision.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9FB75F0252BE320004C7D3F /* BolusDosingDecision.swift */; };
2437A340C1560EEDE55E3F7B /* CarbEntryChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8378B7D1F532A5F47CEBB6AE /* CarbEntryChange.swift */; };
B4001CEE28CBBC82002FB414 /* AlertManagementView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4001CED28CBBC82002FB414 /* AlertManagementView.swift */; };
A1E47000000000000000A001 /* AlertHistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1E47000000000000000A002 /* AlertHistoryView.swift */; };
B405E35A24D2B1A400DD058D /* HUDAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4F2C15961E09E94E00E160D4 /* HUDAssets.xcassets */; };
Expand Down Expand Up @@ -1199,6 +1200,7 @@
A9F703742489C9A000C98AD8 /* GlucoseStore+SimulatedCoreData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GlucoseStore+SimulatedCoreData.swift"; sourceTree = "<group>"; };
A9F703762489D8AA00C98AD8 /* PersistentDeviceLog+SimulatedCoreData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PersistentDeviceLog+SimulatedCoreData.swift"; sourceTree = "<group>"; };
A9FB75F0252BE320004C7D3F /* BolusDosingDecision.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BolusDosingDecision.swift; sourceTree = "<group>"; };
8378B7D1F532A5F47CEBB6AE /* CarbEntryChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarbEntryChange.swift; sourceTree = "<group>"; };
AF16A5EDEB9DF756890E0C09 /* Loop/Resources/Sounds/bloop.caf */ = {isa = PBXFileReference; lastKnownFileType = file; path = Loop/Resources/Sounds/bloop.caf; sourceTree = SOURCE_ROOT; };
B4001CED28CBBC82002FB414 /* AlertManagementView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertManagementView.swift; sourceTree = "<group>"; };
A1E47000000000000000A002 /* AlertHistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertHistoryView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1708,6 +1710,7 @@
DDC389F52A2B61750066E2E8 /* ApplicationFactorStrategy.swift */,
FEEDF0000000000000000000 /* GlucoseStatistics.swift */,
A9FB75F0252BE320004C7D3F /* BolusDosingDecision.swift */,
8378B7D1F532A5F47CEBB6AE /* CarbEntryChange.swift */,
DDC389F92A2B62470066E2E8 /* ConstantApplicationFactorStrategy.swift */,
C1EF747128D6A44A00C8C083 /* CrashRecoveryManager.swift */,
1455ACB12C667BEE004F44F2 /* Deeplink.swift */,
Expand Down Expand Up @@ -3636,6 +3639,7 @@
1455ACB22C667BEE004F44F2 /* Deeplink.swift in Sources */,
43DFB62320D4CAE7008A7BAE /* PumpManager.swift in Sources */,
A9FB75F1252BE320004C7D3F /* BolusDosingDecision.swift in Sources */,
2437A340C1560EEDE55E3F7B /* CarbEntryChange.swift in Sources */,
892A5D59222F0A27008961AB /* Debug.swift in Sources */,
431A8C401EC6E8AB00823B9C /* CircleMaskView.swift in Sources */,
1D05219D2469F1F5000EBBDE /* AlertStore.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Loop/Extensions/CarbStore+SimulatedCoreData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extension CarbStore {
fileprivate extension NewCarbEntry {
static func simulated(startDate: Date, grams: Double, absorptionTime: TimeInterval) -> NewCarbEntry {
return NewCarbEntry(date: startDate,
syncIdentifier: UUID().uuidString,
quantity: LoopQuantity(unit: .gram, doubleValue: grams),
startDate: startDate,
foodType: "Simulated",
Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/LoopAppManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ extension LoopAppManager: UNUserNotificationCenterDelegate {
let mealTime = userInfo[LoopNotificationUserInfoKey.missedMealTime.rawValue] as? Date,
let carbAmount = userInfo[LoopNotificationUserInfoKey.missedMealCarbAmount.rawValue] as? Double
{
let missedEntry = NewCarbEntry(quantity: LoopQuantity(unit: .gram,
let missedEntry = NewCarbEntry(syncIdentifier: UUID().uuidString, quantity: LoopQuantity(unit: .gram,
doubleValue: carbAmount),
startDate: mealTime,
foodType: nil,
Expand Down
51 changes: 30 additions & 21 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -808,14 +808,13 @@ final class LoopDataManager: ObservableObject {

func recommendManualBolus(
manualGlucoseSample: NewGlucoseSample? = nil,
potentialCarbEntry: NewCarbEntry? = nil,
originalCarbEntry: StoredCarbEntry? = nil,
carbEntryChange: CarbEntryChange? = nil,
truncatingActiveOverride: Bool = false
) async throws -> ManualBolusRecommendation? {

var endingPremealOverride = false

if potentialCarbEntry != nil,
if carbEntryChange != nil,
let activeOverride = temporaryPresetsManager.activeOverride,
activeOverride.context == .preMeal
{
Expand All @@ -824,8 +823,8 @@ final class LoopDataManager: ObservableObject {

var input = try await self.fetchData(for: now, presumePresetEndingNow: truncatingActiveOverride || endingPremealOverride)
.addingGlucoseSample(sample: manualGlucoseSample?.asStoredGlucoseSample)
.removingCarbEntry(carbEntry: originalCarbEntry)
.addingCarbEntry(carbEntry: potentialCarbEntry?.asStoredCarbEntry)
.removingCarbEntry(carbEntry: carbEntryChange?.originalEntry)
.addingCarbEntry(carbEntry: carbEntryChange?.asStoredCarbEntry)

input.includePositiveVelocityAndRC = usePositiveMomentumAndRCForManualBoluses
input.recommendationType = .manualBolus
Expand Down Expand Up @@ -990,16 +989,15 @@ extension LoopDataManager {
/// Adds and stores carb data, and recommends a bolus if needed
///
/// - Parameters:
/// - carbEntry: The new carb value
/// - completion: A closure called once upon completion
/// - result: The bolus recommendation
func addCarbEntry(_ carbEntry: NewCarbEntry, replacing replacingEntry: StoredCarbEntry? = nil) async throws -> StoredCarbEntry {
let storedCarbEntry: StoredCarbEntry
if let replacingEntry = replacingEntry {
storedCarbEntry = try await carbStore.replaceCarbEntry(replacingEntry, withEntry: carbEntry)
} else {
storedCarbEntry = try await carbStore.addCarbEntry(carbEntry)
}
/// - carbEntry: The new carb value, stored once however often it is retried
func addCarbEntry(_ carbEntry: NewCarbEntry) async throws -> StoredCarbEntry {
let storedCarbEntry = try await carbStore.addCarbEntry(carbEntry)
self.temporaryPresetsManager.endPreMealOverride()
return storedCarbEntry
}

func replaceCarbEntry(_ oldEntry: StoredCarbEntry, with update: CarbEntryUpdate) async throws -> StoredCarbEntry {
let storedCarbEntry = try await carbStore.replaceCarbEntry(oldEntry, withEntry: update)
self.temporaryPresetsManager.endPreMealOverride()
return storedCarbEntry
}
Expand Down Expand Up @@ -1203,6 +1201,18 @@ extension NewCarbEntry {
}
}

extension CarbEntryUpdate {
var asStoredCarbEntry: StoredCarbEntry {
StoredCarbEntry(
startDate: startDate,
quantity: quantity,
foodType: foodType,
absorptionTime: absorptionTime,
userCreatedDate: date
)
}
}

extension NewGlucoseSample {
var asStoredGlucoseSample: StoredGlucoseSample {
StoredGlucoseSample(
Expand Down Expand Up @@ -1386,7 +1396,7 @@ extension LoopDataManager: ServicesManagerDelegate {
}

let quantity = LoopQuantity(unit: .gram, doubleValue: amountInGrams)
let candidateCarbEntry = NewCarbEntry(quantity: quantity, startDate: startDate ?? now, foodType: foodType, absorptionTime: absorptionTime)
let candidateCarbEntry = NewCarbEntry(syncIdentifier: UUID().uuidString, quantity: quantity, startDate: startDate ?? now, foodType: foodType, absorptionTime: absorptionTime)

let _ = try await carbStore.addCarbEntry(candidateCarbEntry)
}
Expand Down Expand Up @@ -1473,15 +1483,14 @@ extension LoopDataManager: BolusEntryViewModelDelegate {
}

func generatePrediction(
originalCarbEntry: StoredCarbEntry?,
potentialCarbEntry: NewCarbEntry?,
carbEntryChange: CarbEntryChange?,
potentialDose: SimpleInsulinDose?,
manualGlucose: NewGlucoseSample?
) async throws -> (historicGlucose: [StoredGlucoseSample], predictedGlucose: [PredictedGlucoseValue]) {

var endingPremealOverride = false

if potentialCarbEntry != nil,
if carbEntryChange != nil,
let activeOverride = temporaryPresetsManager.activeOverride,
activeOverride.context == .preMeal
{
Expand All @@ -1494,8 +1503,8 @@ extension LoopDataManager: BolusEntryViewModelDelegate {
input = input
.addingDose(dose: potentialDose)
.addingGlucoseSample(sample: manualGlucose?.asStoredGlucoseSample)
.removingCarbEntry(carbEntry: originalCarbEntry)
.addingCarbEntry(carbEntry: potentialCarbEntry?.asStoredCarbEntry)
.removingCarbEntry(carbEntry: carbEntryChange?.originalEntry)
.addingCarbEntry(carbEntry: carbEntryChange?.asStoredCarbEntry)

let prediction = try input.predictGlucose()

Expand Down
2 changes: 1 addition & 1 deletion Loop/Managers/Store Protocols/CarbStoreProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol CarbStoreProtocol: AnyObject {

func getCarbEntries(start: Date?, end: Date?, dateAscending: Bool, fetchLimit: Int?, with favoriteFoodID: String?) async throws -> [StoredCarbEntry]

func replaceCarbEntry(_ oldEntry: StoredCarbEntry, withEntry newEntry: NewCarbEntry) async throws -> StoredCarbEntry
func replaceCarbEntry(_ oldEntry: StoredCarbEntry, withEntry newEntry: CarbEntryUpdate) async throws -> StoredCarbEntry

func addCarbEntry(_ entry: NewCarbEntry) async throws -> StoredCarbEntry

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

if let recommendedBolus = try? await loopDataManager.recommendManualBolus(
manualGlucoseSample: nil,
potentialCarbEntry: potentialCarbEntry,
originalCarbEntry: nil
carbEntryChange: potentialCarbEntry.map { .add($0) }
) {
context.recommendedBolusDose = recommendedBolus.amount
dosingDecision.manualBolusRecommendation = ManualBolusRecommendationWithDate(
Expand Down
58 changes: 58 additions & 0 deletions Loop/Models/CarbEntryChange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// CarbEntryChange.swift
// Loop
//
// Copyright © 2026 LoopKit Authors. All rights reserved.
//

import Foundation
import LoopAlgorithm
import LoopKit

/// A carb entry about to be saved: a new entry, or new content for a stored one.
enum CarbEntryChange: Equatable {
case add(NewCarbEntry)
case edit(StoredCarbEntry, CarbEntryUpdate)

var originalEntry: StoredCarbEntry? {
if case .edit(let original, _) = self {
return original
}
return nil
}

var quantity: LoopQuantity {
switch self {
case .add(let entry): entry.quantity
case .edit(_, let update): update.quantity
}
}

var startDate: Date {
switch self {
case .add(let entry): entry.startDate
case .edit(_, let update): update.startDate
}
}

var foodType: String? {
switch self {
case .add(let entry): entry.foodType
case .edit(_, let update): update.foodType
}
}

var absorptionTime: TimeInterval? {
switch self {
case .add(let entry): entry.absorptionTime
case .edit(_, let update): update.absorptionTime
}
}

var asStoredCarbEntry: StoredCarbEntry {
switch self {
case .add(let entry): entry.asStoredCarbEntry
case .edit(_, let update): update.asStoredCarbEntry
}
}
}
Loading