@@ -46,6 +46,40 @@ - (void)results_getBloodGlucoseSamples:(NSDictionary *)input callback:(RCTRespon
46
46
}];
47
47
}
48
48
49
+ - (void )results_getInsulinDeliverySamples : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
50
+ {
51
+ HKQuantityType *insulinDeliveryType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierInsulinDelivery];
52
+
53
+ HKUnit *unit = [HKUnit internationalUnit ];
54
+
55
+ NSUInteger limit = [RCTAppleHealthKit uintFromOptions: input key: @" limit" withDefault: HKObjectQueryNoLimit];
56
+ BOOL ascending = [RCTAppleHealthKit boolFromOptions: input key: @" ascending" withDefault: false ];
57
+ NSDate *startDate = [RCTAppleHealthKit dateFromOptions: input key: @" startDate" withDefault: nil ];
58
+ NSDate *endDate = [RCTAppleHealthKit dateFromOptions: input key: @" endDate" withDefault: [NSDate date ]];
59
+ if (startDate == nil ){
60
+ callback (@[RCTMakeError (@" startDate is required in options" , nil , nil )]);
61
+ return ;
62
+ }
63
+
64
+ NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates: startDate endDate: endDate];
65
+
66
+ [self fetchQuantitySamplesOfType: insulinDeliveryType
67
+ unit: unit
68
+ predicate: predicate
69
+ ascending: ascending
70
+ limit: limit
71
+ completion: ^(NSArray *results, NSError *error) {
72
+ if (results){
73
+ callback (@[[NSNull null ], results]);
74
+ return ;
75
+ } else {
76
+ NSLog (@" An error occured while retrieving the glucose sample %@ . The error was: " , error);
77
+ callback (@[RCTMakeError (@" An error occured while retrieving the glucose sample" , error, nil )]);
78
+ return ;
79
+ }
80
+ }];
81
+ }
82
+
49
83
- (void )results_getCarbohydratesSamples : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
50
84
{
51
85
HKQuantityType *carbohydratesType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierDietaryCarbohydrates];
@@ -77,6 +111,34 @@ - (void)results_getCarbohydratesSamples:(NSDictionary *)input callback:(RCTRespo
77
111
}];
78
112
}
79
113
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
+
80
142
- (void )results_saveBloodGlucoseSample : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
81
143
{
82
144
HKQuantityType *bloodGlucoseType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierBloodGlucose];
@@ -164,4 +226,27 @@ - (void)results_deleteCarbohydratesSample:(NSString *)oid callback:(RCTResponseS
164
226
}];
165
227
}
166
228
229
+ - (void )results_deleteInsulinDeliverySample : (NSString *)oid callback : (RCTResponseSenderBlock)callback
230
+ {
231
+ HKQuantityType *insulinDeliveryType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierInsulinDelivery];
232
+ NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: oid];
233
+ NSPredicate *uuidPredicate = [HKQuery predicateForObjectWithUUID: uuid];
234
+ [self .healthStore deleteObjectsOfType: insulinDeliveryType predicate: uuidPredicate withCompletion: ^(BOOL success, NSUInteger deletedObjectCount, NSError * _Nullable error) {
235
+ if (!success) {
236
+ NSLog (@" An error occured while deleting the insulin delivery sample %@ . The error was: " , error);
237
+ callback (@[RCTMakeError (@" An error occured while deleting the insulin delivery sample" , error, nil )]);
238
+ return ;
239
+ }
240
+ callback (@[[NSNull null ], @(deletedObjectCount)]);
241
+ }];
242
+ }
243
+
244
+ - (void )results_registerObservers : (RCTBridge *)bridge hasListeners : (bool )hasListeners
245
+ {
246
+ if (@available (iOS 11.0 , *)) {
247
+ HKSampleType* insulinType = [HKObjectType quantityTypeForIdentifier: HKQuantityTypeIdentifierInsulinDelivery];
248
+ [self setObserverForType: insulinType type: @" InsulinDelivery" bridge: bridge hasListeners: hasListeners];
249
+ }
250
+ }
251
+
167
252
@end
0 commit comments