From 4dd11361cc64e656ac5d0238340a792e40fecedf Mon Sep 17 00:00:00 2001 From: skleest Date: Mon, 16 Jan 2017 15:05:11 -0800 Subject: [PATCH 1/8] Take out AppleExerciseTime for <9.3 crash --- RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index e050dab..5ef1c24 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -34,7 +34,7 @@ - (NSDictionary *)readPermsDict { @"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], @"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed], @"NikeFuel" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierNikeFuel], - @"AppleExerciseTime" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierAppleExerciseTime], +// @"AppleExerciseTime" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierAppleExerciseTime], // Nutrition Identifiers @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], // Vital Signs Identifiers From aa94f612352d9001ddc1df93462a1f5fd4c25a08 Mon Sep 17 00:00:00 2001 From: skleest Date: Wed, 18 Jan 2017 15:28:05 -0800 Subject: [PATCH 2/8] Save mindfulness session --- .../RCTAppleHealthKit+Methods_Mindfulness.h | 13 +++++ .../RCTAppleHealthKit+Methods_Mindfulness.m | 47 +++++++++++++++++++ .../RCTAppleHealthKit+TypesAndPermissions.m | 4 ++ RCTAppleHealthKit/RCTAppleHealthKit.m | 6 +++ constants.js | 1 + 5 files changed, 71 insertions(+) create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h create mode 100644 RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h new file mode 100644 index 0000000..ac2f17c --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.h @@ -0,0 +1,13 @@ +// +// RCTAppleHealthKit+Methods_Mindfulness.h +// RCTAppleHealthKit +// +// + +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Methods_Mindfulness) + +- (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m new file mode 100644 index 0000000..8a8a67c --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m @@ -0,0 +1,47 @@ +// +// RCTAppleHealthKit+Methods_Mindfulness.m +// RCTAppleHealthKit +// +// + + +#import "RCTAppleHealthKit+Methods_Mindfulness.h" +#import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" + +@implementation RCTAppleHealthKit (Methods_Sleep) + + +- (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + + if(startDate == nil || endDate == nil){ + callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]); + return; + } + + // original + HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value]; + HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + HKQuantitySample *sample = [HKQuantitySample categorySampleWithType:type quantity:quantity startDate:startDate endDate:endDate]; + + // new + HKCategoryType *type = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierMindfulSession]; + HKCategorySample *sample = [HKCategorySample categorySampleWithType:type value:nil startDate:startDate endDate:endDate]; + + + [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 step count sample", error, nil)]); + return; + } + callback(@[[NSNull null], @(value)]); + }]; + +} + + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index 5ef1c24..b22e6f3 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -47,6 +47,8 @@ - (NSDictionary *)readPermsDict { @"BloodGlucose" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose], // Sleep @"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis], + // Mindfulness + @"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession], }; return readPerms; } @@ -73,6 +75,8 @@ - (NSDictionary *)writePermsDict { @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], // Sleep @"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis], + // Mindfulness + @"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession], }; return writePerms; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 3a389ce..dbe7645 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -15,6 +15,7 @@ #import "RCTAppleHealthKit+Methods_Vitals.h" #import "RCTAppleHealthKit+Methods_Results.h" #import "RCTAppleHealthKit+Methods_Sleep.h" +#import "RCTAppleHealthKit+Methods_Mindfulness.h" #import #import @@ -164,6 +165,11 @@ @implementation RCTAppleHealthKit [self getModuleInfo:input callback:callback]; } +RCT_EXPORT_METHOD(saveMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self mindfulness_saveMindfulSession:input callback:callback]; +} + - (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback { diff --git a/constants.js b/constants.js index 34427d0..6e345c5 100644 --- a/constants.js +++ b/constants.js @@ -26,6 +26,7 @@ const PERMISSIONS = { RespiratoryRate: 'RespiratoryRate', BloodGlucose: 'BloodGlucose', SleepAnalysis: 'SleepAnalysis', + MindfulSession: 'MindfulSession', }; From c888e03f765b7c2623b88fa3f5990ce82597ab3e Mon Sep 17 00:00:00 2001 From: skleest Date: Wed, 18 Jan 2017 15:48:35 -0800 Subject: [PATCH 3/8] typos --- .../RCTAppleHealthKit+Methods_Mindfulness.m | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m index 8a8a67c..3fe42f0 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m @@ -14,6 +14,7 @@ @implementation RCTAppleHealthKit (Methods_Sleep) - (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { + double value = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:(double)0]; NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; @@ -22,14 +23,8 @@ - (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTRespon return; } - // original - HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value]; - HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; - HKQuantitySample *sample = [HKQuantitySample categorySampleWithType:type quantity:quantity startDate:startDate endDate:endDate]; - - // new HKCategoryType *type = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierMindfulSession]; - HKCategorySample *sample = [HKCategorySample categorySampleWithType:type value:nil startDate:startDate endDate:endDate]; + HKCategorySample *sample = [HKCategorySample categorySampleWithType:type value:value startDate:startDate endDate:endDate]; [self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) { From 0773671209c663af48e4e732a9d94a2017e76d3f Mon Sep 17 00:00:00 2001 From: skleest Date: Thu, 19 Jan 2017 20:18:24 -0800 Subject: [PATCH 4/8] error console fix --- RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m index 3fe42f0..b14ef81 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Mindfulness.m @@ -30,7 +30,7 @@ - (void)mindfulness_saveMindfulSession:(NSDictionary *)input callback:(RCTRespon [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 step count sample", error, nil)]); + callback(@[RCTMakeError(@"An error occured saving the mindful session sample", error, nil)]); return; } callback(@[[NSNull null], @(value)]); From 0a887c1bab9fc45d4e7ce3833d383968fe1d22e6 Mon Sep 17 00:00:00 2001 From: skleest Date: Thu, 30 Mar 2017 14:03:57 -0700 Subject: [PATCH 5/8] save mindfulness bugfix --- RCTAppleHealthKit.xcodeproj/project.pbxproj | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/RCTAppleHealthKit.xcodeproj/project.pbxproj b/RCTAppleHealthKit.xcodeproj/project.pbxproj index 247f7ef..0f9d512 100644 --- a/RCTAppleHealthKit.xcodeproj/project.pbxproj +++ b/RCTAppleHealthKit.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 3774C8D71D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m in Sources */ = {isa = PBXBuildFile; fileRef = 3774C8D61D20C65F0000B3F3 /* RCTAppleHealthKit+Methods_Fitness.m */; }; 377D44F31D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m in Sources */ = {isa = PBXBuildFile; fileRef = 377D44F21D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m */; }; 37837E7D1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m in Sources */ = {isa = PBXBuildFile; fileRef = 37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */; }; + 61232F931E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m in Sources */ = {isa = PBXBuildFile; fileRef = 61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */; }; 64C42D4A1D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m in Sources */ = {isa = PBXBuildFile; fileRef = 64C42D491D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m */; }; 64E0E73F1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m in Sources */ = {isa = PBXBuildFile; fileRef = 64E0E73E1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m */; }; /* End PBXBuildFile section */ @@ -51,6 +52,8 @@ 377D44F21D247D0A004E35CB /* RCTAppleHealthKit+Methods_Characteristic.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Characteristic.m"; sourceTree = ""; }; 37837E7B1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Sleep.h"; sourceTree = ""; }; 37837E7C1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Sleep.m"; sourceTree = ""; }; + 61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Mindfulness.h"; sourceTree = ""; }; + 61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Mindfulness.m"; sourceTree = ""; }; 64C42D481D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Vitals.h"; sourceTree = ""; }; 64C42D491D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Vitals.m"; sourceTree = ""; }; 64E0E73D1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Results.h"; sourceTree = ""; }; @@ -87,6 +90,8 @@ 3774C88F1D2092F20000B3F3 /* RCTAppleHealthKit */ = { isa = PBXGroup; children = ( + 61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */, + 61232F921E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m */, 64E0E73D1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.h */, 64E0E73E1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m */, 64C42D481D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.h */, @@ -171,6 +176,7 @@ 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */, 37837E7D1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m in Sources */, 3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */, + 61232F931E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m in Sources */, 64E0E73F1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m in Sources */, 64C42D4A1D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m in Sources */, 3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */, From 9ce644787cf283354ed40545a776a88e81d09d1a Mon Sep 17 00:00:00 2001 From: skleest Date: Thu, 30 Mar 2017 14:11:35 -0700 Subject: [PATCH 6/8] update readme --- README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de26adf..f0d18b5 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ A React Native bridge module for interacting with [Apple HealthKit] data. * [getRespiratoryRateSamples](#getrespiratoryratesamples) * [getBloodGlucoseSamples](#getbloodglucosesamples) * [getSleepSamples](#getsleepsamples) + * [saveMindfulSession](#saveMindfulSession) * [Examples](#examples) @@ -152,8 +153,11 @@ The available HealthKit permissions to use with `initHealthKit` | BloodPressureSystolic | [HKQuantityTypeIdentifierBloodPressureSystolic](https://developer.apple.com/reference/healthkit/hkquantitytypeidentifierbloodpressuresystolic?language=objc) | ✓ | | | BloodPressureDiastolic | [HKQuantityTypeIdentifierBloodPressureDiastolic](https://developer.apple.com/reference/healthkit/hkquantitytypeidentifierbloodpressurediastolic?language=objc) | ✓ | | | RespiratoryRate | [HKQuantityTypeIdentifierRespiratoryRate](https://developer.apple.com/reference/healthkit/hkquantitytypeidentifierrespiratoryrate?language=objc) | ✓ | | -| BloodGlucose | [HKQuantityTypeIdentifierBloodGlucose](https://developer.apple.com/reference/healthkit/hkquantitytypeidentifierbloodglucose?language=objc) | ✓ | -| SleepAnalysis | [HKCategoryTypeIdentifierSleepAnalysis](https://developer.apple.com/reference/healthkit/hkcategorytypeidentifiersleepanalysis?language=objc) | ✓ | +| BloodGlucose | [HKQuantityTypeIdentifierBloodGlucose](https://developer.apple.com/reference/healthkit/hkquantitytypeidentifierbloodglucose?language=objc) | ✓ | +| +| SleepAnalysis | [HKCategoryTypeIdentifierSleepAnalysis](https://developer.apple.com/reference/healthkit/hkcategorytypeidentifiersleepanalysis?language=objc) | ✓ | +| MindfulSession | [HKCategoryTypeIdentifierMindfulSession](https://developer.apple.com/reference/healthkit/hkcategorytypeidentifiermindfulsession?language=objc) | | ✓ | + These permissions are exported as constants of the `react-native-apple-healthkit` module. @@ -909,6 +913,20 @@ AppleHealthKit.getSleepSamples(options, (err: Object, samples: Array) => }); ``` +___ + +#### **`saveMindfulSession`** +saveMindfulSession + +each mindfulness sample represents a period of time with a startDate and an endDate. +the options must contain `startDate` and `endDate` + +```javascript +let options = { + startDate: (new Date(2016,10,1)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now +}; +``` ## Examples From 187341ff15285bf0b73eb2420e822cd539694077 Mon Sep 17 00:00:00 2001 From: skleest Date: Thu, 30 Mar 2017 14:13:52 -0700 Subject: [PATCH 7/8] update readme more --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f0d18b5..feb89c8 100644 --- a/README.md +++ b/README.md @@ -916,7 +916,6 @@ AppleHealthKit.getSleepSamples(options, (err: Object, samples: Array) => ___ #### **`saveMindfulSession`** -saveMindfulSession each mindfulness sample represents a period of time with a startDate and an endDate. the options must contain `startDate` and `endDate` @@ -926,6 +925,14 @@ let options = { startDate: (new Date(2016,10,1)).toISOString(), // required endDate: (new Date()).toISOString(), // optional; default now }; + +AppleHealthKit.saveMindfulSession({ + startDate: startDate, + endDate: endDate +}, (err, res) => { + if (err) return console.log(err) + console.log('Mindful session saved') +}); ``` From 4739bfac45cf18d078b2ea79cd45611035377f85 Mon Sep 17 00:00:00 2001 From: skleest Date: Thu, 30 Mar 2017 14:14:41 -0700 Subject: [PATCH 8/8] minor --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index feb89c8..f836141 100644 --- a/README.md +++ b/README.md @@ -926,10 +926,7 @@ let options = { endDate: (new Date()).toISOString(), // optional; default now }; -AppleHealthKit.saveMindfulSession({ - startDate: startDate, - endDate: endDate -}, (err, res) => { +AppleHealthKit.saveMindfulSession(options, (err, res) => { if (err) return console.log(err) console.log('Mindful session saved') });