Skip to content

Commit

Permalink
PR #919 & support for HealthPlatformType
Browse files Browse the repository at this point in the history
  • Loading branch information
bardram committed Apr 3, 2024
1 parent f6326cb commit 122b3ae
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 80 deletions.
4 changes: 3 additions & 1 deletion packages/health/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
## 10.2.0

* Using named parameters in most methods for consistency.
* Improved support for Google Health Connect
* Added a `HealthPlatformType` to save which health platform the data originates from (Apple Health, Google Fit, or Google Health Connect).
* Android: Improved support for Google Health Connect
* getHealthConnectSdkStatus, PR [#941](https://github.com/cph-cachet/flutter-plugins/pull/941)
* installHealthConnect, PR [#943](https://github.com/cph-cachet/flutter-plugins/pull/943)
* workout title, PR [#938](https://github.com/cph-cachet/flutter-plugins/pull/938)
* iOS: Add support for saving blood pressure as a correlation, PR [#919](https://github.com/cph-cachet/flutter-plugins/pull/919)

## 10.1.1

Expand Down
28 changes: 23 additions & 5 deletions packages/health/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,35 @@ HealthDataType type;
HealthDataUnit unit;
DateTime dateFrom;
DateTime dateTo;
PlatformType platform;
String uuid, deviceId;
HealthPlatformType sourcePlatform;
String sourceDeviceId;
String sourceId;
String sourceName;
bool isManualEntry;
WorkoutSummary? workoutSummary;
```

where a [HealthValue](https://pub.dev/documentation/health/latest/health/HealthValue-class.html) can be any type of `AudiogramHealthValue`, `ElectrocardiogramHealthValue`, `ElectrocardiogramVoltageValue`, `NumericHealthValue`, `NutritionHealthValue`, or `WorkoutHealthValue`.

A `HealthDataPoint` object can be serialized to and from JSON using the `toJson()` and `fromJson()` methods. JSON serialization is using camel_case notation.
where a [`HealthValue`](https://pub.dev/documentation/health/latest/health/HealthValue-class.html) can be any type of `AudiogramHealthValue`, `ElectrocardiogramHealthValue`, `ElectrocardiogramVoltageValue`, `NumericHealthValue`, `NutritionHealthValue`, or `WorkoutHealthValue`.

A `HealthDataPoint` object can be serialized to and from JSON using the `toJson()` and `fromJson()` methods. JSON serialization is using camel_case notation. Null values are not serialized. For example;

```json
{
"value": {
"__type": "NumericHealthValue",
"numeric_value": 141.0
},
"type": "STEPS",
"unit": "COUNT",
"date_from": "2024-04-03T10:06:57.736",
"date_to": "2024-04-03T10:12:51.724",
"source_platform": "appleHealth",
"source_device_id": "F74938B9-C011-4DE4-AA5E-CF41B60B96E7",
"source_id": "com.apple.health.81AE7156-EC05-47E3-AC93-2D6F65C717DF",
"source_name": "iPhone12.bardram.net",
"is_manual_entry": false
}
```

### Fetch health data

Expand Down
34 changes: 15 additions & 19 deletions packages/health/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import 'package:health_example/util.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:carp_serializable/carp_serializable.dart';

// /// A connivent function to convert a Dart object into a formatted JSON string.
// String toJsonString(Object? object) =>
// const JsonEncoder.withIndent(' ').convert(object);

void main() => runApp(HealthApp());

class HealthApp extends StatefulWidget {
Expand Down Expand Up @@ -135,23 +131,23 @@ class _HealthAppState extends State<HealthApp> {
// Clear old data points
_healthDataList.clear();

try {
// fetch health data
List<HealthDataPoint> healthData = await Health().getHealthDataFromTypes(
types: types,
startTime: yesterday,
endTime: now,
);
// try {
// fetch health data
List<HealthDataPoint> healthData = await Health().getHealthDataFromTypes(
types: types,
startTime: yesterday,
endTime: now,
);

debugPrint('Total number of data points: ${healthData.length}. '
'${healthData.length > 100 ? 'Only showing the first 100.' : ''}');
debugPrint('Total number of data points: ${healthData.length}. '
'${healthData.length > 100 ? 'Only showing the first 100.' : ''}');

// save all the new data points (only the first 100)
_healthDataList.addAll(
(healthData.length < 100) ? healthData : healthData.sublist(0, 100));
} catch (error) {
debugPrint("Exception in getHealthDataFromTypes: $error");
}
// save all the new data points (only the first 100)
_healthDataList.addAll(
(healthData.length < 100) ? healthData : healthData.sublist(0, 100));
// } catch (error) {
// debugPrint("Exception in getHealthDataFromTypes: $error");
// }

// filter out duplicates
_healthDataList = Health().removeDuplicates(_healthDataList);
Expand Down
16 changes: 9 additions & 7 deletions packages/health/lib/health.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions packages/health/lib/src/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class HealthException implements Exception {
"Error requesting health data type '$dataType' - cause: $cause";
}

/// A list of supported platforms.
enum PlatformType { IOS, ANDROID }

/// The status of Google Health Connect.
///
/// **NOTE** - The enum order is arbitrary. If you need the native value,
Expand Down
29 changes: 16 additions & 13 deletions packages/health/lib/src/health_data_point.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
part of '../health.dart';

/// Types of health platforms.
enum HealthPlatformType { appleHealth, googleFit, googleHealthConnect }

/// A [HealthDataPoint] object corresponds to a data point capture from
/// Apple HealthKit or Google Fit or Google Health Connect with a [HealthValue]
/// as value.
Expand All @@ -26,11 +29,11 @@ class HealthDataPoint {
/// The end of the time interval.
DateTime dateTo;

/// The software platform of the data point.
PlatformType platform;
/// The health platform that this data point was fetched.
HealthPlatformType sourcePlatform;

/// The id of the device from which the data point was fetched.
String deviceId;
String sourceDeviceId;

/// The id of the source from which the data point was fetched.
String sourceId;
Expand All @@ -50,8 +53,8 @@ class HealthDataPoint {
required this.unit,
required this.dateFrom,
required this.dateTo,
required this.platform,
required this.deviceId,
required this.sourcePlatform,
required this.sourceDeviceId,
required this.sourceId,
required this.sourceName,
this.isManualEntry = false,
Expand Down Expand Up @@ -131,8 +134,8 @@ class HealthDataPoint {
unit: unit,
dateFrom: from,
dateTo: to,
platform: Health().platformType,
deviceId: Health().deviceId,
sourcePlatform: Health().platformType,
sourceDeviceId: Health().deviceId,
sourceId: sourceId,
sourceName: sourceName,
isManualEntry: isManualEntry,
Expand All @@ -147,8 +150,8 @@ class HealthDataPoint {
dateFrom: $dateFrom,
dateTo: $dateTo,
dataType: ${type.name},
platform: $platform,
deviceId: $deviceId,
platform: $sourcePlatform,
deviceId: $sourceDeviceId,
sourceId: $sourceId,
sourceName: $sourceName
isManualEntry: $isManualEntry
Expand All @@ -162,13 +165,13 @@ class HealthDataPoint {
dateFrom == other.dateFrom &&
dateTo == other.dateTo &&
type == other.type &&
platform == other.platform &&
deviceId == other.deviceId &&
sourcePlatform == other.sourcePlatform &&
sourceDeviceId == other.sourceDeviceId &&
sourceId == other.sourceId &&
sourceName == other.sourceName &&
isManualEntry == other.isManualEntry;

@override
int get hashCode => Object.hash(value, unit, dateFrom, dateTo, type, platform,
deviceId, sourceId, sourceName);
int get hashCode => Object.hash(value, unit, dateFrom, dateTo, type,
sourcePlatform, sourceDeviceId, sourceId, sourceName);
}
Loading

0 comments on commit 122b3ae

Please sign in to comment.