From 0a72448110cc572a9f8720efa67b9db819ce6a1d Mon Sep 17 00:00:00 2001 From: Nanda Kista Permana Date: Thu, 10 Apr 2025 14:21:01 +0700 Subject: [PATCH] Added deviceModel for iOS to identify data coming from iPhone, Watch, or Others --- .../health/ios/Classes/SwiftHealthPlugin.swift | 1 + packages/health/lib/health.g.dart | 2 ++ packages/health/lib/src/health_data_point.dart | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/health/ios/Classes/SwiftHealthPlugin.swift b/packages/health/ios/Classes/SwiftHealthPlugin.swift index 8b4fa1bbf..3a71a121e 100644 --- a/packages/health/ios/Classes/SwiftHealthPlugin.swift +++ b/packages/health/ios/Classes/SwiftHealthPlugin.swift @@ -902,6 +902,7 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin { "date_to": Int(sample.endDate.timeIntervalSince1970 * 1000), "source_id": sample.sourceRevision.source.bundleIdentifier, "source_name": sample.sourceRevision.source.name, + "device_model": sample.device?.model ?? "unknown", "recording_method": (sample.metadata?[HKMetadataKeyWasUserEntered] as? Bool == true) ? RecordingMethod.manual.rawValue : RecordingMethod.automatic.rawValue, diff --git a/packages/health/lib/health.g.dart b/packages/health/lib/health.g.dart index d985c19dc..701be9c5e 100644 --- a/packages/health/lib/health.g.dart +++ b/packages/health/lib/health.g.dart @@ -27,6 +27,7 @@ HealthDataPoint _$HealthDataPointFromJson(Map json) => : WorkoutSummary.fromJson( json['workoutSummary'] as Map), metadata: json['metadata'] as Map?, + deviceModel: json['deviceModel'] as String?, ); Map _$HealthDataPointToJson(HealthDataPoint instance) => @@ -45,6 +46,7 @@ Map _$HealthDataPointToJson(HealthDataPoint instance) => if (instance.workoutSummary?.toJson() case final value?) 'workoutSummary': value, if (instance.metadata case final value?) 'metadata': value, + if (instance.deviceModel case final value?) 'deviceModel': value, }; const _$HealthDataTypeEnumMap = { diff --git a/packages/health/lib/src/health_data_point.dart b/packages/health/lib/src/health_data_point.dart index 029af835c..70dc1d882 100644 --- a/packages/health/lib/src/health_data_point.dart +++ b/packages/health/lib/src/health_data_point.dart @@ -55,6 +55,11 @@ class HealthDataPoint { /// The metadata for this data point. Map? metadata; + /// The source of the data, whether from the iPhone or Watch or something else. + /// Only available fo iOS + /// On Android: always return null + String? deviceModel; + HealthDataPoint({ required this.uuid, required this.value, @@ -69,6 +74,7 @@ class HealthDataPoint { this.recordingMethod = RecordingMethod.unknown, this.workoutSummary, this.metadata, + this.deviceModel, }) { // set the value to minutes rather than the category // returned by the native API @@ -137,6 +143,7 @@ class HealthDataPoint { : Map.from(dataPoint['metadata'] as Map); final unit = dataTypeToUnit[dataType] ?? HealthDataUnit.UNKNOWN_UNIT; final String? uuid = dataPoint["uuid"] as String?; + final String? deviceModel = dataPoint["device_model"] as String?; // Set WorkoutSummary, if available. WorkoutSummary? workoutSummary; @@ -163,6 +170,7 @@ class HealthDataPoint { recordingMethod: RecordingMethod.fromInt(recordingMethod), workoutSummary: workoutSummary, metadata: metadata, + deviceModel: deviceModel, ); } @@ -180,7 +188,8 @@ class HealthDataPoint { sourceName: $sourceName recordingMethod: $recordingMethod workoutSummary: $workoutSummary - metadata: $metadata"""; + metadata: $metadata + deviceModel: $deviceModel"""; @override bool operator ==(Object other) => @@ -196,9 +205,10 @@ class HealthDataPoint { sourceId == other.sourceId && sourceName == other.sourceName && recordingMethod == other.recordingMethod && - metadata == other.metadata; + metadata == other.metadata && + deviceModel == other.deviceModel; @override int get hashCode => Object.hash(uuid, value, unit, dateFrom, dateTo, type, - sourcePlatform, sourceDeviceId, sourceId, sourceName, metadata); + sourcePlatform, sourceDeviceId, sourceId, sourceName, metadata, deviceModel); }