Skip to content

Commit

Permalink
Added fixes to heartrate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesh108 committed Feb 9, 2022
1 parent 202a4ca commit 504a3c9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Vitals.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ @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:(double)60]; // Default HR is 60

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:[HKUnit unitFromString: @"count/min"] doubleValue:heartRateValue]
quantity:[HKQuantity quantityWithUnit:unit doubleValue:heartRateValue]
startDate:timeHeartRateSampleWasTaken
endDate:timeHeartRateSampleWasTaken];

// Save the HeartRate Sample to HealthKit //
[self.healthStore saveObject:heartRate withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the heart rate sample %@. The error was: ", error);
NSLog(@"An error occured saving the heart rate sample: %@", error);
callback(@[RCTMakeError(@"An error occured saving the heart rate sample", error, nil)]);
return;
}
Expand Down

0 comments on commit 504a3c9

Please sign in to comment.