@@ -163,13 +163,13 @@ final class DeviceDataManager {
163
163
var readTypes : Set < HKSampleType > = [ ]
164
164
165
165
if FeatureFlags . observeHealthKitCarbSamplesFromOtherApps {
166
- readTypes. insert ( carbStore . sampleType )
166
+ readTypes. insert ( HealthKitSampleStore . carbType )
167
167
}
168
168
if FeatureFlags . observeHealthKitDoseSamplesFromOtherApps {
169
- readTypes. insert ( doseStore . sampleType )
169
+ readTypes. insert ( HealthKitSampleStore . insulinQuantityType )
170
170
}
171
171
if FeatureFlags . observeHealthKitGlucoseSamplesFromOtherApps {
172
- readTypes. insert ( glucoseStore . sampleType )
172
+ readTypes. insert ( HealthKitSampleStore . glucoseType )
173
173
}
174
174
175
175
readTypes. insert ( HKObjectType . categoryType ( forIdentifier: HKCategoryTypeIdentifier . sleepAnalysis) !)
@@ -180,35 +180,35 @@ final class DeviceDataManager {
180
180
/// All the HealthKit types to be shared by stores
181
181
private var shareTypes : Set < HKSampleType > {
182
182
return Set ( [
183
- glucoseStore . sampleType ,
184
- carbStore . sampleType ,
185
- doseStore . sampleType ,
183
+ HealthKitSampleStore . glucoseType ,
184
+ HealthKitSampleStore . carbType ,
185
+ HealthKitSampleStore . insulinQuantityType ,
186
186
] )
187
187
}
188
188
189
189
var sleepDataAuthorizationRequired : Bool {
190
- return carbStore . healthStore. authorizationStatus ( for: HKObjectType . categoryType ( forIdentifier: HKCategoryTypeIdentifier . sleepAnalysis) !) == . notDetermined
190
+ return healthStore. authorizationStatus ( for: HKObjectType . categoryType ( forIdentifier: HKCategoryTypeIdentifier . sleepAnalysis) !) == . notDetermined
191
191
}
192
192
193
193
var sleepDataSharingDenied : Bool {
194
- return carbStore . healthStore. authorizationStatus ( for: HKObjectType . categoryType ( forIdentifier: HKCategoryTypeIdentifier . sleepAnalysis) !) == . sharingDenied
194
+ return healthStore. authorizationStatus ( for: HKObjectType . categoryType ( forIdentifier: HKCategoryTypeIdentifier . sleepAnalysis) !) == . sharingDenied
195
195
}
196
196
197
197
/// True if any stores require HealthKit authorization
198
198
var authorizationRequired : Bool {
199
- return glucoseStore . authorizationRequired ||
200
- carbStore . authorizationRequired ||
201
- doseStore . authorizationRequired ||
202
- sleepDataAuthorizationRequired
199
+ return healthStore . authorizationStatus ( for : HealthKitSampleStore . glucoseType ) == . notDetermined ||
200
+ healthStore . authorizationStatus ( for : HealthKitSampleStore . carbType ) == . notDetermined ||
201
+ healthStore . authorizationStatus ( for : HealthKitSampleStore . insulinQuantityType ) == . notDetermined ||
202
+ sleepDataAuthorizationRequired
203
203
}
204
204
205
205
/// True if the user has explicitly denied access to any stores' HealthKit types
206
- private var sharingDenied : Bool {
207
- return glucoseStore . sharingDenied ||
208
- carbStore. sharingDenied ||
209
- doseStore. sharingDenied ||
210
- sleepDataSharingDenied
211
- }
206
+ // private var sharingDenied: Bool {
207
+ // return healthStore.authorizationStatus(for: HealthKitSampleStore.glucoseType) == .sharingDenied ||
208
+ // carbStore.sharingDenied ||
209
+ // doseStore.sharingDenied ||
210
+ // sleepDataSharingDenied
211
+ // }
212
212
213
213
// MARK: Services
214
214
@@ -277,14 +277,19 @@ final class DeviceDataManager {
277
277
278
278
let absorptionTimes = LoopCoreConstants . defaultCarbAbsorptionTimes
279
279
let sensitivitySchedule = settingsManager. latestSettings. insulinSensitivitySchedule
280
-
281
- self . carbStore = CarbStore (
280
+
281
+ let carbHealthStore = HealthKitSampleStore (
282
282
healthStore: healthStore,
283
283
observeHealthKitSamplesFromOtherApps: FeatureFlags . observeHealthKitCarbSamplesFromOtherApps, // At some point we should let the user decide which apps they would like to import from.
284
+ type: HealthKitSampleStore . carbType,
285
+ observationStart: Date ( ) . addingTimeInterval ( - absorptionTimes. slow * 2 )
286
+ )
287
+
288
+ self . carbStore = CarbStore (
289
+ healthKitSampleStore: carbHealthStore,
284
290
cacheStore: cacheStore,
285
291
cacheLength: localCacheDuration,
286
292
defaultAbsorptionTimes: absorptionTimes,
287
- observationInterval: absorptionTimes. slow * 2 ,
288
293
carbRatioSchedule: settingsManager. latestSettings. carbRatioSchedule,
289
294
insulinSensitivitySchedule: sensitivitySchedule,
290
295
overrideHistory: overrideHistory,
@@ -300,10 +305,16 @@ final class DeviceDataManager {
300
305
}
301
306
302
307
self . analyticsServicesManager = analyticsServicesManager
303
-
304
- self . doseStore = DoseStore (
308
+
309
+ let insulinHealthStore = HealthKitSampleStore (
305
310
healthStore: healthStore,
306
311
observeHealthKitSamplesFromOtherApps: FeatureFlags . observeHealthKitDoseSamplesFromOtherApps,
312
+ type: HealthKitSampleStore . insulinQuantityType,
313
+ observationStart: Date ( ) . addingTimeInterval ( - absorptionTimes. slow * 2 )
314
+ )
315
+
316
+ self . doseStore = DoseStore (
317
+ healthKitSampleStore: insulinHealthStore,
307
318
cacheStore: cacheStore,
308
319
cacheLength: localCacheDuration,
309
320
insulinModelProvider: insulinModelProvider,
@@ -314,13 +325,18 @@ final class DeviceDataManager {
314
325
lastPumpEventsReconciliation: nil , // PumpManager is nil at this point. Will update this via addPumpEvents below
315
326
provenanceIdentifier: HKSource . default ( ) . bundleIdentifier
316
327
)
328
+
329
+ let glucoseHealthStore = HealthKitSampleStore (
330
+ healthStore: healthStore,
331
+ observeHealthKitSamplesFromOtherApps: FeatureFlags . observeHealthKitGlucoseSamplesFromOtherApps,
332
+ type: HealthKitSampleStore . glucoseType,
333
+ observationStart: Date ( ) . addingTimeInterval ( - . hours ( 24 ) )
334
+ )
317
335
318
336
self . glucoseStore = GlucoseStore (
319
- healthStore: healthStore,
320
- observeHealthKitSamplesFromOtherApps: FeatureFlags . observeHealthKitGlucoseSamplesFromOtherApps,
337
+ healthKitSampleStore: glucoseHealthStore,
321
338
cacheStore: cacheStore,
322
339
cacheLength: localCacheDuration,
323
- observationInterval: . hours( 24 ) ,
324
340
provenanceIdentifier: HKSource . default ( ) . bundleIdentifier
325
341
)
326
342
@@ -334,7 +350,7 @@ final class DeviceDataManager {
334
350
self . automaticDosingStatus = automaticDosingStatus
335
351
336
352
// HealthStorePreferredGlucoseUnitDidChange will be notified once the user completes the health access form. Set to .milligramsPerDeciliter until then
337
- displayGlucoseUnitObservable = DisplayGlucoseUnitObservable ( displayGlucoseUnit: glucoseStore . preferredUnit ?? . milligramsPerDeciliter)
353
+ displayGlucoseUnitObservable = DisplayGlucoseUnitObservable ( displayGlucoseUnit: . milligramsPerDeciliter)
338
354
339
355
self . trustedTimeChecker = trustedTimeChecker
340
356
@@ -438,14 +454,16 @@ final class DeviceDataManager {
438
454
. assign ( to: \. automaticDosingStatus. isAutomaticDosingAllowed, on: self )
439
455
. store ( in: & cancellables)
440
456
441
- NotificationCenter . default. addObserver ( forName: . HealthStorePreferredGlucoseUnitDidChange, object: glucoseStore . healthStore, queue: nil ) { [ weak self] _ in
442
- guard let strongSelf = self else {
457
+ NotificationCenter . default. addObserver ( forName: . HealthStorePreferredGlucoseUnitDidChange, object: healthStore, queue: nil ) { [ weak self] _ in
458
+ guard let self else {
443
459
return
444
460
}
445
461
446
- if let preferredGlucoseUnit = strongSelf. glucoseStore. preferredUnit {
447
- strongSelf. displayGlucoseUnitObservable. displayGlucoseUnitDidChange ( to: preferredGlucoseUnit)
448
- strongSelf. notifyObserversOfDisplayGlucoseUnitChange ( to: preferredGlucoseUnit)
462
+ Task {
463
+ if let unit = await self . healthStore. cachedPreferredUnits ( for: . bloodGlucose) {
464
+ self . displayGlucoseUnitObservable. displayGlucoseUnitDidChange ( to: unit)
465
+ self . notifyObserversOfDisplayGlucoseUnitChange ( to: unit)
466
+ }
449
467
}
450
468
}
451
469
}
@@ -644,9 +662,9 @@ final class DeviceDataManager {
644
662
healthStore. requestAuthorization ( toShare: shareTypes, read: readTypes) { ( success, error) in
645
663
if success {
646
664
// Call the individual authorization methods to trigger query creation
647
- self . carbStore. authorize ( toShare : true , read : FeatureFlags . observeHealthKitCarbSamplesFromOtherApps , { _ in } )
648
- self . doseStore. insulinDeliveryStore . authorize ( toShare : true , read : FeatureFlags . observeHealthKitDoseSamplesFromOtherApps , { _ in } )
649
- self . glucoseStore. authorize ( toShare : true , read : FeatureFlags . observeHealthKitGlucoseSamplesFromOtherApps , { _ in } )
665
+ self . carbStore. hkSampleStore ? . authorizationIsDetermined ( )
666
+ self . doseStore. hkSampleStore ? . authorizationIsDetermined ( )
667
+ self . glucoseStore. hkSampleStore ? . authorizationIsDetermined ( )
650
668
}
651
669
652
670
self . getHealthStoreAuthorization ( completion)
@@ -1212,7 +1230,8 @@ extension DeviceDataManager {
1212
1230
return
1213
1231
}
1214
1232
1215
- guard !self . doseStore. sharingDenied else {
1233
+ let insulinSharingDenied = self . healthStore. authorizationStatus ( for: HealthKitSampleStore . insulinQuantityType) == . sharingDenied
1234
+ guard !insulinSharingDenied else {
1216
1235
// only clear cache since access to health kit is denied
1217
1236
insulinDeliveryStore. purgeCachedInsulinDeliveryObjects ( ) { error in
1218
1237
completion ? ( error)
@@ -1232,7 +1251,8 @@ extension DeviceDataManager {
1232
1251
return
1233
1252
}
1234
1253
1235
- guard !glucoseStore. sharingDenied else {
1254
+ let glucoseSharingDenied = self . healthStore. authorizationStatus ( for: HealthKitSampleStore . glucoseType) == . sharingDenied
1255
+ guard !glucoseSharingDenied else {
1236
1256
// only clear cache since access to health kit is denied
1237
1257
glucoseStore. purgeCachedGlucoseObjects ( ) { error in
1238
1258
completion ? ( error)
@@ -1673,10 +1693,8 @@ extension DeviceDataManager {
1673
1693
func addDisplayGlucoseUnitObserver( _ observer: DisplayGlucoseUnitObserver ) {
1674
1694
let queue = DispatchQueue . main
1675
1695
displayGlucoseUnitObservers. insert ( observer, queue: queue)
1676
- if let displayGlucoseUnit = glucoseStore. preferredUnit {
1677
- queue. async {
1678
- observer. displayGlucoseUnitDidChange ( to: displayGlucoseUnit)
1679
- }
1696
+ queue. async {
1697
+ observer. displayGlucoseUnitDidChange ( to: self . displayGlucoseUnitObservable. displayGlucoseUnit)
1680
1698
}
1681
1699
}
1682
1700
0 commit comments