From 55f6440049873821233efa465b6bae6f1b7b86da Mon Sep 17 00:00:00 2001 From: skleest Date: Wed, 18 Jan 2017 15:28:05 -0800 Subject: [PATCH] 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 @@ @"BloodGlucose" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose], // Sleep @"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis], + // Mindfulness + @"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession], }; return readPerms; } @@ -73,6 +75,8 @@ @"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 c54a4bc..0249d12 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 "RCTBridge.h" #import "RCTEventDispatcher.h" @@ -164,6 +165,11 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock [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', }; -- 2.26.2