Spike Flutter SDK (version 0.0.4) allows you to read the Apple HealthKit data (based on another package - https://pub.dev/packages/health_kit_reporter ). Then, it allows you to send the data to your backend chosen. Also, you can enable background tasks you want. And finally, it is possible to enable event tracking to see if everything works as intended.

Features

  1. Read the Apple HealthKit data.
  2. Send this data to backend.
  3. Register periodic background tasks to send and read the data.
  4. Track various events of the package by utilizing shared preferences.

Getting started

Setup everything like you would to with https://pub.dev/packages/health_kit_reporter . Then, use this library as shown in example below (or in the longer one). Then, if you want to use background tasks for the Health Kit data processing and sending, you should configure your Flutter app according to this package guides: https://pub.dev/packages/background_fetch .

Usage

/*
* 1. Initialization
*/
// Your API credentials
const _authToken = 'fa0b3803-6068-4ea7-9788-eccce210d30c';
const _clientId = 'ea9e03f5-be45-49fb-bf4c-47a88c184c3b';
const _host = 'https://api.spikeapi.com';
const _callbackUrl = ''; // Configurable by the client.
const _userId = 'jasbdhasbfashfj';

// Origin of the endpoint you are going to sync the data with
const _origin = Origin(
  host: _host,
  userId: _userId,
);

// API to read the data from Apple HealthKit
final _reporting = HealthKitReporting();

const _api = SpikeApi(origin: _origin);

/*
* 2. Requesting authorization.
*/
await _reporting.requestReadAuthorization();

/*
* 3. Initializing user integration.
*/
final integrationResult = await _api.initIntegration(
  authToken: _authToken,
  clientId: _clientId,
);

/*
* 4. Reading Apple HealthKit summary data
*/
final appleActivitiesSummaryData = await _reporting.readActivitiesSummaryData(
  from: DateTime(2022, 09, 22),
  to: DateTime.now(),
);

/*
* 5. Sending Apple HealthKit summary data to your backend.
*/
await _api.sendSummaryData(
  authToken: _authToken,
  request: SummaryDataRequest(
    records: SummaryData.fromActivityData(appleActivitiesSummaryData),
    userId: integrationResult.userId,
    callBackUrl: _callbackUrl,
  ),
);

/*
* 5. Reading Apple HealthKit specified identifier data
*/
final appleHeartRateActivities = await _reporting.readIdentifierData(
  from: DateTime(2022, 09, 22),
  to: DateTime.now(),
  quantity: QuantityType.heartRate.name,
);

/*
* 6. Sending Apple HealthKit specified identifier data to your backend.
*/
await _api.sendIdentifierData(
  authToken: _authToken,
  request: IdentifierDataRequest(
    identifier: QuantityType.heartRate.name,
    activities: appleHeartRateActivities,
    userId: integrationResult.userId,
    callBackUrl: _callbackUrl,
  ),
);

/*
* 7. Reading Apple HealthKit workouts data.
*/
final workoutRecords = await _reporting.readWorkouts(
  from: DateTime(2022, 09, 22),
  to: DateTime.now(),
);

/*
* 8. Sending Apple HealthKit workouts data to your backend.
*/
await _api.sendWorkoutsData(
  authToken: _authToken,
  request: WorkoutsDataRequest(
    workouts: WorkoutsData.fromWorkoutRecords(workoutRecords),
    userId: integrationResult.userId,
    callBackUrl: _callbackUrl,
  ),
);

/*
* 9. Reading Apple HealthKit specified identifier data.
*/
final appleHeartGroupActivities = await _reporting.readGroupData(
  from: DateTime(2022, 09, 22),
  to: DateTime.now(),
  group: ActivityGroup.heart,
);

/*
* 10. Sending Apple HealthKit specified group data to your backend.
*/
await _api.sendActivityGroupData(
  authToken: _authToken,
  request: ActivityGroupDataRequest(
    group: ActivityGroup.heart,
    activities: appleHeartGroupActivities,
    userId: integrationResult.userId,
    callBackUrl: _callbackUrl,
  ),
);

/*
* 11. Reading Apple HealthKit specified sleep analysis data.
*/
final sleepData = await _reporting.readSleepData(
  from: DateTime(2022, 09, 22),
  to: DateTime.now(),
);

/*
* 12. Sending Apple HealthKit specified sleep data to your backend.
*/
await _api.sendSleepData(
  authToken: _authToken,
  request: SleepDataRequest(
    data: sleepData,
    userId: integrationResult.userId,
    callBackUrl: _callbackUrl,
  ),
);

/*
* 13. Registering SpikeTaskRunner as a way to send data to backend periodically.
*/
SpikeTaskRunner.registerTask({
  origin: _origin,
  clientId: _clientId,
  authToken: _authToken,
  callbackUrl: _callbackUrl,
});

/*
* 14. SpikeEventTracker can be used for events debugging (it utilizes the internal storage of the device).
*/
final tracker = SpikeEventTracker();

// Enabling the tracking.
await tracker.setEnabled(true);

// Disabling the tracking.
await tracker.setEnabled(false);

// Receiving all of the events tracked.
final events = await tracker.getAllEvents();

// Cleaning up the tracker.
await tracker.clean();

/*
* 15. HealthKitReporting supports also enabling the background delivery.
*/
// Configure the SpikeTask configuration in order to setup desired background delivery experience (it is also needed for the background dispatch).
SpikeTaskConfigService().setSpikeTaskConfig(...);

// Enable the background delivery.
HealthKitReporting().enableBackgroundDelivery(() {
  // Perform your own action on update.
});

// Disable the background delivery.
HealthKitReporting().disableBackgroundDelivery();

Additional information

All information is here and in the GitLab page of this package.

Libraries

health_kit_reporting
model/data/date_interval
model/data/date_interval_list
model/exceptions/api_exception
model/health_data/activity/activity_data
model/health_data/activity/activity_group
model/health_data/activity/activity_identifiers
model/health_data/activity/activity_record
model/health_data/activity/activity_record_stats
model/health_data/activity/activity_record_value
model/health_data/record
model/health_data/request/activity_group_data_request
model/health_data/request/identifier_data_request
model/health_data/request/request_base
model/health_data/request/sleep_data_request
model/health_data/request/summary_data_request
model/health_data/request/workouts_data_request
model/health_data/response/activity_group_data_response
model/health_data/response/identifier_data_response
model/health_data/response/response_util
model/health_data/response/sleep_data_response
model/health_data/response/summary_data_response
model/health_data/response/workout_data_response
model/health_data/sleep/sleep_data
model/health_data/sleep/sleep_level
model/health_data/sleep/sleep_level_type
model/health_data/sleep/sleep_statistics
model/health_data/source
model/health_data/summary/record_summary_resolver
model/health_data/summary/resolvers/list_summary_record_resolver
model/health_data/summary/resolvers/record_summary_single_resolver
model/health_data/summary/summary_data
model/health_data/workouts/workout_event_record
model/health_data/workouts/workout_record
model/health_data/workouts/workouts_data
model/init_response
model/origin
model/spike_event/spike_event_type
model/spike_task_config
model/utils/date_util
reporter/exceptions
reporter/health_kit_reporter
reporter/model/decorator/extensions
reporter/model/payload/activity_summary
reporter/model/payload/category
reporter/model/payload/characteristic/activity_move_mode
reporter/model/payload/characteristic/biological_sex
reporter/model/payload/characteristic/blood_type
reporter/model/payload/characteristic/characteristic
reporter/model/payload/characteristic/fitzpatrick_skin_type
reporter/model/payload/characteristic/wheelchair_use
reporter/model/payload/correlation
reporter/model/payload/date_components
reporter/model/payload/deleted_object
reporter/model/payload/device
reporter/model/payload/document
reporter/model/payload/electrocardiogram
reporter/model/payload/heartbeat_series
reporter/model/payload/preferred_unit
reporter/model/payload/quantity
reporter/model/payload/sample
reporter/model/payload/sleep_record
reporter/model/payload/source
reporter/model/payload/source_revision
reporter/model/payload/statistics
reporter/model/payload/workout
reporter/model/payload/workout_activity_type
reporter/model/payload/workout_configuration
reporter/model/payload/workout_event
reporter/model/payload/workout_event_type
reporter/model/payload/workout_route
reporter/model/predicate
reporter/model/type/activity_summary_type
reporter/model/type/category_type
reporter/model/type/characteristic_type
reporter/model/type/correlation_type
reporter/model/type/document_type
reporter/model/type/electrocardiogram_type
reporter/model/type/quantity_type
reporter/model/type/series_type
reporter/model/type/vision_prescription_type
reporter/model/type/workout_type
reporter/model/update_frequency
spike_api
spike_event_tracker
spike_task_config_service
spike_task_runner