Skip to content

Added saveHeartRateSample to the library #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@interface RCTAppleHealthKit (Methods_Vitals)

- (void)vitals_saveHeartRateSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)vitals_getHeartRateSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)vitals_getRestingHeartRate:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)vitals_getWalkingHeartRateAverage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
Expand Down
27 changes: 27 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@

@implementation RCTAppleHealthKit (Methods_Vitals)

- (void)vitals_saveHeartRateSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
NSDate *timeHeartRateSampleWasTaken = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]];
double heartRateValue = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:-99];
if(heartRateValue == -99){
callback(@[RCTMakeError(@"heartRateValue is required in options", nil, nil)]);
return;
}

HKUnit *count = [HKUnit countUnit];
HKUnit *minute = [HKUnit minuteUnit];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[count unitDividedByUnit:minute]];

HKQuantitySample* heartRate = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
quantity:[HKQuantity quantityWithUnit:unit doubleValue:heartRateValue]
startDate:timeHeartRateSampleWasTaken
endDate:timeHeartRateSampleWasTaken];

[self.healthStore saveObject:heartRate withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the heart rate sample: %@", error);
callback(@[RCTMakeError(@"An error occured saving the heart rate sample", error, nil)]);
return;
}
callback(@[[NSNull null], @true]);
}];
}

- (void)vitals_getHeartRateSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
Expand Down
3 changes: 3 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ - (nullable HKObjectType *)getWritePermFromText:(nonnull NSString*) key {
if ([@"BloodAlcoholContent" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodAlcoholContent];
}
if ([@"HeartRate" isEqualToString: key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
}

return nil;
}
Expand Down
6 changes: 6 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ + (BOOL)requiresMainQueueSetup
[self getWater:input callback:callback];
}

RCT_EXPORT_METHOD(saveHeartRateSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
[self vitals_saveHeartRateSample:input callback:callback];
}

RCT_EXPORT_METHOD(getWaterSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ All the documentation is under the [docs](/docs) folder. They are split into the

- [getBloodPressureSamples](/docs/getBloodPressureSamples.md)
- [getElectrocardiogramSamples](/docs/getElectrocardiogramSamples.md)
- [saveHeartRateSample](/docs/saveHeartRateSample.md)
- [getHeartRateSamples](/docs/getHeartRateSamples.md)
- [getHeartRateVariabilitySamples](/docs/getHeartRateVariabilitySamples.md)
- [getHeartbeatSeriesSamples](/docs/getHeartbeatSeriesSamples.md)
Expand Down
33 changes: 33 additions & 0 deletions docs/saveHeartRateSample.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# saveHeartRateSample

Save a Heart Rate sample to Healthkit

`saveHeartRateSample` accepts an options object containing a numeric heart rate value in Beats per minute (BPM).
In the example below a heart rate sample of 72BPM is added to Healthkit.

Example input options:

```javascript
let options = {
value: 72, // Amount in BPM
date: (new Date(2018,10,1)).toISOString(), // Optional, time when the Heart Rate was logged
}
```

Call the method:

```javascript
AppleHealthKit.saveHeartRateSample((options: Object), (err: Object, results: boolean) => {
if (err) {
console.log('Error saving Heart Rate to Healthkit: ', err)
return
}
// Heart Rate successfully saved
})
```

Example output:

```json
1
```
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ declare module 'react-native-health' {
callback: (err: string, results: HealthValue) => void,
): void

saveHeartRateSample(
options: HealthValueOptions,
callback: (error: string, result: HealthValue) => void,
): void

getWaterSamples(
options: HealthInputOptions,
callback: (err: string, results: Array<HealthValue>) => void,
Expand Down