|
|
@@ -98,6 +98,9 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
}
|
|
|
|
|
|
func save(bloodGlucoses: [BloodGlucose], completion: ((Result<Bool, Error>) -> Void)? = nil) {
|
|
|
+ guard settingsManager.settings.useAppleHealth,
|
|
|
+ bloodGlucoses.isNotEmpty else { return }
|
|
|
+
|
|
|
for bgItem in bloodGlucoses {
|
|
|
let bgQuantity = HKQuantity(
|
|
|
unit: .milligramsPerDeciliter,
|
|
|
@@ -145,14 +148,21 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
}
|
|
|
|
|
|
// loading only daily bg
|
|
|
- let predicate = HKQuery.predicateForSamples(
|
|
|
+ let predicateByDate = HKQuery.predicateForSamples(
|
|
|
withStart: Date().addingTimeInterval(-1.days.timeInterval),
|
|
|
end: nil,
|
|
|
options: .strictStartDate
|
|
|
)
|
|
|
+ // loading only not FreeAPS bg
|
|
|
+ let predicateByMeta = HKQuery.predicateForObjects(
|
|
|
+ withMetadataKey: "fromFreeAPSX",
|
|
|
+ operatorType: .notEqualTo,
|
|
|
+ value: 1
|
|
|
+ )
|
|
|
+ let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateByDate, predicateByMeta])
|
|
|
|
|
|
- healthKitStore.execute(getQueryForDeletedBloodGlucose(sampleType: bgType, predicate: predicate))
|
|
|
- healthKitStore.execute(getQueryForAddedBloodGlucose(sampleType: bgType, predicate: predicate))
|
|
|
+ healthKitStore.execute(getQueryForDeletedBloodGlucose(sampleType: bgType, predicate: predicateByDate))
|
|
|
+ healthKitStore.execute(getQueryForAddedBloodGlucose(sampleType: bgType, predicate: compoundPredicate))
|
|
|
}
|
|
|
healthKitStore.execute(query)
|
|
|
}
|
|
|
@@ -200,7 +210,8 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
removingBGID.append($0.uuid.uuidString)
|
|
|
}
|
|
|
}
|
|
|
- glucoseStorage.removeGlucose(byIDCollection: removingBGID)
|
|
|
+ glucoseStorage.removeGlucose(ids: removingBGID)
|
|
|
+ newGlucose = newGlucose.filter { !removingBGID.contains($0.id) }
|
|
|
}
|
|
|
}
|
|
|
return query
|
|
|
@@ -259,6 +270,13 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
}
|
|
|
return query
|
|
|
}
|
|
|
+
|
|
|
+ func fetch() -> AnyPublisher<[BloodGlucose], Never> {
|
|
|
+ guard settingsManager.settings.useAppleHealth else { return Just([]).eraseToAnyPublisher() }
|
|
|
+ let actualGlucose = newGlucose.filter { $0.dateString <= Date() }
|
|
|
+ newGlucose = newGlucose.filter { !actualGlucose.contains($0) }
|
|
|
+ return Just(actualGlucose).eraseToAnyPublisher()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
enum HealthKitPermissionRequestStatus {
|