Skip to content

Commit 6e37bab

Browse files
author
Rick Pasetto
authored
LOOP-1328: Initialize GlucoseStore with a healthKitStorageDelay (#428)
* LOOP-1328: Initialize GlucoseStore with whatever CGMManager reports as its healthKitStorageDelay * LOOP-1328: Adds `device` to CachedGlucoseObject * unit tests and WatchHistoricalGlucose * pr feedback: remove unneeded change to `GlucoseStoreProtocol`
1 parent 4376aa7 commit 6e37bab

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

Common/Models/WatchHistoricalGlucose.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extension WatchHistoricalGlucose: RawRepresentable {
4343
let quantities: [Double]
4444
let isDisplayOnlys: [Bool]
4545
let wasUserEntereds: [Bool]
46+
let devices: [Data?]
4647

4748
init(samples: [StoredGlucoseSample]) {
4849
self.uuids = samples.map { $0.uuid }
@@ -53,27 +54,29 @@ extension WatchHistoricalGlucose: RawRepresentable {
5354
self.quantities = samples.map { $0.quantity.doubleValue(for: .milligramsPerDeciliter) }
5455
self.isDisplayOnlys = samples.map { $0.isDisplayOnly }
5556
self.wasUserEntereds = samples.map { $0.wasUserEntered }
57+
self.devices = samples.map { try? WatchHistoricalGlucose.encoder.encode($0.device) }
5658
}
5759

5860
var samples: [StoredGlucoseSample] {
5961
return (0..<uuids.count).map {
60-
StoredGlucoseSample(uuid: uuids[$0],
62+
return StoredGlucoseSample(uuid: uuids[$0],
6163
provenanceIdentifier: provenanceIdentifiers[$0],
6264
syncIdentifier: syncIdentifiers[$0],
6365
syncVersion: syncVersions[$0],
6466
startDate: startDates[$0],
6567
quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: quantities[$0]),
6668
isDisplayOnly: isDisplayOnlys[$0],
67-
wasUserEntered: wasUserEntereds[$0])
69+
wasUserEntered: wasUserEntereds[$0],
70+
device: devices[$0].flatMap { try? HKDevice(from: $0) })
6871
}
6972
}
7073
}
7174

72-
private static var encoder: PropertyListEncoder {
75+
fileprivate static var encoder: PropertyListEncoder {
7376
let encoder = PropertyListEncoder()
7477
encoder.outputFormat = .binary
7578
return encoder
7679
}
7780

78-
private static var decoder: PropertyListDecoder = PropertyListDecoder()
81+
fileprivate static var decoder: PropertyListDecoder = PropertyListDecoder()
7982
}

Loop/Managers/DeviceDataManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ private extension DeviceDataManager {
616616
cgmManager?.delegateQueue = queue
617617

618618
glucoseStore.managedDataInterval = cgmManager?.managedDataInterval
619+
glucoseStore.healthKitStorageDelay = cgmManager.map{ type(of: $0).healthKitStorageDelay } ?? 0
619620

620621
updatePumpManagerBLEHeartbeatPreference()
621622
if let cgmManager = cgmManager {

Loop/Managers/Store Protocols/GlucoseStoreProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protocol GlucoseStoreProtocol: AnyObject {
2525
var authorizationRequired: Bool { get }
2626

2727
var sharingDenied: Bool { get }
28-
28+
2929
func authorize(toShare: Bool, read: Bool, _ completion: @escaping (_ result: HealthKitSampleStoreResult<Bool>) -> Void)
3030

3131
// MARK: Sample Management

LoopTests/Managers/CGMStalenessMonitorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CGMStalenessMonitorTests: XCTestCase {
1818
private var fetchExpectation: XCTestExpectation?
1919

2020
private var storedGlucoseSample: StoredGlucoseSample {
21-
return StoredGlucoseSample(uuid: UUID(), provenanceIdentifier: UUID().uuidString, syncIdentifier: "syncIdentifier", syncVersion: 1, startDate: Date().addingTimeInterval(-.minutes(5)), quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 120), isDisplayOnly: false, wasUserEntered: false)
21+
return StoredGlucoseSample(uuid: UUID(), provenanceIdentifier: UUID().uuidString, syncIdentifier: "syncIdentifier", syncVersion: 1, startDate: Date().addingTimeInterval(-.minutes(5)), quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 120), isDisplayOnly: false, wasUserEntered: false, device: nil)
2222
}
2323

2424
private var newGlucoseSample: NewGlucoseSample {

LoopTests/Mock Stores/MockGlucoseStore.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import LoopKit
1111
@testable import Loop
1212

1313
class MockGlucoseStore: GlucoseStoreProtocol {
14+
1415
init(for test: DataManagerTestType = .flatAndStable) {
1516
self.testType = test // The store returns different effect values based on the test type
1617
}
@@ -38,6 +39,8 @@ class MockGlucoseStore: GlucoseStoreProtocol {
3839

3940
var managedDataInterval: TimeInterval?
4041

42+
var healthKitStorageDelay = TimeInterval(0)
43+
4144
var authorizationRequired: Bool = false
4245

4346
var sharingDenied: Bool = false

LoopTests/Models/WatchHistoricalGlucoseTest.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import LoopKit
1313
@testable import Loop
1414

1515
class WatchHistoricalGlucoseTests: XCTestCase {
16+
private lazy var device = HKDevice(name: "NAME", manufacturer: "MANUFACTURER", model: "MODEL", hardwareVersion: "HARDWAREVERSION", firmwareVersion: "FIRMWAREVERSION", softwareVersion: "SOFTWAREVERSION", localIdentifier: "LOCALIDENTIFIER", udiDeviceIdentifier: "UDIDEVICEIDENTIFIER")
1617
private lazy var samples: [StoredGlucoseSample] = {
1718
return [StoredGlucoseSample(uuid: UUID(),
1819
provenanceIdentifier: UUID().uuidString,
@@ -21,23 +22,26 @@ class WatchHistoricalGlucoseTests: XCTestCase {
2122
startDate: Date(timeIntervalSinceReferenceDate: .hours(100)),
2223
quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 123.45),
2324
isDisplayOnly: false,
24-
wasUserEntered: true),
25+
wasUserEntered: true,
26+
device: device),
2527
StoredGlucoseSample(uuid: UUID(),
2628
provenanceIdentifier: UUID().uuidString,
2729
syncIdentifier: UUID().uuidString,
2830
syncVersion: 2,
2931
startDate: Date(timeIntervalSinceReferenceDate: .hours(99)),
3032
quantity: HKQuantity(unit: .millimolesPerLiter, doubleValue: 7.2),
3133
isDisplayOnly: true,
32-
wasUserEntered: false),
34+
wasUserEntered: false,
35+
device: device),
3336
StoredGlucoseSample(uuid: nil,
3437
provenanceIdentifier: UUID().uuidString,
3538
syncIdentifier: nil,
3639
syncVersion: nil,
3740
startDate: Date(timeIntervalSinceReferenceDate: .hours(98)),
3841
quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 187.65),
3942
isDisplayOnly: false,
40-
wasUserEntered: false),
43+
wasUserEntered: false,
44+
device: nil),
4145
]
4246
}()
4347

0 commit comments

Comments
 (0)