Skip to content

Commit 75f4961

Browse files
committed
Fix getDataByUUID for iOS
1 parent 9a8972f commit 75f4961

2 files changed

Lines changed: 18 additions & 28 deletions

File tree

packages/health/ios/Classes/HealthDataReader.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ class HealthDataReader {
319319
healthStore.execute(query)
320320
}
321321

322+
/// Gets single health data by UUID
323+
/// - Parameters:
324+
/// - call: Flutter method call
325+
/// - result: Flutter result callback
322326
func getDataByUUID(call: FlutterMethodCall, result: @escaping FlutterResult) {
323327

324328
guard let arguments = call.arguments as? NSDictionary,
@@ -333,9 +337,6 @@ class HealthDataReader {
333337
}
334338

335339
let dataUnitKey = arguments["dataUnitKey"] as? String
336-
let recordingMethodsToFilter = (arguments["recordingMethodsToFilter"] as? [Int]) ?? []
337-
let includeManualEntry = !recordingMethodsToFilter.contains(HealthConstants.RecordingMethod.manual.rawValue)
338-
339340
var unit: HKUnit?
340341
if let dataUnitKey = dataUnitKey {
341342
unit = unitDict[dataUnitKey] // Ensure unitDict exists and contains the key
@@ -351,7 +352,7 @@ class HealthDataReader {
351352
}
352353

353354
guard let uuid = UUID(uuidString: uuidarg) else {
354-
result(false)
355+
result(nil)
355356
return
356357
}
357358

@@ -360,11 +361,6 @@ class HealthDataReader {
360361
let sourceIdForCharacteristic = "com.apple.Health"
361362
let sourceNameForCharacteristic = "Health"
362363

363-
if (!includeManualEntry) {
364-
let manualPredicate = NSPredicate(format: "metadata.%K != YES", HKMetadataKeyWasUserEntered)
365-
predicate = NSCompoundPredicate(type: .and, subpredicates: [predicate, manualPredicate])
366-
}
367-
368364
let query = HKSampleQuery(
369365
sampleType: dataType,
370366
predicate: predicate,
@@ -374,7 +370,6 @@ class HealthDataReader {
374370
[self]
375371
x, samplesOrNil, error in
376372

377-
378373
guard error == nil else {
379374
DispatchQueue.main.async {
380375
result(FlutterError(code: "HEALTH_ERROR",
@@ -386,7 +381,7 @@ class HealthDataReader {
386381

387382
guard let samples = samplesOrNil else {
388383
DispatchQueue.main.async {
389-
result([])
384+
result(nil)
390385
}
391386
return
392387
}
@@ -408,7 +403,7 @@ class HealthDataReader {
408403
]
409404
}
410405
DispatchQueue.main.async {
411-
result(dictionaries)
406+
result(dictionaries.first)
412407
}
413408
} else if var categorySamples = samples as? [HKCategorySample] {
414409
// filter category samples based on dataTypeKey
@@ -454,7 +449,7 @@ class HealthDataReader {
454449
]
455450
}
456451
DispatchQueue.main.async {
457-
result(categories)
452+
result(categories.first)
458453
}
459454
} else if let workoutSamples = samples as? [HKWorkout] {
460455
let dictionaries = workoutSamples.map { sample -> NSDictionary in
@@ -481,7 +476,7 @@ class HealthDataReader {
481476
}
482477

483478
DispatchQueue.main.async {
484-
result(dictionaries)
479+
result(dictionaries.first)
485480
}
486481
} else if let audiogramSamples = samples as? [HKAudiogramSample] {
487482
let dictionaries = audiogramSamples.map { sample -> NSDictionary in
@@ -507,7 +502,7 @@ class HealthDataReader {
507502
]
508503
}
509504
DispatchQueue.main.async {
510-
result(dictionaries)
505+
result(dictionaries.first)
511506
}
512507
} else if let nutritionSamples = samples as? [HKCorrelation] {
513508
var foods: [[String: Any?]] = []
@@ -543,13 +538,13 @@ class HealthDataReader {
543538
}
544539

545540
DispatchQueue.main.async {
546-
result(foods)
541+
result(foods.first)
547542
}
548543
} else {
549544
if #available(iOS 14.0, *), let ecgSamples = samples as? [HKElectrocardiogram] {
550545
let dictionaries = ecgSamples.map(self.fetchEcgMeasurements)
551546
DispatchQueue.main.async {
552-
result(dictionaries)
547+
result(dictionaries.first)
553548
}
554549
} else {
555550
DispatchQueue.main.async {

packages/health/lib/src/health_plugin.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,13 +1033,6 @@ class Health {
10331033
required String uuid,
10341034
required HealthDataType type,
10351035
}) async {
1036-
if (Platform.isAndroid) {
1037-
throw HealthException(
1038-
type,
1039-
'getHealthDataByUUID is not available for Android at this moment.',
1040-
);
1041-
}
1042-
10431036
if (uuid.isEmpty) {
10441037
throw HealthException(type, 'UUID is empty!');
10451038
}
@@ -1246,13 +1239,15 @@ class Health {
12461239
'uuid': uuid,
12471240
};
12481241

1249-
final fetchedDataPoints =
1250-
await _channel.invokeMethod('getDataByUUID', args);
1242+
final fetchedDataPoint = await _channel.invokeMethod('getDataByUUID', args);
12511243

1252-
if (fetchedDataPoints != null) {
1244+
// fetchedDataPoint is Map<Object, Object>. // Must be converted to List first
1245+
// so no need to recreate _parse() to handle single HealthDataPoint.
1246+
1247+
if (fetchedDataPoint != null) {
12531248
final msg = <String, dynamic>{
12541249
"dataType": dataType,
1255-
"dataPoints": fetchedDataPoints,
1250+
"dataPoints": [fetchedDataPoint],
12561251
};
12571252

12581253
// get single record of parsed fetchedDataPoints

0 commit comments

Comments
 (0)