From cc22bd9e2b6c98cfd490dcc6be2b7ce7ea3929ee Mon Sep 17 00:00:00 2001 From: bardram Date: Sat, 30 Mar 2024 11:05:05 +0100 Subject: [PATCH 1/2] fix of workout serialization & fromHealthDataPoint factory --- packages/health/CHANGELOG.md | 5 +++ packages/health/lib/health.dart | 2 +- packages/health/lib/health.g.dart | 8 ++-- .../health/lib/src/health_data_point.dart | 7 +-- ...{data_types.dart => heath_data_types.dart} | 0 packages/health/lib/src/workout_summary.dart | 45 +++++++++---------- packages/health/pubspec.yaml | 2 +- 7 files changed, 32 insertions(+), 37 deletions(-) rename packages/health/lib/src/{data_types.dart => heath_data_types.dart} (100%) diff --git a/packages/health/CHANGELOG.md b/packages/health/CHANGELOG.md index 56b44a6a4..9e3779d0c 100644 --- a/packages/health/CHANGELOG.md +++ b/packages/health/CHANGELOG.md @@ -1,3 +1,7 @@ +## 10.1.0 + +* fix of error in `WorkoutSummary` JSON serialization. + ## 10.0.0 * **BREAKING** The plugin now works as a singleton using `Health()` to access it (instead of creating an instance of `HealthFactory`). @@ -6,6 +10,7 @@ * Support for new data types: * body water mass, PR [#917](https://github.com/cph-cachet/flutter-plugins/pull/917) * caffeine, PR [#924](https://github.com/cph-cachet/flutter-plugins/pull/924) + * workout summary, manual entry and new health data types, PR [#920](https://github.com/cph-cachet/flutter-plugins/pull/920) * Fixed `SleepSessionRecord`, PR [#928](https://github.com/cph-cachet/flutter-plugins/pull/928) * Update to API and README docs * Upgrade to Dart 3.2 and Flutter 3. diff --git a/packages/health/lib/health.dart b/packages/health/lib/health.dart index d33ffd440..1c960d54f 100644 --- a/packages/health/lib/health.dart +++ b/packages/health/lib/health.dart @@ -10,7 +10,7 @@ import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; -part 'src/data_types.dart'; +part 'src/heath_data_types.dart'; part 'src/functions.dart'; part 'src/health_data_point.dart'; part 'src/health_value_types.dart'; diff --git a/packages/health/lib/health.g.dart b/packages/health/lib/health.g.dart index 0a85cd51e..1e52a3762 100644 --- a/packages/health/lib/health.g.dart +++ b/packages/health/lib/health.g.dart @@ -530,10 +530,10 @@ Map _$NutritionHealthValueToJson( WorkoutSummary _$WorkoutSummaryFromJson(Map json) => WorkoutSummary( - json['workout_type'] as String, - json['total_distance'] as num, - json['total_energy_burned'] as num, - json['total_steps'] as num, + workoutType: json['workout_type'] as String, + totalDistance: json['total_distance'] as num, + totalEnergyBurned: json['total_energy_burned'] as num, + totalSteps: json['total_steps'] as num, ); Map _$WorkoutSummaryToJson(WorkoutSummary instance) => diff --git a/packages/health/lib/src/health_data_point.dart b/packages/health/lib/src/health_data_point.dart index 8d75f76a3..a734aa690 100644 --- a/packages/health/lib/src/health_data_point.dart +++ b/packages/health/lib/src/health_data_point.dart @@ -122,12 +122,7 @@ class HealthDataPoint { dataPoint["total_distance"] != null || dataPoint["total_energy_burned"] != null || dataPoint["total_steps"] != null) { - workoutSummary = WorkoutSummary( - dataPoint["workout_type"] as String? ?? '', - dataPoint["total_distance"] as num? ?? 0, - dataPoint["total_energy_burned"] as num? ?? 0, - dataPoint["total_steps"] as num? ?? 0, - ); + workoutSummary = WorkoutSummary.fromHealthDataPoint(dataPoint); } return HealthDataPoint( diff --git a/packages/health/lib/src/data_types.dart b/packages/health/lib/src/heath_data_types.dart similarity index 100% rename from packages/health/lib/src/data_types.dart rename to packages/health/lib/src/heath_data_types.dart diff --git a/packages/health/lib/src/workout_summary.dart b/packages/health/lib/src/workout_summary.dart index 81dab385c..c36613802 100644 --- a/packages/health/lib/src/workout_summary.dart +++ b/packages/health/lib/src/workout_summary.dart @@ -1,9 +1,11 @@ part of '../health.dart'; /// A [WorkoutSummary] object store vary metrics of a workout. -/// * totalDistance - The total distance that was traveled during a workout. -/// * totalEnergyBurned - The amount of energy that was burned during a workout. -/// * totalSteps - The count of steps was burned during a workout. +/// +/// * [workoutType] - The type of workout. See [HealthWorkoutActivityType] for available types. +/// * [totalDistance] - The total distance that was traveled during a workout. +/// * [totalEnergyBurned] - The amount of energy that was burned during a workout. +/// * [totalSteps] - The number of steps during a workout. @JsonSerializable(fieldRename: FieldRename.snake, includeIfNull: false) class WorkoutSummary { /// Workout type. @@ -18,12 +20,21 @@ class WorkoutSummary { /// The total steps value of the workout. num totalSteps; - WorkoutSummary( - this.workoutType, - this.totalDistance, - this.totalEnergyBurned, - this.totalSteps, - ); + WorkoutSummary({ + required this.workoutType, + required this.totalDistance, + required this.totalEnergyBurned, + required this.totalSteps, + }); + + /// Create a [WorkoutSummary] based on a health data point from native data format. + factory WorkoutSummary.fromHealthDataPoint(dynamic dataPoint) => + WorkoutSummary( + workoutType: dataPoint['workout_type'] as String? ?? '', + totalDistance: dataPoint['total_distance'] as num? ?? 0, + totalEnergyBurned: dataPoint['total_energy_burned'] as num? ?? 0, + totalSteps: dataPoint['total_steps'] as num? ?? 0, + ); /// Create a [HealthDataPoint] from json. factory WorkoutSummary.fromJson(Map json) => @@ -32,22 +43,6 @@ class WorkoutSummary { /// Convert this [HealthDataPoint] to json. Map toJson() => _$WorkoutSummaryToJson(this); - // /// Converts a json object to the [WorkoutSummary] - // factory WorkoutSummary.fromJson(json) => WorkoutSummary( - // json['workoutType'], - // json['totalDistance'], - // json['totalEnergyBurned'], - // json['totalSteps'], - // ); - - // /// Converts the [WorkoutSummary] to a json object - // Map toJson() => { - // 'workoutType': workoutType, - // 'totalDistance': totalDistance, - // 'totalEnergyBurned': totalEnergyBurned, - // 'totalSteps': totalSteps - // }; - @override String toString() => '$runtimeType - ' 'workoutType: $workoutType' diff --git a/packages/health/pubspec.yaml b/packages/health/pubspec.yaml index 578b4b8f2..124f47f12 100644 --- a/packages/health/pubspec.yaml +++ b/packages/health/pubspec.yaml @@ -1,6 +1,6 @@ name: health description: Wrapper for HealthKit on iOS and Google Fit and Health Connect on Android. -version: 10.0.0 +version: 10.1.0 homepage: https://github.com/cph-cachet/flutter-plugins/tree/master/packages/health environment: From 211a2b7873cc926a47d91f0e6cdd291a4061a244 Mon Sep 17 00:00:00 2001 From: bardram Date: Sat, 30 Mar 2024 11:34:47 +0100 Subject: [PATCH 2/2] PR #926 included --- packages/health/CHANGELOG.md | 1 + .../src/main/kotlin/cachet/plugins/health/HealthPlugin.kt | 2 +- packages/health/ios/Classes/SwiftHealthPlugin.swift | 6 ++++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/health/CHANGELOG.md b/packages/health/CHANGELOG.md index 9e3779d0c..49129463e 100644 --- a/packages/health/CHANGELOG.md +++ b/packages/health/CHANGELOG.md @@ -1,6 +1,7 @@ ## 10.1.0 * fix of error in `WorkoutSummary` JSON serialization. +* empty value check for calories nutrition, PR [#926](https://github.com/cph-cachet/flutter-plugins/pull/926) ## 10.0.0 diff --git a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt index 63323ba88..89affa122 100644 --- a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt +++ b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt @@ -3002,7 +3002,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) : } is BodyTemperatureRecord -> return listOf( - mapOf( + mapOf(w "value" to record.temperature .inCelsius, diff --git a/packages/health/ios/Classes/SwiftHealthPlugin.swift b/packages/health/ios/Classes/SwiftHealthPlugin.swift index 3e199d2ed..a85a51197 100644 --- a/packages/health/ios/Classes/SwiftHealthPlugin.swift +++ b/packages/health/ios/Classes/SwiftHealthPlugin.swift @@ -465,8 +465,10 @@ public class SwiftHealthPlugin: NSObject, FlutterPlugin { var nutrition = Set() - let caloriesSample = HKQuantitySample(type: HKSampleType.quantityType(forIdentifier: .dietaryEnergyConsumed)!, quantity: HKQuantity(unit: HKUnit.kilocalorie(), doubleValue: calories), start: dateFrom, end: dateTo, metadata: metadata) - nutrition.insert(caloriesSample) + if(calories > 0) { + let caloriesSample = HKQuantitySample(type: HKSampleType.quantityType(forIdentifier: .dietaryEnergyConsumed)!, quantity: HKQuantity(unit: HKUnit.kilocalorie(), doubleValue: calories), start: dateFrom, end: dateTo, metadata: metadata) + nutrition.insert(caloriesSample) + } if(carbs > 0) { let carbsSample = HKQuantitySample(type: HKSampleType.quantityType(forIdentifier: .dietaryCarbohydrates)!, quantity: HKQuantity(unit: HKUnit.gram(), doubleValue: carbs), start: dateFrom, end: dateTo, metadata: metadata)