spike_flutter_sdk 0.0.9
spike_flutter_sdk: ^0.0.9 copied to clipboard
Reads HealthKit data from iOS HealthKit, and allows you to sync it with your chosen backend.
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 #
- Read the Apple HealthKit data.
- Send this data to backend.
- Register periodic background tasks to send and read the data.
- 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. Registering SpikeTaskRunner as a way to send data to backend periodically.
*/
SpikeTaskRunner.registerTask({
origin: _origin,
clientId: _clientId,
authToken: _authToken,
callbackUrl: _callbackUrl,
});
/*
* 12. 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();
/*
* 13. 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.