Skip to content

Commit 69cc770

Browse files
authored
Merge pull request #1252 from cph-cachet/health-13
Health 13.1.2
2 parents 9c8890c + 74e5076 commit 69cc770

File tree

7 files changed

+236
-316
lines changed

7 files changed

+236
-316
lines changed

packages/health/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 13.1.2
2+
3+
* Fix [#1250](https://github.com/cph-cachet/flutter-plugins/issues/1250)
4+
* Fix [#1233](https://github.com/cph-cachet/flutter-plugins/issues/1233)
5+
16
## 13.1.1
27

38
* Fix [#1207](https://github.com/cph-cachet/flutter-plugins/issues/1207) - (**Important**: Some property names might have changed compared to before for `Nutrition`)

packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt

Lines changed: 108 additions & 85 deletions
Large diffs are not rendered by default.

packages/health/example/lib/main.dart

Lines changed: 64 additions & 194 deletions
Large diffs are not rendered by default.

packages/health/example/lib/util.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const List<HealthDataType> dataTypesIOS = [
5252
HealthDataType.LEAN_BODY_MASS,
5353

5454
// note that a phone cannot write these ECG-based types - only read them
55-
// HealthDataType.ELECTROCARDIOGRAM,
55+
HealthDataType.ELECTROCARDIOGRAM,
5656
// HealthDataType.HIGH_HEART_RATE_EVENT,
5757
// HealthDataType.IRREGULAR_HEART_RATE_EVENT,
5858
// HealthDataType.LOW_HEART_RATE_EVENT,

packages/health/ios/Classes/HealthDataReader.swift

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,14 @@ class HealthDataReader {
303303
}
304304
} else {
305305
if #available(iOS 14.0, *), let ecgSamples = samples as? [HKElectrocardiogram] {
306-
let dictionaries = ecgSamples.map(self.fetchEcgMeasurements)
307-
DispatchQueue.main.async {
308-
result(dictionaries)
309-
}
306+
self.fetchEcgMeasurements(ecgSamples, result)
310307
} else {
311308
DispatchQueue.main.async {
312309
print("Error getting ECG - only available on iOS 14.0 and above!")
313310
result(nil)
314311
}
315312
}
313+
316314
}
317315
}
318316

@@ -546,38 +544,62 @@ class HealthDataReader {
546544
/// - Parameter sample: ECG sample
547545
/// - Returns: Dictionary with ECG data
548546
@available(iOS 14.0, *)
549-
private func fetchEcgMeasurements(_ sample: HKElectrocardiogram) -> NSDictionary {
550-
let semaphore = DispatchSemaphore(value: 0)
551-
var voltageValues = [NSDictionary]()
552-
let voltageQuery = HKElectrocardiogramQuery(sample) { query, result in
553-
switch result {
554-
case let .measurement(measurement):
555-
if let voltageQuantity = measurement.quantity(for: .appleWatchSimilarToLeadI) {
556-
let voltage = voltageQuantity.doubleValue(for: HKUnit.volt())
557-
let timeSinceSampleStart = measurement.timeSinceSampleStart
558-
voltageValues.append(["voltage": voltage, "timeSinceSampleStart": timeSinceSampleStart])
547+
private func fetchEcgMeasurements(_ ecgSample: [HKElectrocardiogram], _ result: @escaping FlutterResult) {
548+
let group = DispatchGroup()
549+
var dictionaries = [NSDictionary]()
550+
let lock = NSLock()
551+
552+
for ecg in ecgSample {
553+
group.enter()
554+
555+
var voltageValues = [[String: Any]]()
556+
let expected = Int(ecg.numberOfVoltageMeasurements)
557+
if expected > 0 {
558+
voltageValues.reserveCapacity(expected)
559+
}
560+
561+
let q = HKElectrocardiogramQuery(ecg) { _, res in
562+
switch res {
563+
case .measurement(let m):
564+
if let v = m.quantity(for: .appleWatchSimilarToLeadI)?
565+
.doubleValue(for: HKUnit.volt()) {
566+
voltageValues.append([
567+
"voltage": v,
568+
"timeSinceSampleStart": m.timeSinceSampleStart
569+
])
570+
}
571+
case .done:
572+
let dict: NSDictionary = [
573+
"uuid": "\(ecg.uuid)",
574+
"voltageValues": voltageValues,
575+
"averageHeartRate": ecg.averageHeartRate?
576+
.doubleValue(for: HKUnit.count()
577+
.unitDivided(by: HKUnit.minute())),
578+
"samplingFrequency": ecg.samplingFrequency?
579+
.doubleValue(for: HKUnit.hertz()),
580+
"classification": ecg.classification.rawValue,
581+
"date_from": Int(ecg.startDate.timeIntervalSince1970 * 1000),
582+
"date_to": Int(ecg.endDate.timeIntervalSince1970 * 1000),
583+
"source_id": ecg.sourceRevision.source.bundleIdentifier,
584+
"source_name": ecg.sourceRevision.source.name
585+
]
586+
lock.lock()
587+
dictionaries.append(dict)
588+
lock.unlock()
589+
group.leave()
590+
case .error(let e):
591+
print("ECG query error: \(e)")
592+
group.leave()
593+
@unknown default:
594+
print("ECG query unknown result")
595+
group.leave()
559596
}
560-
case .done:
561-
semaphore.signal()
562-
case let .error(error):
563-
print(error)
564-
@unknown default:
565-
print("Unknown error occurred")
566597
}
598+
self.healthStore.execute(q)
599+
}
600+
601+
group.notify(queue: .main) {
602+
result(dictionaries)
567603
}
568-
healthStore.execute(voltageQuery)
569-
semaphore.wait()
570-
return [
571-
"uuid": "\(sample.uuid)",
572-
"voltageValues": voltageValues,
573-
"averageHeartRate": sample.averageHeartRate?.doubleValue(
574-
for: HKUnit.count().unitDivided(by: HKUnit.minute())),
575-
"samplingFrequency": sample.samplingFrequency?.doubleValue(for: HKUnit.hertz()),
576-
"classification": sample.classification.rawValue,
577-
"date_from": Int(sample.startDate.timeIntervalSince1970 * 1000),
578-
"date_to": Int(sample.endDate.timeIntervalSince1970 * 1000),
579-
"source_id": sample.sourceRevision.source.bundleIdentifier,
580-
"source_name": sample.sourceRevision.source.name,
581-
]
582604
}
583605
}

packages/health/ios/health.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
Pod::Spec.new do |s|
55
s.name = 'health'
6-
s.version = '13.1.1'
6+
s.version = '13.1.2'
77
s.summary = 'Wrapper for Apple\'s HealthKit on iOS and Google\'s Health Connect on Android.'
88
s.description = <<-DESC
99
Wrapper for Apple's HealthKit on iOS and Google's Health Connect on Android.

packages/health/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: health
22
description: Wrapper for Apple's HealthKit on iOS and Google's Health Connect on Android.
3-
version: 13.1.1
3+
version: 13.1.2
44
homepage: https://github.com/cph-cachet/flutter-plugins/tree/master/packages/health
55

66
environment:

0 commit comments

Comments
 (0)