Skip to content

Conversation

iarata
Copy link
Contributor

@iarata iarata commented Apr 9, 2025

This PR refactors the Swift native implementation. It introduces the following new classes:

  • HealthDataWriter: writing health data to HealthKit functions
  • HealthDataReader: reading health data from HealthKit functions
  • HealthDataOperations: managing data permissions and delete functions
  • HealthConstants: constants used in the plugin (where types are introduced)
  • HealthUtilities: helper functions - also includes type conversion helpers for HKWorkoutActivityType

The initialisation of these classes are lazy now to help with performance.

In the new approach, when defining a new method for iOS native, for better error handling the cases should be handled as follow under the handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) function:

case "writeMeal":
   do {
         try healthDataWriter.writeMeal(call: call, result: result)
    } catch {
         result(FlutterError(code: "WRITE_ERROR",
                    message: "Error writing meal: \(error.localizedDescription)",
                    details: nil))
    }

This now requires each new method to throw an error in case of failure.

Supported error codes to be used:

  • REQUEST_AUTH_ERROR: Error requesting authorisation
  • PERMISSION_ERROR: Error checking permissions
  • WRITE_ERROR: Error writing [DataType]
  • DELETE_ERROR: Error deleting data
  • ARGUMENT_ERROR: Missing required argument
  • INVALID_TYPE: Invalid dataTypeKey
  • HEALTH_ERROR: Error getting health data
  • INTERNAL_ERROR: Internal instance reference lost
  • STATISTICS_ERROR: Error getting statistics
  • STEPS_ERROR: Error getting step count

@iarata iarata requested review from Panosfunk and bardram April 9, 2025 08:14
@iarata iarata self-assigned this Apr 9, 2025
@iarata
Copy link
Contributor Author

iarata commented Apr 30, 2025

Only files under packages/health/* are relevant. The rest of the changes are the synced contributions to master

iarata added 4 commits May 5, 2025 14:51
# Conflicts:
#	packages/health/CHANGELOG.md
#	packages/health/ios/Classes/SwiftHealthPlugin.swift
#	packages/health/ios/health.podspec
#	packages/health/pubspec.yaml
…12/refactor

# Conflicts:
#	packages/health/CHANGELOG.md
#	packages/health/ios/health.podspec
#	packages/health/pubspec.yaml
@Panosfunk
Copy link
Contributor

Error deleting data:

Error deleting HKQuantityTypeIdentifierActiveEnergyBurned Sample: dataObjects cannot be empty
Error deleting HKDataTypeIdentifierAudiogram Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBasalEnergyBurned Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBloodGlucose Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierOxygenSaturation Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBloodPressureDiastolic Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBloodPressureSystolic Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBodyFatPercentage Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBodyMassIndex Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBodyTemperature Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDietaryCarbohydrates Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDietaryCaffeine Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDietaryEnergyConsumed Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDietaryFatTotal Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDietaryProtein Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierElectrodermalActivity Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierForcedExpiratoryVolume1 Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierHeartRate Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierHeartRateVariabilitySDNN Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierHeight Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierRespiratoryRate Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierPeripheralPerfusionIndex Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierStepCount Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierWaistCircumference Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierBodyMass Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierFlightsClimbed Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDistanceWalkingRunning Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierMindfulSession Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierSleepAnalysis Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierSleepAnalysis Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierSleepAnalysis Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierSleepAnalysis Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierSleepAnalysis Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierSleepAnalysis Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierDietaryWater Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierAppleExerciseTime Sample: dataObjects cannot be empty
Error deleting HKWorkoutTypeIdentifier Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierHeadache Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierHeadache Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierHeadache Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierHeadache Sample: dataObjects cannot be empty
Error deleting HKCategoryTypeIdentifierHeadache Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierLeanBodyMass Sample: dataObjects cannot be empty
Error deleting HKCorrelationTypeIdentifierFood Sample: dataObjects cannot be empty
Warning: Health data type 'GENDER' not found in dataTypesDict
Warning: Health data type 'BLOOD_TYPE' not found in dataTypesDict
Warning: Health data type 'BIRTH_DATE' not found in dataTypesDict
Error deleting HKCategoryTypeIdentifierMenstrualFlow Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierWaterTemperature Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierUnderwaterDepth Sample: dataObjects cannot be empty
Error deleting HKQuantityTypeIdentifierUVExposure Sample: dataObjects cannot be empty

@Panosfunk
Copy link
Contributor

It worked well on the Carp Studies, example app gives error above but data seems to be deleted nevertheless.

Copy link
Contributor

@Panosfunk Panosfunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Copy link
Contributor

@bardram bardram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the same errors when deleting data as reported by @Panosfunk - but othervise it seems to work fine.

But I get the

Warning: Health data type 'GENDER' not found in dataTypesDict
Warning: Health data type 'BLOOD_TYPE' not found in dataTypesDict
Warning: Health data type 'BIRTH_DATE' not found in dataTypesDict

when trying to authenticate. Don't know what this means?

bardram and others added 2 commits May 17, 2025 20:59
- Includes the Swift Version on the podspec for the health plugin
@sikandernoori
Copy link

Any update on this release. waiting for feature blocked in https://github.com/cph-cachet/flutter-plugins/issues/1178 in favour of Health 13

@iarata
Copy link
Contributor Author

iarata commented Jun 3, 2025

@bardram @Panosfunk issues were some data were read-only and some didn't have any samples to be deleted

@iarata iarata merged commit 067741c into health-13 Jun 3, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants