Skip to content

Commit f9a4f62

Browse files
committed
Backfill up to 6 values (30 minutes) of glucose from Share
Fixes #21
1 parent 3f01d8c commit f9a4f62

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

Cartfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github "loudnate/LoopKit" ~> 0.3.3
1+
github "loudnate/LoopKit" "dev"
22
github "loudnate/xDripG5" ~> 0.5.0
33
github "loudnate/SwiftCharts" "loudnate/naterade"
44
github "mddub/dexcom-share-client-swift" ~> 0.1

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
github "amplitude/Amplitude-iOS" "a3d6720626e72a678489bce83ed9dacadb505dd7"
22
github "loudnate/Crypto" "13fee45175b88629aeabe60b4b4fc3daf86fa0a3"
3-
github "loudnate/LoopKit" "v0.3.3"
3+
github "loudnate/LoopKit" "16e5c9a82a360c163c14d2d958aa662cb3ab9fc4"
44
github "loudnate/SwiftCharts" "8671287afb29640f9cffced6521b1098b7aac085"
55
github "mddub/dexcom-share-client-swift" "v0.1.3"
66
github "loudnate/xDripG5" "0.5.0"

Loop/Managers/DeviceDataManager.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,23 +483,21 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
483483
return
484484
}
485485

486-
shareClient.fetchLast(1) { (error, glucose) in
487-
if let error = error {
488-
self.logger.addError(error, fromSource: "ShareClient")
489-
}
486+
shareClient.fetchLast(6) { (error, glucose) in
487+
guard let glucose = glucose else {
488+
if let error = error {
489+
self.logger.addError(error, fromSource: "ShareClient")
490+
}
490491

491-
guard let glucose = glucose?.first else {
492-
completion?()
493492
return
494493
}
495494

496-
// Ignore glucose values that are less than a minute newer than our previous value
497-
if let latestGlucose = glucoseStore.latestGlucose where latestGlucose.startDate.timeIntervalSinceDate(glucose.startDate) > -NSTimeInterval(minutes: 1) {
498-
completion?()
499-
return
495+
// Ignore glucose values that are up to a minute newer than our previous value, to account for possible time shifting in Share data
496+
let newGlucose = glucose.filterDateRange(glucoseStore.latestGlucose?.startDate.dateByAddingTimeInterval(NSTimeInterval(minutes: 1)), nil).map {
497+
return (quantity: $0.quantity, date: $0.startDate, displayOnly: false)
500498
}
501499

502-
glucoseStore.addGlucose(glucose.quantity, date: glucose.startDate, displayOnly: false, device: nil) { (_, value, error) -> Void in
500+
glucoseStore.addGlucoseValues(newGlucose, device: nil) { (_, _, error) -> Void in
503501
if let error = error {
504502
self.logger.addError(error, fromSource: "GlucoseStore")
505503
}

0 commit comments

Comments
 (0)