Skip to content

Commit

Permalink
- Added save insulin sample function
Browse files Browse the repository at this point in the history
  • Loading branch information
GGGava committed Jun 29, 2023
1 parent ed36912 commit 40683d5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
- (void)results_saveCarbohydratesSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)results_deleteBloodGlucoseSample:(NSString *)oid callback:(RCTResponseSenderBlock)callback;
- (void)results_deleteCarbohydratesSample:(NSString *)oid callback:(RCTResponseSenderBlock)callback;
- (void)results_saveInsulinDeliverySample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;

@end
28 changes: 28 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ - (void)results_getCarbohydratesSamples:(NSDictionary *)input callback:(RCTRespo
}];
}

- (void)results_saveInsulinDeliverySample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *insulinDeliveryType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierInsulinDelivery];

HKUnit *unit = [HKUnit internationalUnit];

double value = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:startDate];
NSDictionary *metadata = [RCTAppleHealthKit metadataFromOptions:input withDefault:nil];

HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value];
HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:insulinDeliveryType
quantity:quantity
startDate:startDate
endDate:endDate
metadata:metadata];

[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured while saving the insulin sample %@. The error was: ", error);
callback(@[RCTMakeError(@"An error occured while saving the insulin sample", error, nil)]);
return;
}
callback(@[[NSNull null], [sample.UUID UUIDString]]);
}];
}

- (void)results_saveBloodGlucoseSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *bloodGlucoseType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose];
Expand Down
2 changes: 2 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ - (nullable HKObjectType *)getWritePermFromText:(nonnull NSString*) key {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
} else if ([@"BloodGlucose" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose];
} else if ([@"InsulinDelivery" isEqualToString:key]) {
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierInsulinDelivery];
}

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

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

RCT_EXPORT_METHOD(saveCarbohydratesSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self _initializeHealthStore];
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ declare module 'react-native-health' {
callback: (err: string, results: Array<HealthValue>) => void,
): void

saveInsulinDeliverySample(
options: HealthValueOptions,
callback: (err: string, results: HealthValue) => void,
): void


Constants: Constants
}
Expand Down Expand Up @@ -470,6 +475,7 @@ declare module 'react-native-health' {

export interface RecordMetadata {
HKBloodGlucoseMealTime?: BloodGlucoseMealTime
HKInsulinDeliveryReason?: InsulinDeliveryReason
HKWasUserEntered?: boolean
[key: string]: string | number | boolean | undefined
}
Expand Down

0 comments on commit 40683d5

Please sign in to comment.