|
|
@@ -102,7 +102,8 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
}
|
|
|
|
|
|
func save(bloodGlucoses: [BloodGlucose], completion: ((Result<Bool, Error>) -> Void)? = nil) {
|
|
|
- guard settingsManager.settings.useAppleHealth else { return }
|
|
|
+ guard settingsManager.settings.useAppleHealth,
|
|
|
+ bloodGlucoses.isNotEmpty else { return }
|
|
|
|
|
|
for bgItem in bloodGlucoses {
|
|
|
let bgQuantity = HKQuantity(
|
|
|
@@ -153,14 +154,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)
|
|
|
}
|
|
|
@@ -206,6 +214,7 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
$0.metadata?["HKMetadataKeySyncIdentifier"] as? String ?? $0.uuid.uuidString
|
|
|
}
|
|
|
glucoseStorage.removeGlucose(ids: removingBGID)
|
|
|
+ newGlucose = newGlucose.filter { !removingBGID.contains($0.id) }
|
|
|
}
|
|
|
}
|
|
|
return query
|
|
|
@@ -268,11 +277,9 @@ final class BaseHealthKitManager: HealthKitManager, Injectable {
|
|
|
|
|
|
func fetch() -> AnyPublisher<[BloodGlucose], Never> {
|
|
|
guard settingsManager.settings.useAppleHealth else { return Just([]).eraseToAnyPublisher() }
|
|
|
-
|
|
|
- let copy = newGlucose
|
|
|
- newGlucose = []
|
|
|
-
|
|
|
- return Just(copy).eraseToAnyPublisher()
|
|
|
+ let actualGlucose = newGlucose.filter { $0.dateString <= Date() }
|
|
|
+ newGlucose = newGlucose.filter { !actualGlucose.contains($0) }
|
|
|
+ return Just(actualGlucose).eraseToAnyPublisher()
|
|
|
}
|
|
|
}
|
|
|
|