Skip to content

Commit 40683d5

Browse files
committed
- Added save insulin sample function
1 parent ed36912 commit 40683d5

5 files changed

+43
-0
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
- (void)results_saveCarbohydratesSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
1717
- (void)results_deleteBloodGlucoseSample:(NSString *)oid callback:(RCTResponseSenderBlock)callback;
1818
- (void)results_deleteCarbohydratesSample:(NSString *)oid callback:(RCTResponseSenderBlock)callback;
19+
- (void)results_saveInsulinDeliverySample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
1920

2021
@end

RCTAppleHealthKit/RCTAppleHealthKit+Methods_Results.m

+28
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ - (void)results_getCarbohydratesSamples:(NSDictionary *)input callback:(RCTRespo
111111
}];
112112
}
113113

114+
- (void)results_saveInsulinDeliverySample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
115+
{
116+
HKQuantityType *insulinDeliveryType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierInsulinDelivery];
117+
118+
HKUnit *unit = [HKUnit internationalUnit];
119+
120+
double value = [RCTAppleHealthKit doubleValueFromOptions:input];
121+
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]];
122+
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:startDate];
123+
NSDictionary *metadata = [RCTAppleHealthKit metadataFromOptions:input withDefault:nil];
124+
125+
HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value];
126+
HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:insulinDeliveryType
127+
quantity:quantity
128+
startDate:startDate
129+
endDate:endDate
130+
metadata:metadata];
131+
132+
[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
133+
if (!success) {
134+
NSLog(@"An error occured while saving the insulin sample %@. The error was: ", error);
135+
callback(@[RCTMakeError(@"An error occured while saving the insulin sample", error, nil)]);
136+
return;
137+
}
138+
callback(@[[NSNull null], [sample.UUID UUIDString]]);
139+
}];
140+
}
141+
114142
- (void)results_saveBloodGlucoseSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
115143
{
116144
HKQuantityType *bloodGlucoseType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose];

RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m

+2
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ - (nullable HKObjectType *)getWritePermFromText:(nonnull NSString*) key {
357357
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
358358
} else if ([@"BloodGlucose" isEqualToString:key]) {
359359
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose];
360+
} else if ([@"InsulinDelivery" isEqualToString:key]) {
361+
return [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierInsulinDelivery];
360362
}
361363

362364
// Sleep

RCTAppleHealthKit/RCTAppleHealthKit.m

+6
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,12 @@ + (BOOL)requiresMainQueueSetup
478478
[self results_getInsulinDeliverySamples:input callback:callback];
479479
}
480480

481+
RCT_EXPORT_METHOD(saveInsulinDeliverySample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
482+
{
483+
[self _initializeHealthStore];
484+
[self results_saveInsulinDeliverySample:input callback:callback];
485+
}
486+
481487
RCT_EXPORT_METHOD(saveCarbohydratesSample:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
482488
{
483489
[self _initializeHealthStore];

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ declare module 'react-native-health' {
442442
callback: (err: string, results: Array<HealthValue>) => void,
443443
): void
444444

445+
saveInsulinDeliverySample(
446+
options: HealthValueOptions,
447+
callback: (err: string, results: HealthValue) => void,
448+
): void
449+
445450

446451
Constants: Constants
447452
}
@@ -470,6 +475,7 @@ declare module 'react-native-health' {
470475

471476
export interface RecordMetadata {
472477
HKBloodGlucoseMealTime?: BloodGlucoseMealTime
478+
HKInsulinDeliveryReason?: InsulinDeliveryReason
473479
HKWasUserEntered?: boolean
474480
[key: string]: string | number | boolean | undefined
475481
}

0 commit comments

Comments
 (0)