Skip to content

Commit 47b53ca

Browse files
author
Darin Krauss
authored
LOOP-1361 Capture manually entered BG reading (#110)
- https://tidepool.atlassian.net/browse/LOOP-1361 - Add wasUserEntered property to various glucose data structures
1 parent 6f8933b commit 47b53ca

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

Common/Models/WatchContext.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class WatchContext: RawRepresentable {
2121
var glucose: HKQuantity?
2222
var glucoseTrendRawValue: Int?
2323
var glucoseDate: Date?
24+
var glucoseIsDisplayOnly: Bool?
25+
var glucoseWasUserEntered: Bool?
2426
var glucoseSyncIdentifier: String?
2527

2628
var predictedGlucose: WatchPredictedGlucose?
@@ -61,6 +63,8 @@ final class WatchContext: RawRepresentable {
6163

6264
glucoseTrendRawValue = rawValue["gt"] as? Int
6365
glucoseDate = rawValue["gd"] as? Date
66+
glucoseIsDisplayOnly = rawValue["gdo"] as? Bool
67+
glucoseWasUserEntered = rawValue["gue"] as? Bool
6468
glucoseSyncIdentifier = rawValue["gs"] as? String
6569
iob = rawValue["iob"] as? Double
6670
reservoir = rawValue["r"] as? Double
@@ -103,6 +107,8 @@ final class WatchContext: RawRepresentable {
103107

104108
raw["gt"] = glucoseTrendRawValue
105109
raw["gd"] = glucoseDate
110+
raw["gdo"] = glucoseIsDisplayOnly
111+
raw["gue"] = glucoseWasUserEntered
106112
raw["gs"] = glucoseSyncIdentifier
107113
raw["iob"] = iob
108114
raw["ld"] = loopLastRunDate
@@ -132,7 +138,7 @@ extension WatchContext {
132138
extension WatchContext {
133139
var newGlucoseSample: NewGlucoseSample? {
134140
if let quantity = glucose, let date = glucoseDate, let syncIdentifier = glucoseSyncIdentifier {
135-
return NewGlucoseSample(date: date, quantity: quantity, isDisplayOnly: false, syncIdentifier: syncIdentifier, syncVersion: 0)
141+
return NewGlucoseSample(date: date, quantity: quantity, isDisplayOnly: glucoseIsDisplayOnly ?? false, wasUserEntered: glucoseWasUserEntered ?? false, syncIdentifier: syncIdentifier, syncVersion: 0)
136142
}
137143
return nil
138144
}

Common/Models/WatchHistoricalGlucose.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct WatchHistoricalGlucose {
1616

1717
init(with samples: [StoredGlucoseSample]) {
1818
self.samples = samples.map {
19-
NewGlucoseSample(date: $0.startDate, quantity: $0.quantity, isDisplayOnly: false, syncIdentifier: $0.syncIdentifier, syncVersion: 0)
19+
NewGlucoseSample(date: $0.startDate, quantity: $0.quantity, isDisplayOnly: $0.isDisplayOnly, wasUserEntered: $0.wasUserEntered, syncIdentifier: $0.syncIdentifier, syncVersion: 0)
2020
}
2121
}
2222
}
@@ -29,7 +29,9 @@ extension WatchHistoricalGlucose: RawRepresentable {
2929
return [
3030
"d": samples.map { $0.date },
3131
"v": samples.map { Int16($0.quantity.doubleValue(for: .milligramsPerDeciliter)) },
32-
"id": samples.map { $0.syncIdentifier }
32+
"id": samples.map { $0.syncIdentifier },
33+
"do": samples.map { $0.isDisplayOnly },
34+
"ue": samples.map { $0.wasUserEntered }
3335
]
3436
}
3537

@@ -38,14 +40,18 @@ extension WatchHistoricalGlucose: RawRepresentable {
3840
let dates = rawValue["d"] as? [Date],
3941
let values = rawValue["v"] as? [Int16],
4042
let syncIdentifiers = rawValue["id"] as? [String],
43+
let isDisplayOnly = rawValue["do"] as? [Bool],
44+
let wasUserEntered = rawValue["ue"] as? [Bool],
4145
dates.count == values.count,
42-
dates.count == syncIdentifiers.count
46+
dates.count == syncIdentifiers.count,
47+
dates.count == isDisplayOnly.count,
48+
dates.count == wasUserEntered.count
4349
else {
4450
return nil
4551
}
4652

4753
self.samples = (0..<dates.count).map {
48-
NewGlucoseSample(date: dates[$0], quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: Double(values[$0])), isDisplayOnly: false, syncIdentifier: syncIdentifiers[$0], syncVersion: 0)
54+
NewGlucoseSample(date: dates[$0], quantity: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: Double(values[$0])), isDisplayOnly: isDisplayOnly[$0], wasUserEntered: wasUserEntered[$0], syncIdentifier: syncIdentifiers[$0], syncVersion: 0)
4955
}
5056
}
5157
}

Loop/Extensions/GlucoseStore+SimulatedCoreData.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ fileprivate extension StoredGlucoseSample {
6969
startDate: startDate,
7070
quantity: HKQuantity(unit: unit, doubleValue: value),
7171
isDisplayOnly: false,
72+
wasUserEntered: false,
7273
provenanceIdentifier: Bundle.main.bundleIdentifier!)
7374
}
7475
}

Loop/Managers/WatchDataManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ final class WatchDataManager: NSObject {
170170
if let sample = samples.last {
171171
context.glucose = sample.quantity
172172
context.glucoseDate = sample.startDate
173+
context.glucoseIsDisplayOnly = sample.isDisplayOnly
174+
context.glucoseWasUserEntered = sample.wasUserEntered
173175
context.glucoseSyncIdentifier = sample.syncIdentifier
174176
}
175177
updateGroup.leave()

Loop/Models/WatchContext+LoopKit.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import HealthKit
1111
import LoopKit
1212

1313
extension WatchContext {
14-
convenience init(glucose: GlucoseValue?, glucoseUnit: HKUnit?) {
14+
convenience init(glucose: GlucoseSampleValue?, glucoseUnit: HKUnit?) {
1515
self.init()
1616

1717
self.glucose = glucose?.quantity
1818
self.glucoseDate = glucose?.startDate
19+
self.glucoseIsDisplayOnly = glucose?.isDisplayOnly
20+
self.glucoseWasUserEntered = glucose?.wasUserEntered
1921
self.preferredGlucoseUnit = glucoseUnit
2022
}
2123
}

0 commit comments

Comments
 (0)