Commit 5b51ff08 authored by Evstropov Evgenii's avatar Evstropov Evgenii Committed by GitHub

Merge pull request #84 from allenan/master

adds get/save-BodyFatPercentage and get/save-LeanBodyMass
parents 581a9828 2d3897f7
...@@ -23,6 +23,11 @@ ...@@ -23,6 +23,11 @@
- (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getBodyFatPercentageSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end @end
...@@ -249,6 +249,62 @@ ...@@ -249,6 +249,62 @@
} }
- (void)body_getBodyFatPercentageSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
HKUnit *unit = [HKUnit percentUnit];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:bodyFatPercentType
unit:unit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting body fat percentage samples: %@", error);
callback(@[RCTMakeError(@"error getting body fat percentage samples", nil, nil)]);
return;
}
}];
}
- (void)body_saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double percentage = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];
HKUnit *unit = [HKUnit percentUnit];
percentage = percentage / 100;
HKQuantity *bodyFatPercentQuantity = [HKQuantity quantityWithUnit:unit doubleValue:percentage];
HKQuantityType *bodyFatPercentType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage];
HKQuantitySample *bodyFatPercentSample = [HKQuantitySample quantitySampleWithType:bodyFatPercentType quantity:bodyFatPercentQuantity startDate:sampleDate endDate:sampleDate];
[self.healthStore saveObject:bodyFatPercentSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving body fat percent sample: %@", error);
callback(@[RCTMakeError(@"error saving body fat percent sample", error, nil)]);
return;
}
callback(@[[NSNull null], @(percentage)]);
}];
}
- (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{ {
HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass]; HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
...@@ -274,4 +330,58 @@ ...@@ -274,4 +330,58 @@
}]; }];
} }
- (void)body_getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];
NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit];
BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false];
NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil];
NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]];
if(startDate == nil){
callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]);
return;
}
NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate];
[self fetchQuantitySamplesOfType:leanBodyMassType
unit:unit
predicate:predicate
ascending:ascending
limit:limit
completion:^(NSArray *results, NSError *error) {
if(results){
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting lean body mass samples: %@", error);
callback(@[RCTMakeError(@"error getting lean body mass samples", nil, nil)]);
return;
}
}];
}
- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
double mass = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]];
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];
HKQuantity *massQuantity = [HKQuantity quantityWithUnit:unit doubleValue:mass];
HKQuantityType *massType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
HKQuantitySample *massSample = [HKQuantitySample quantitySampleWithType:massType quantity:massQuantity startDate:sampleDate endDate:sampleDate];
[self.healthStore saveObject:massSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving lean body mass sample: %@", error);
callback(@[RCTMakeError(@"error saving lean body mass sample", error, nil)]);
return;
}
callback(@[[NSNull null], @(mass)]);
}];
}
@end @end
...@@ -99,11 +99,31 @@ RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCT ...@@ -99,11 +99,31 @@ RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCT
[self body_getLatestBodyFatPercentage:input callback:callback]; [self body_getLatestBodyFatPercentage:input callback:callback];
} }
RCT_EXPORT_METHOD(getBodyFatPercentageSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_getBodyFatPercentageSamples:input callback:callback];
}
RCT_EXPORT_METHOD(saveBodyFatPercentage:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_saveBodyFatPercentage:input callback:callback];
}
RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self body_getLatestLeanBodyMass:input callback:callback]; [self body_getLatestLeanBodyMass:input callback:callback];
} }
RCT_EXPORT_METHOD(getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_getLeanBodyMassSamples:input callback:callback];
}
RCT_EXPORT_METHOD(saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{
[self body_saveLeanBodyMass:input callback:callback];
}
RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getStepCount:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback)
{ {
[self fitness_getStepCountOnDay:input callback:callback]; [self fitness_getStepCountOnDay:input callback:callback];
......
...@@ -129,8 +129,10 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => ...@@ -129,8 +129,10 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) =>
* [getHeightSamples](/docs/getHeightSamples().md) * [getHeightSamples](/docs/getHeightSamples().md)
* [getLatestBmi](/docs/getLatestBmi().md) * [getLatestBmi](/docs/getLatestBmi().md)
* [getLatestBodyFatPercentage](/docs/getLatestBodyFatPercentage().md) * [getLatestBodyFatPercentage](/docs/getLatestBodyFatPercentage().md)
* [getBodyFatPercentageSamples](/docs/getBodyFatPercentageSamples().md)
* [getLatestHeight](/docs/getLatestHeight().md) * [getLatestHeight](/docs/getLatestHeight().md)
* [getLatestLeanBodyMass](/docs/getLatestLeanBodyMass().md) * [getLatestLeanBodyMass](/docs/getLatestLeanBodyMass().md)
* [getLeanBodyMassSamples](/docs/getLeanBodyMassSamples().md)
* [getLatestWeight](/docs/getLatestWeight().md) * [getLatestWeight](/docs/getLatestWeight().md)
* [getRespiratoryRateSamples](/docs/getRespiratoryRateSamples().md) * [getRespiratoryRateSamples](/docs/getRespiratoryRateSamples().md)
* [getSleepSamples](/docs/getSleepSamples().md) * [getSleepSamples](/docs/getSleepSamples().md)
...@@ -143,6 +145,8 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => ...@@ -143,6 +145,8 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) =>
* [saveMindfulSession](/docs/saveMindfulSession().md) * [saveMindfulSession](/docs/saveMindfulSession().md)
* [saveWeight](/docs/saveWeight().md) * [saveWeight](/docs/saveWeight().md)
* [saveSteps](/docs/saveSteps().md) * [saveSteps](/docs/saveSteps().md)
* [saveBodyFatPercentage](/docs/saveBodyFatPercentage().md)
* [saveLeanBodyMass](/docs/saveLeanBodyMass().md)
* [References](#references) * [References](#references)
## Supported Apple Permissions ## Supported Apple Permissions
...@@ -172,6 +176,7 @@ The available Healthkit permissions to use with `initHealthKit` ...@@ -172,6 +176,7 @@ The available Healthkit permissions to use with `initHealthKit`
| StepCount | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc) | ✓ | ✓ | | StepCount | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc) | ✓ | ✓ |
| Steps | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?language=objc) | ✓ | ✓ | | Steps | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?language=objc) | ✓ | ✓ |
| Weight | [HKQuantityTypeIdentifierBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymass?language=objc) | ✓ | ✓ | | Weight | [HKQuantityTypeIdentifierBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymass?language=objc) | ✓ | ✓ |
| BodyFatPercentage | [HKQuantityTypeIdentifierBodyFatPercentage](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodyfatpercentage?language=objc) | ✓ | ✓ |
These permissions are exported as constants of the `rn-apple-healthkit` module. These permissions are exported as constants of the `rn-apple-healthkit` module.
......
Query for body fat percentage samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getBodyFatPercentageSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 16.5, startDate: '2016-07-09T00:00:00.000-0400', endDate: '2016-07-10T00:00:00.000-0400' },
{ value: 16.1, startDate: '2016-07-08T00:00:00.000-0400', endDate: '2016-07-09T00:00:00.000-0400' },
{ value: 15.9, startDate: '2016-07-07T00:00:00.000-0400', endDate: '2016-07-08T00:00:00.000-0400' },
]
```
Query for lean body mass samples. the options object is used to setup a query to retrieve relevant samples.
```javascript
let options = {
unit: 'pound', // optional; default 'pound'
startDate: (new Date(2016,4,27)).toISOString(), // required
endDate: (new Date()).toISOString(), // optional; default now
ascending: false, // optional; default false
limit:10, // optional; default no limit
};
```
```javascript
AppleHealthKit.getLeanBodyMassSamples(options, (err: Object, results: Array<Object>) => {
if (err) {
return;
}
console.log(results)
});
```
```javascript
[
{ value: 160, startDate: '2016-07-09T00:00:00.000-0400', endDate: '2016-07-10T00:00:00.000-0400' },
{ value: 161, startDate: '2016-07-08T00:00:00.000-0400', endDate: '2016-07-09T00:00:00.000-0400' },
{ value: 165, startDate: '2016-07-07T00:00:00.000-0400', endDate: '2016-07-08T00:00:00.000-0400' },
]
```
save a percentage body fat value to Healthkit
`saveBodyFatPercentage` accepts an options object containing a percent value:
```javascript
let options = {
value: 16.7 // 16.7%
}
```
```javascript
AppleHealthKit.saveBodyFatPercentage(options: Object, (err: Object, results: Object) => {
if (err) {
return;
}
// body fat percentage successfully saved
});
```
save a numeric lean body mass value to Healthkit
`saveLeanBodyMass` accepts an options object containing a numeric weight value:
```javascript
let options = {
value: 155.6 // lbs
}
```
```javascript
AppleHealthKit.saveLeanBodyMass(options: Object, (err: Object, results: Object) => {
if (err) {
console.log("error saving lean body mass to Healthkit: ", err);
return;
}
// Done
});
```
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment