From 44c7c21f876f10f1831a2b29c205d86b2c44bec6 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Thu, 30 Jun 2016 20:01:07 -0400 Subject: [PATCH] adding constants to the package index.js HealthKit export object and updating permissions --- RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 6 +- .../RCTAppleHealthKit+TypesAndPermissions.m | 37 +++++++--- RCTAppleHealthKit/RCTAppleHealthKit+Utils.h | 2 +- RCTAppleHealthKit/RCTAppleHealthKit+Utils.m | 2 +- constants.js | 67 +++++++++++++++++++ .../StepsDemo/app/components/home/index.js | 14 ++-- .../ios/StepsDemo.xcodeproj/project.pbxproj | 24 +++---- index.js | 10 ++- 8 files changed, 128 insertions(+), 34 deletions(-) create mode 100644 constants.js diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 8975c88..d621700 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -260,8 +260,6 @@ NSDate *endDate = result.endDate; double value = [quantity doubleValueForUnit:unit]; -// NSLog(@"%@: %f", date, value); - NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:startDate]; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:endDate]; @@ -271,15 +269,13 @@ @"endDate" : endDateString, }; -// NSArray *elem = @[dateString, @(value)]; - [data addObject:elem]; } }]; // is ascending by default if(asc == false) { - [self reverseNSMutableArray:data]; + [RCTAppleHealthKit reverseNSMutableArray:data]; } if(lim > 0) { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index c259379..f81d229 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -14,34 +14,51 @@ - (NSDictionary *)readPermsDict { NSDictionary *readPerms = @{ - @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], - @"ActiveEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], - + // Characteristic Identifiers + @"DateOfBirth" : [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth], + @"BiologicalSex" : [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex], + // Body Measurements @"Height" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight], @"Weight" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], + @"BodyMass" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], @"BodyFatPercentage" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage], @"BodyMassIndex" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex], @"LeanBodyMass" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass], - + // Fitness Identifiers @"Steps" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount], - @"DateOfBirth" : [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierDateOfBirth], - @"BiologicalSex" : [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex], + @"StepCount" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount], + @"DistanceWalkingRunning" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning], + @"DistanceCycling" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling], + @"BasalEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned], + @"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], + @"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed], + @"NikeFuel" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierNikeFuel], + @"AppleExerciseTime" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierAppleExerciseTime], + // Nutrition Identifiers + @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], }; return readPerms; } - (NSDictionary *)writePermsDict { NSDictionary *writePerms = @{ - @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], - @"ActiveEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], - + // Body Measurements @"Height" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight], @"Weight" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], + @"BodyMass" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass], @"BodyFatPercentage" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage], @"BodyMassIndex" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex], @"LeanBodyMass" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass], - + // Fitness Identifiers @"Steps" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount], + @"StepCount" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount], + @"DistanceWalkingRunning" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning], + @"DistanceCycling" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling], + @"BasalEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned], + @"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], + @"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed], + // Nutrition Identifiers + @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], }; return writePerms; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h index 08fe6e3..f5d33f3 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.h @@ -31,6 +31,6 @@ + (NSString *)stringFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(NSString *)defaultValue; + (bool)boolFromOptions:(NSDictionary *)options key:(NSString *)key withDefault:(bool)defaultValue; -- (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array; ++ (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m index ac9b9f5..dea660f 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Utils.m @@ -280,7 +280,7 @@ -- (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array { ++ (NSMutableArray *)reverseNSMutableArray:(NSMutableArray *)array { if ([array count] <= 1) return array; NSUInteger i = 0; diff --git a/constants.js b/constants.js new file mode 100644 index 0000000..8c7ecbc --- /dev/null +++ b/constants.js @@ -0,0 +1,67 @@ +'use strict' + +const PERMISSIONS = { + READ: { + DateOfBirth: 'DateOfBirth', + BiologicalSex: 'BiologicalSex', + Height: 'Height', + Weight: 'Weight', + BodyMass: 'BodyMass', + BodyFatPercentage: 'BodyFatPercentage', + BodyMassIndex: 'BodyMassIndex', + LeanBodyMass: 'LeanBodyMass', + Steps: 'Steps', + StepCount: 'StepCount', + DistanceWalkingRunning: 'DistanceWalkingRunning', + DistanceCycling: 'DistanceCycling', + BasalEnergyBurned: 'BasalEnergyBurned', + ActiveEnergyBurned: 'ActiveEnergyBurned', + FlightsClimbed: 'FlightsClimbed', + NikeFuel: 'NikeFuel', + AppleExerciseTime: 'AppleExerciseTime', + DietaryEnergy: 'DietaryEnergy', + }, + WRITE: { + Height: 'Height', + Weight: 'Weight', + BodyMass: 'BodyMass', + BodyFatPercentage: 'BodyFatPercentage', + BodyMassIndex: 'BodyMassIndex', + LeanBodyMass: 'LeanBodyMass', + Steps: 'Steps', + StepCount: 'StepCount', + DistanceWalkingRunning: 'DistanceWalkingRunning', + DistanceCycling: 'DistanceCycling', + BasalEnergyBurned: 'BasalEnergyBurned', + ActiveEnergyBurned: 'ActiveEnergyBurned', + FlightsClimbed: 'FlightsClimbed', + DietaryEnergy: 'DietaryEnergy', + } +}; + + +const UNITS = { + gram: 'gram', + pound: 'pound', + meter: 'meter', + inch: 'inch', + foot: 'foot', + second: 'second', + minute: 'minute', + hour: 'hour', + day: 'day', + joule: 'joule', + calorie: 'calorie', + count: 'count', + percent: 'percent' +}; + + + +const CONSTANTS = { + Permissions: PERMISSIONS, + Units: UNITS, +}; + +export default CONSTANTS; +export const Constants = CONSTANTS; diff --git a/examples/StepsDemo/app/components/home/index.js b/examples/StepsDemo/app/components/home/index.js index f6d4799..bc41018 100644 --- a/examples/StepsDemo/app/components/home/index.js +++ b/examples/StepsDemo/app/components/home/index.js @@ -15,16 +15,18 @@ import TimerMixin from 'react-timer-mixin'; var reactMixin = require('react-mixin'); import styles from '../../styles/styles'; -var AppleHealthKit = require('react-native-apple-healthkit'); - +//var AppleHealthKit = require('react-native-apple-healthkit'); +import AppleHealthKit from 'react-native-apple-healthkit'; import History from './history'; +const WPERMS = AppleHealthKit.CONSTANTS.PERMISSIONS.WRITE; +const RPERMS = AppleHealthKit.CONSTANTS.PERMISSIONS.READ; const HKOPTIONS = { permissions: { - read: ['Steps'], - write: ['Steps'], + read: [RPERMS.StepCount], + write: [WPERMS.StepCount], } }; @@ -40,6 +42,10 @@ class Home extends Component { } componentDidMount() { + + console.log('CONSTANTS: ', AppleHealthKit.CONSTANTS); + //console.log('balls: ', ahk); + AppleHealthKit.isAvailable((err,available) => { if(available){ AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => { diff --git a/examples/StepsDemo/ios/StepsDemo.xcodeproj/project.pbxproj b/examples/StepsDemo/ios/StepsDemo.xcodeproj/project.pbxproj index e74d646..b60f5a8 100644 --- a/examples/StepsDemo/ios/StepsDemo.xcodeproj/project.pbxproj +++ b/examples/StepsDemo/ios/StepsDemo.xcodeproj/project.pbxproj @@ -23,7 +23,7 @@ 140ED2AC1D01E1AD002B40FF /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.a */; }; 146834051AC3E58100842450 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.a */; }; 378616B61D257B040027C300 /* HealthKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 378616B51D257B040027C300 /* HealthKit.framework */; }; - 378616C51D259EE50027C300 /* libRCTAppleHealthKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 378616C41D259EE10027C300 /* libRCTAppleHealthKit.a */; }; + 3787CCA91D25E10E0080733E /* libRCTAppleHealthKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3787CCA81D25E10A0080733E /* libRCTAppleHealthKit.a */; }; 832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; }; /* End PBXBuildFile section */ @@ -91,9 +91,9 @@ remoteGlobalIDString = 83CBBA2E1A601D0E00E9B192; remoteInfo = React; }; - 378616C31D259EE10027C300 /* PBXContainerItemProxy */ = { + 3787CCA71D25E10A0080733E /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; - containerPortal = 378616BF1D259EE00027C300 /* RCTAppleHealthKit.xcodeproj */; + containerPortal = 3787CCA31D25E10A0080733E /* RCTAppleHealthKit.xcodeproj */; proxyType = 2; remoteGlobalIDString = 3774C88D1D2092F20000B3F3; remoteInfo = RCTAppleHealthKit; @@ -136,7 +136,7 @@ 146833FF1AC3E56700842450 /* React.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = React.xcodeproj; path = "../node_modules/react-native/React/React.xcodeproj"; sourceTree = ""; }; 378616B51D257B040027C300 /* HealthKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HealthKit.framework; path = System/Library/Frameworks/HealthKit.framework; sourceTree = SDKROOT; }; 378616B71D257B040027C300 /* StepsDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = StepsDemo.entitlements; path = StepsDemo/StepsDemo.entitlements; sourceTree = ""; }; - 378616BF1D259EE00027C300 /* RCTAppleHealthKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAppleHealthKit.xcodeproj; path = "../node_modules/react-native-apple-healthkit/RCTAppleHealthKit.xcodeproj"; sourceTree = ""; }; + 3787CCA31D25E10A0080733E /* RCTAppleHealthKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAppleHealthKit.xcodeproj; path = "../node_modules/react-native-apple-healthkit/RCTAppleHealthKit.xcodeproj"; sourceTree = ""; }; 78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = "../node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj"; sourceTree = ""; }; 832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "../node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -154,7 +154,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 378616C51D259EE50027C300 /* libRCTAppleHealthKit.a in Frameworks */, + 3787CCA91D25E10E0080733E /* libRCTAppleHealthKit.a in Frameworks */, 146834051AC3E58100842450 /* libReact.a in Frameworks */, 00C302E51ABCBA2D00DB3ED1 /* libRCTActionSheet.a in Frameworks */, 378616B61D257B040027C300 /* HealthKit.framework in Frameworks */, @@ -268,10 +268,10 @@ name = Products; sourceTree = ""; }; - 378616C01D259EE00027C300 /* Products */ = { + 3787CCA41D25E10A0080733E /* Products */ = { isa = PBXGroup; children = ( - 378616C41D259EE10027C300 /* libRCTAppleHealthKit.a */, + 3787CCA81D25E10A0080733E /* libRCTAppleHealthKit.a */, ); name = Products; sourceTree = ""; @@ -287,7 +287,7 @@ 832341AE1AAA6A7D00B99B32 /* Libraries */ = { isa = PBXGroup; children = ( - 378616BF1D259EE00027C300 /* RCTAppleHealthKit.xcodeproj */, + 3787CCA31D25E10A0080733E /* RCTAppleHealthKit.xcodeproj */, 146833FF1AC3E56700842450 /* React.xcodeproj */, 00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */, 00C302B51ABCB90400DB3ED1 /* RCTGeolocation.xcodeproj */, @@ -411,8 +411,8 @@ ProjectRef = 00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */; }, { - ProductGroup = 378616C01D259EE00027C300 /* Products */; - ProjectRef = 378616BF1D259EE00027C300 /* RCTAppleHealthKit.xcodeproj */; + ProductGroup = 3787CCA41D25E10A0080733E /* Products */; + ProjectRef = 3787CCA31D25E10A0080733E /* RCTAppleHealthKit.xcodeproj */; }, { ProductGroup = 00C302B61ABCB90400DB3ED1 /* Products */; @@ -516,11 +516,11 @@ remoteRef = 146834031AC3E56700842450 /* PBXContainerItemProxy */; sourceTree = BUILT_PRODUCTS_DIR; }; - 378616C41D259EE10027C300 /* libRCTAppleHealthKit.a */ = { + 3787CCA81D25E10A0080733E /* libRCTAppleHealthKit.a */ = { isa = PBXReferenceProxy; fileType = archive.ar; path = libRCTAppleHealthKit.a; - remoteRef = 378616C31D259EE10027C300 /* PBXContainerItemProxy */; + remoteRef = 3787CCA71D25E10A0080733E /* PBXContainerItemProxy */; sourceTree = BUILT_PRODUCTS_DIR; }; 78C398B91ACF4ADC00677621 /* libRCTLinking.a */ = { diff --git a/index.js b/index.js index bc62780..2e5bc83 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,12 @@ 'use strict' let { AppleHealthKit } = require('react-native').NativeModules; -module.exports = AppleHealthKit; + +import Constants from './constants'; + +let HealthKit = Object.assign({}, AppleHealthKit, { + Constants: Constants +}); + +export default HealthKit +module.exports = HealthKit; -- 2.26.2