Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
* upstream/master: (55 commits)
  removing checkpermission
  Update getBloodGlucoseSamples().md
  feat: add source id and name to fetchQuantitySamplesOfType func
  add docs for getBodyFatPercentageSamples and getLeanBodyMassSamples
  update docs according actual implementation
  docs
  adding a flag includeManuallyAdded
  add getLeanBodyMassSamples and getBodyFatPercentageSamples
  add saveLeanBodyMass
  add saveBodyFatPercentage
  Update README.md
  0.6.5
  Fix doc example
  Add documentation for authorizationStatusForType
  remove checkPermission functions in order to use from PR lucaspbordignon#69
  change back default to imperial system, add activity name field, fix docs
  fix typo
  update docs, change one of the returning key from getSamples
  adding period for all
  adding swimming persmission and method
  ...
  • Loading branch information
rfader committed May 29, 2019
2 parents a43cc7a + c97e721 commit 3ca8eb0
Show file tree
Hide file tree
Showing 31 changed files with 992 additions and 159 deletions.
4 changes: 3 additions & 1 deletion Constants/Permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const Permissions = {
Zinc: "Zinc",
Water: "Water",
DistanceCycling: "DistanceCycling",
DistanceSwimming: "DistanceSwimming",
DistanceWalkingRunning: "DistanceWalkingRunning",
FlightsClimbed: "FlightsClimbed",
HeartRate: "HeartRate",
Expand All @@ -66,5 +67,6 @@ export const Permissions = {
SleepAnalysis: "SleepAnalysis",
StepCount: "StepCount",
Steps: "Steps",
Weight: "Weight"
Weight: "Weight",
Workout: "Workout"
}
4 changes: 3 additions & 1 deletion RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// RCTAppleHealthKit
//
// Created by Alexander Vallorosi on 4/27/17.
// Copyright © 2017 Alexander Vallorosi. All rights reserved.
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//

#import "RCTAppleHealthKit.h"

@interface RCTAppleHealthKit (Methods_Activity)
Expand Down
10 changes: 4 additions & 6 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// RCTAppleHealthKit
//
// Created by Alexander Vallorosi on 4/27/17.
// Copyright © 2017 Alexander Vallorosi. All rights reserved.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.

#import "RCTAppleHealthKit+Methods_Activity.h"
#import "RCTAppleHealthKit+Queries.h"
Expand Down Expand Up @@ -35,8 +35,7 @@ - (void)activity_getActiveEnergyBurned:(NSDictionary *)input callback:(RCTRespon
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;
}
}];
Expand Down Expand Up @@ -65,8 +64,7 @@ - (void)activity_getBasalEnergyBurned:(NSDictionary *)input callback:(RCTRespons
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;
}
}];
Expand Down
5 changes: 5 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
157 changes: 123 additions & 34 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@ - (void)body_getLatestWeight:(NSDictionary *)input callback:(RCTResponseSenderBl
{
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];

HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(unit == nil){
unit = [HKUnit poundUnit];
}

HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]];

[self fetchMostRecentQuantitySampleOfType:weightType
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest weight: %@", error);
callback(@[RCTMakeError(@"error getting latest weight", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
// Determine the weight in the required unit.
double usersWeight = [mostRecentQuantity doubleValueForUnit:unit];

NSDictionary *response = @{
@"value" : @(usersWeight),
@"startDate" : [RCTAppleHealthKit buildISO8601StringFromDate:startDate],
Expand Down Expand Up @@ -71,8 +66,7 @@ - (void)body_getWeightSamples:(NSDictionary *)input callback:(RCTResponseSenderB
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting weight samples: %@", error);
callback(@[RCTMakeError(@"error getting weight samples", nil, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
Expand All @@ -91,8 +85,7 @@ - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)c

[self.healthStore saveObject:weightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving the weight sample: %@", error);
callback(@[RCTMakeError(@"error saving the weight sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(weight)]);
Expand All @@ -108,8 +101,7 @@ - (void)body_getLatestBodyMassIndex:(NSDictionary *)input callback:(RCTResponseS
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest BMI: %@", error);
callback(@[RCTMakeError(@"error getting latest BMI", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
// Determine the bmi in the required unit.
Expand Down Expand Up @@ -140,8 +132,7 @@ - (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSender

[self.healthStore saveObject:bmiSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving BMI sample: %@.", error);
callback(@[RCTMakeError(@"error saving BMI sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(bmi)]);
Expand All @@ -152,11 +143,7 @@ - (void)body_saveBodyMassIndex:(NSDictionary *)input callback:(RCTResponseSender
- (void)body_getLatestHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback
{
HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];

HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(unit == nil){
unit = [HKUnit inchUnit];
}
HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit inchUnit]];;

[self fetchMostRecentQuantitySampleOfType:heightType
predicate:nil
Expand Down Expand Up @@ -206,8 +193,7 @@ - (void)body_getHeightSamples:(NSDictionary *)input callback:(RCTResponseSenderB
callback(@[[NSNull null], results]);
return;
} else {
NSLog(@"error getting height samples: %@", error);
callback(@[RCTMakeError(@"error getting height samples", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
}];
Expand All @@ -218,20 +204,15 @@ - (void)body_saveHeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)c
{
double height = [RCTAppleHealthKit doubleValueFromOptions:input];
NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input];

HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input];
if(heightUnit == nil){
heightUnit = [HKUnit inchUnit];
}
HKUnit *heightUnit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit inchUnit]];

HKQuantity *heightQuantity = [HKQuantity quantityWithUnit:heightUnit doubleValue:height];
HKQuantityType *heightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
HKQuantitySample *heightSample = [HKQuantitySample quantitySampleWithType:heightType quantity:heightQuantity startDate:sampleDate endDate:sampleDate];

[self.healthStore saveObject:heightSample withCompletion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"error saving height sample: %@", error);
callback(@[RCTMakeError(@"error saving height sample", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}
callback(@[[NSNull null], @(height)]);
Expand All @@ -247,8 +228,7 @@ - (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTRespo
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest body fat percentage: %@", error);
callback(@[RCTMakeError(@"error getting latest body fat percentage", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
// Determine the weight in the required unit.
Expand All @@ -269,6 +249,62 @@ - (void)body_getLatestBodyFatPercentage:(NSDictionary *)input callback:(RCTRespo
}


- (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
{
HKQuantityType *leanBodyMassType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass];
Expand All @@ -277,8 +313,7 @@ - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSe
predicate:nil
completion:^(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"error getting latest lean body mass: %@", error);
callback(@[RCTMakeError(@"error getting latest lean body mass", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
}
else {
HKUnit *weightUnit = [HKUnit poundUnit];
Expand All @@ -295,4 +330,58 @@ - (void)body_getLatestLeanBodyMass:(NSDictionary *)input callback:(RCTResponseSe
}];
}


- (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
6 changes: 2 additions & 4 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Characteristic.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ - (void)characteristic_getBiologicalSex:(NSDictionary *)input callback:(RCTRespo
}

if(value == nil){
NSLog(@"error getting biological sex: %@", error);
callback(@[RCTMakeError(@"error getting biological sex", error, nil)]);
callback(@[RCTJSErrorFromNSError(error)]);
return;
}

Expand All @@ -52,8 +51,7 @@ - (void)characteristic_getDateOfBirth:(NSDictionary *)input callback:(RCTRespons
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) {
Expand Down
3 changes: 3 additions & 0 deletions RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
- (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getHourlyStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_setObserver:(NSDictionary *)input;
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceSwimmingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
Expand Down
Loading

0 comments on commit 3ca8eb0

Please sign in to comment.