@@ -118,6 +118,37 @@ - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseS
118
118
}];
119
119
}
120
120
121
+ - (void )body_getBodyMassIndexSamples : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
122
+ {
123
+ HKQuantityType *bmiType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierBodyMassIndex];
124
+
125
+ HKUnit *countUnit = [HKUnit countUnit ];
126
+ NSUInteger limit = [RCTAppleHealthKit uintFromOptions: input key: @" limit" withDefault: HKObjectQueryNoLimit];
127
+ BOOL ascending = [RCTAppleHealthKit boolFromOptions: input key: @" ascending" withDefault: false ];
128
+ NSDate *startDate = [RCTAppleHealthKit dateFromOptions: input key: @" startDate" withDefault: nil ];
129
+ NSDate *endDate = [RCTAppleHealthKit dateFromOptions: input key: @" endDate" withDefault: [NSDate date ]];
130
+ if (startDate == nil ){
131
+ callback (@[RCTMakeError (@" startDate is required in options" , nil , nil )]);
132
+ return ;
133
+ }
134
+ NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates: startDate endDate: endDate];
135
+
136
+ [self fetchQuantitySamplesOfType: bmiType
137
+ unit: countUnit
138
+ predicate: predicate
139
+ ascending: ascending
140
+ limit: limit
141
+ completion: ^(NSArray *results, NSError *error) {
142
+ if (results){
143
+ callback (@[[NSNull null ], results]);
144
+ return ;
145
+ } else {
146
+ callback (@[RCTJSErrorFromNSError (error)]);
147
+ return ;
148
+ }
149
+ }];
150
+ }
151
+
121
152
122
153
- (void )body_saveBodyMassIndex : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
123
154
{
@@ -297,6 +328,81 @@ - (void)body_saveWaistCircumference:(NSDictionary *)input callback:(RCTResponseS
297
328
}];
298
329
}
299
330
331
+ - (void )body_getLatestPeakFlow : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
332
+ {
333
+ HKQuantityType *peakFlowType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierPeakExpiratoryFlowRate];
334
+ HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions: input key: @" unit" withDefault: [[HKUnit literUnit ] unitDividedByUnit: [HKUnit minuteUnit ]]];
335
+
336
+ [self fetchMostRecentQuantitySampleOfType: peakFlowType
337
+ predicate: nil
338
+ completion: ^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
339
+ if (error) {
340
+ NSLog (@" error getting latest peak flow: %@ " , error);
341
+ callback (@[RCTMakeError (@" error getting latest peak flow" , error, nil )]);
342
+ }
343
+ else {
344
+ // Determine the peak flow rate in the required unit.
345
+ double peakFlow = [mostRecentQuantity doubleValueForUnit: unit];
346
+
347
+ NSDictionary *response = @{
348
+ @" value" : @(peakFlow),
349
+ @" startDate" : [RCTAppleHealthKit buildISO8601StringFromDate: startDate],
350
+ @" endDate" : [RCTAppleHealthKit buildISO8601StringFromDate: endDate],
351
+ };
352
+
353
+ callback (@[[NSNull null ], response]);
354
+ }
355
+ }];
356
+ }
357
+
358
+ - (void )body_getPeakFlowSamples : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
359
+ {
360
+ HKQuantityType *peakFlowType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierPeakExpiratoryFlowRate];
361
+
362
+ HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions: input key: @" unit" withDefault: [[HKUnit literUnit ] unitDividedByUnit: [HKUnit minuteUnit ]]];
363
+
364
+ NSUInteger limit = [RCTAppleHealthKit uintFromOptions: input key: @" limit" withDefault: HKObjectQueryNoLimit];
365
+ BOOL ascending = [RCTAppleHealthKit boolFromOptions: input key: @" ascending" withDefault: false ];
366
+ NSDate *startDate = [RCTAppleHealthKit dateFromOptions: input key: @" startDate" withDefault: nil ];
367
+ NSDate *endDate = [RCTAppleHealthKit dateFromOptions: input key: @" endDate" withDefault: [NSDate date ]];
368
+ if (startDate == nil ){
369
+ callback (@[RCTMakeError (@" startDate is required in options" , nil , nil )]);
370
+ return ;
371
+ }
372
+ NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates: startDate endDate: endDate];
373
+
374
+ [self fetchQuantitySamplesOfType: peakFlowType
375
+ unit: unit
376
+ predicate: predicate
377
+ ascending: ascending
378
+ limit: limit
379
+ completion: ^(NSArray *results, NSError *error) {
380
+ if (results){
381
+ callback (@[[NSNull null ], results]);
382
+ } else {
383
+ callback (@[RCTJSErrorFromNSError (error)]);
384
+ }
385
+ }];
386
+ }
387
+
388
+ - (void )body_savePeakFlow : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
389
+ {
390
+ double peakFlow = [RCTAppleHealthKit doubleValueFromOptions: input];
391
+ NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow: input];
392
+ HKUnit *peakFlowUnit = [RCTAppleHealthKit hkUnitFromOptions: input key: @" unit" withDefault: [[HKUnit literUnit ] unitDividedByUnit: [HKUnit minuteUnit ]]];
393
+
394
+ HKQuantity *peakFlowQuantity = [HKQuantity quantityWithUnit: peakFlowUnit doubleValue: peakFlow];
395
+ HKQuantityType *peakFlowType = [HKQuantityType quantityTypeForIdentifier: HKQuantityTypeIdentifierPeakExpiratoryFlowRate];
396
+ HKQuantitySample *peakFlowSample = [HKQuantitySample quantitySampleWithType: peakFlowType quantity: peakFlowQuantity startDate: sampleDate endDate: sampleDate];
397
+
398
+ [self .healthStore saveObject: peakFlowSample withCompletion: ^(BOOL success, NSError *error) {
399
+ if (!success) {
400
+ callback (@[RCTJSErrorFromNSError (error)]);
401
+ return ;
402
+ }
403
+ callback (@[[NSNull null ], @(peakFlow)]);
404
+ }];
405
+ }
300
406
301
407
- (void )body_getLatestBodyFatPercentage : (NSDictionary *)input callback : (RCTResponseSenderBlock)callback
302
408
{
0 commit comments