Commit ca3d94b9 authored by Furyou81's avatar Furyou81
parents 66d0d625 5b51ff08
......@@ -35,8 +35,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting active energy burned samples: %@", error);
callback(@[RCTMakeError(@"error getting active energy burned samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......@@ -65,8 +64,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting basal energy burned samples: %@", error);
callback(@[RCTMakeError(@"error getting basal energy burned samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -23,6 +23,11 @@
- (void)body_saveHeight:(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_getLeanBodyMassSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)body_saveLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
@end
......@@ -34,8 +34,7 @@
}
if(value == nil){
NSLog(@"error getting biological sex: %@", error);
callback(@[RCTMakeError(@"error getting biological sex", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -52,8 +51,7 @@
NSDate *dob = [self.healthStore dateOfBirthWithError:&error];
if(error != nil){
NSLog(@"error getting date of birth: %@", error);
callback(@[RCTMakeError(@"error getting date of birth", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
if(dob == nil) {
......
......@@ -34,8 +34,7 @@
day:date
completion:^(double value, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!value) {
NSLog(@"could not fetch step count for day: %@", error);
callback(@[RCTMakeError(@"could not fetch step count for day", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -123,8 +122,7 @@
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
......@@ -150,8 +148,7 @@
[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the step count sample %@. The error was: %@.", sample, error);
callback(@[RCTMakeError(@"An error occured saving the step count sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(value)]);
......@@ -173,9 +170,7 @@
NSError *error) {
if (error) {
// Perform Proper Error Handling Here...
NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***", error.localizedDescription);
callback(@[RCTMakeError(@"An error occured while setting up the stepCount observer", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -200,8 +195,7 @@
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!distance) {
NSLog(@"ERROR getting DistanceWalkingRunning: %@", error);
callback(@[RCTMakeError(@"ERROR getting DistanceWalkingRunning", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -276,8 +270,7 @@
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
......@@ -293,8 +286,7 @@
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!distance) {
NSLog(@"ERROR getting DistanceCycling: %@", error);
callback(@[RCTMakeError(@"ERROR getting DistanceCycling", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -334,8 +326,7 @@
includeManuallyAdded:includeManuallyAdded
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
......@@ -351,8 +342,7 @@
[self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double count, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!count) {
NSLog(@"ERROR getting FlightsClimbed: %@", error);
callback(@[RCTMakeError(@"ERROR getting FlightsClimbed", error, nil), @(count)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
......@@ -388,8 +378,7 @@
limit:limit
completion:^(NSArray *arr, NSError *err){
if (err != nil) {
NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err);
callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]);
callback(@[RCTJSErrorFromNSError(err)]);
return;
}
callback(@[[NSNull null], arr]);
......
......@@ -29,8 +29,7 @@
[self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"An error occured saving the mindful session sample %@. The error was: %@.", sample, error);
callback(@[RCTMakeError(@"An error occured saving the mindful session sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(value)]);
......
......@@ -32,8 +32,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting blood glucose samples: %@", error);
callback(@[RCTMakeError(@"error getting blood glucose samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -36,8 +36,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting sleep samples: %@", error);
callback(@[RCTMakeError(@"error getting sleep samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -33,8 +33,7 @@
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting heart rate samples: %@", error);
callback(@[RCTMakeError(@"error getting heart rate samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
......
......@@ -15,5 +15,7 @@
- (NSDictionary *)writePermsDict;
- (NSSet *)getReadPermsFromOptions:(NSArray *)options;
- (NSSet *)getWritePermsFromOptions:(NSArray *)options;
- (HKObjectType *)getWritePermFromString:(NSString *)string;
- (NSString *)getAuthorizationStatusString:(HKAuthorizationStatus)status;
@end
......@@ -153,4 +153,18 @@
return writePermSet;
}
- (HKObjectType *)getWritePermFromString:(NSString *)writePerm {
return [[self writePermsDict] objectForKey:writePerm];
}
- (NSString *)getAuthorizationStatusString:(HKAuthorizationStatus)status {
switch (status) {
case HKAuthorizationStatusNotDetermined:
return @"NotDetermined";
case HKAuthorizationStatusSharingDenied:
return @"SharingDenied";
case HKAuthorizationStatusSharingAuthorized:
return @"SharingAuthorized";
}
}
@end
......@@ -99,11 +99,31 @@ RCT_EXPORT_METHOD(getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCT
[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)
{
[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)
{
[self fitness_getStepCountOnDay:input callback:callback];
......@@ -271,9 +291,7 @@ RCT_EXPORT_METHOD(saveMindfulSession:(NSDictionary *)input callback:(RCTResponse
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
if (!success) {
NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
NSLog(errMsg);
callback(@[RCTMakeError(errMsg, nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
......@@ -286,6 +304,28 @@ RCT_EXPORT_METHOD(saveMindfulSession:(NSDictionary *)input callback:(RCTResponse
}
}
RCT_EXPORT_METHOD(authorizationStatusForType:(NSString *)type
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject
{
if (self.healthStore == nil) {
self.healthStore = [[HKHealthStore alloc] init];
}
if ([HKHealthStore isHealthDataAvailable]) {
HKObjectType *objectType = [self getWritePermFromString:type];
if (objectType == nil) {
reject(@"unknown write permission", nil, nil);
return;
}
NSString *status = [self getAuthorizationStatusString:[self.healthStore authorizationStatusForType:objectType]];
resolve(status);
} else {
reject(@"HealthKit data is not available", nil, nil);
}
})
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
NSDictionary *info = @{
......
......@@ -106,6 +106,7 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) =>
* Base Methods
* [isAvailable](/docs/isAvailable().md)
* [initHealthKit](/docs/initHealthKit().md)
* [authorizationStatusForType](/docs/authorizationStatusForType().md)
* Realtime Methods
* [initStepCountObserver](/docs/initStepCountObserver().md)
* [setObserver](/docs/setObserver().md)
......@@ -129,8 +130,10 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) =>
* [getHeightSamples](/docs/getHeightSamples().md)
* [getLatestBmi](/docs/getLatestBmi().md)
* [getLatestBodyFatPercentage](/docs/getLatestBodyFatPercentage().md)
* [getBodyFatPercentageSamples](/docs/getBodyFatPercentageSamples().md)
* [getLatestHeight](/docs/getLatestHeight().md)
* [getLatestLeanBodyMass](/docs/getLatestLeanBodyMass().md)
* [getLeanBodyMassSamples](/docs/getLeanBodyMassSamples().md)
* [getLatestWeight](/docs/getLatestWeight().md)
* [getRespiratoryRateSamples](/docs/getRespiratoryRateSamples().md)
* [getSleepSamples](/docs/getSleepSamples().md)
......@@ -143,6 +146,8 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) =>
* [saveMindfulSession](/docs/saveMindfulSession().md)
* [saveWeight](/docs/saveWeight().md)
* [saveSteps](/docs/saveSteps().md)
* [saveBodyFatPercentage](/docs/saveBodyFatPercentage().md)
* [saveLeanBodyMass](/docs/saveLeanBodyMass().md)
* [References](#references)
## Supported Apple Permissions
......@@ -172,6 +177,7 @@ The available Healthkit permissions to use with `initHealthKit`
| StepCount | [HKQuantityTypeIdentifierStepCount](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierstepcount?language=objc) | ✓ | ✓ |
| Steps | [HKQuantityTypeIdentifierSteps](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifiersteps?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.
......
Check the authorization status for sharing (writing) the specified data type.
Status will be one of `"NotDetermined"`, `"SharingDenied"`, `"SharingAuthorized"`.
```javascript
try {
const status = await AppleHealthKit.authorizationStatusForType("StepCount")
if (status) {
console.log("status is", status)
}
} catch (error) {
console.warn(error)
}
```
There is no way to check authorization status for read permission, [see this](https://developer.apple.com/documentation/healthkit/hkhealthstore/1614154-authorizationstatusfortype?language=objc).
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