diff --git a/Constants/Permissions.js b/Constants/Permissions.js index d54f0b85..59be9b5b 100644 --- a/Constants/Permissions.js +++ b/Constants/Permissions.js @@ -4,69 +4,73 @@ * @type {Object} */ export const Permissions = { - ActiveEnergyBurned: "ActiveEnergyBurned", - AppleExerciseTime: "AppleExerciseTime", - BasalEnergyBurned: "BasalEnergyBurned", - BiologicalSex: "BiologicalSex", - BloodGlucose: "BloodGlucose", - BloodPressureDiastolic: "BloodPressureDiastolic", - BloodPressureSystolic: "BloodPressureSystolic", - BodyFatPercentage: "BodyFatPercentage", - BodyMass: "BodyMass", - BodyMassIndex: "BodyMassIndex", - BodyTemperature: "BodyTemperature", - DateOfBirth: "DateOfBirth", - Biotin: "Biotin", - Caffeine: "Caffeine", - Calcium: "Calcium", - Carbohydrates: "Carbohydrates", - Chloride: "Chloride", - Cholesterol: "Cholesterol", - Copper: "Copper", - EnergyConsumed: "EnergyConsumed", - FatMonounsaturated: "FatMonounsaturated", - FatPolyunsaturated: "FatPolyunsaturated", - FatSaturated: "FatSaturated", - FatTotal: "FatTotal", - Fiber: "Fiber", - Folate: "Folate", - Iodine: "Iodine", - Iron: "Iron", - Magnesium: "Magnesium", - Manganese: "Manganese", - Molybdenum: "Molybdenum", - Niacin: "Niacin", - PantothenicAcid: "PantothenicAcid", - Phosphorus: "Phosphorus", - Potassium: "Potassium", - Protein: "Protein", - Riboflavin: "Riboflavin", - Selenium: "Selenium", - Sodium: "Sodium", - Sugar: "Sugar", - Thiamin: "Thiamin", - VitaminA: "VitaminA", - VitaminB12: "VitaminB12", - VitaminB6: "VitaminB6", - VitaminC: "VitaminC", - VitaminD: "VitaminD", - VitaminE: "VitaminE", - VitaminK: "VitaminK", - Zinc: "Zinc", - Water: "Water", - DistanceCycling: "DistanceCycling", - DistanceSwimming: "DistanceSwimming", - DistanceWalkingRunning: "DistanceWalkingRunning", - FlightsClimbed: "FlightsClimbed", - HeartRate: "HeartRate", - Height: "Height", - LeanBodyMass: "LeanBodyMass", - MindfulSession: "MindfulSession", - NikeFuel: "NikeFuel", - RespiratoryRate: "RespiratoryRate", - SleepAnalysis: "SleepAnalysis", - StepCount: "StepCount", - Steps: "Steps", - Weight: "Weight", - Workout: "Workout" -} + ActiveEnergyBurned: 'ActiveEnergyBurned', + AppleExerciseTime: 'AppleExerciseTime', + BasalBodyTemperature: 'BasalBodyTemperature', + BasalEnergyBurned: 'BasalEnergyBurned', + BiologicalSex: 'BiologicalSex', + Biotin: 'Biotin', + BloodGlucose: 'BloodGlucose', + BloodPressureDiastolic: 'BloodPressureDiastolic', + BloodPressureSystolic: 'BloodPressureSystolic', + BodyFatPercentage: 'BodyFatPercentage', + BodyMass: 'BodyMass', + BodyMassIndex: 'BodyMassIndex', + BodyTemperature: 'BodyTemperature', + Caffeine: 'Caffeine', + Calcium: 'Calcium', + Carbohydrates: 'Carbohydrates', + Chloride: 'Chloride', + Cholesterol: 'Cholesterol', + Copper: 'Copper', + DateOfBirth: 'DateOfBirth', + DistanceCycling: 'DistanceCycling', + DistanceSwimming: 'DistanceSwimming', + DistanceWalkingRunning: 'DistanceWalkingRunning', + EnergyConsumed: 'EnergyConsumed', + FatMonounsaturated: 'FatMonounsaturated', + FatPolyunsaturated: 'FatPolyunsaturated', + FatSaturated: 'FatSaturated', + FatTotal: 'FatTotal', + Fiber: 'Fiber', + FlightsClimbed: 'FlightsClimbed', + Folate: 'Folate', + HeartRate: 'HeartRate', + Height: 'Height', + Iodine: 'Iodine', + Iron: 'Iron', + LeanBodyMass: 'LeanBodyMass', + Magnesium: 'Magnesium', + Manganese: 'Manganese', + MenstruationFlow: 'MenstruationFlow', + MindfulSession: 'MindfulSession', + Molybdenum: 'Molybdenum', + Niacin: 'Niacin', + NikeFuel: 'NikeFuel', + OvulationTestResult: 'OvulationTestResult', + PantothenicAcid: 'PantothenicAcid', + Phosphorus: 'Phosphorus', + Potassium: 'Potassium', + Protein: 'Protein', + RespiratoryRate: 'RespiratoryRate', + Riboflavin: 'Riboflavin', + Selenium: 'Selenium', + SexualActivity: 'SexualActivity', + SleepAnalysis: 'SleepAnalysis', + Sodium: 'Sodium', + StepCount: 'StepCount', + Steps: 'Steps', + Sugar: 'Sugar', + Thiamin: 'Thiamin', + VitaminA: 'VitaminA', + VitaminB12: 'VitaminB12', + VitaminB6: 'VitaminB6', + VitaminC: 'VitaminC', + VitaminD: 'VitaminD', + VitaminE: 'VitaminE', + VitaminK: 'VitaminK', + Water: 'Water', + Weight: 'Weight', + Workout: 'Workout', + Zinc: 'Zinc' +}; diff --git a/RCTAppleHealthKit.xcodeproj/project.pbxproj b/RCTAppleHealthKit.xcodeproj/project.pbxproj index ab99383a..efaad6f0 100644 --- a/RCTAppleHealthKit.xcodeproj/project.pbxproj +++ b/RCTAppleHealthKit.xcodeproj/project.pbxproj @@ -20,6 +20,8 @@ 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 */; }; + B790AE8A22E5CCDE00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.m in Sources */ = {isa = PBXBuildFile; fileRef = B790AE8922E5CCDD00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.m */; }; + B79BEDD622E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.m in Sources */ = {isa = PBXBuildFile; fileRef = B79BEDD422E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -61,6 +63,10 @@ 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 = ""; }; 64E0E73E1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Results.m"; sourceTree = ""; }; + B790AE8822E5CCDD00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_ReproductiveHealth.h"; sourceTree = ""; }; + B790AE8922E5CCDD00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_ReproductiveHealth.m"; sourceTree = ""; }; + B79BEDD422E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTAppleHealthKit+Methods_Dietary.m"; sourceTree = ""; }; + B79BEDD522E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTAppleHealthKit+Methods_Dietary.h"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -93,6 +99,10 @@ 3774C88F1D2092F20000B3F3 /* RCTAppleHealthKit */ = { isa = PBXGroup; children = ( + B790AE8822E5CCDD00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.h */, + B790AE8922E5CCDD00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.m */, + B79BEDD522E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.h */, + B79BEDD422E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.m */, 58C81E6E1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.h */, 58C81E6D1F84F6970005DD48 /* RCTAppleHealthKit+Methods_Activity.m */, 61232F911E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.h */, @@ -180,8 +190,10 @@ 3774C89B1D2095450000B3F3 /* RCTAppleHealthKit+Queries.m in Sources */, 3774C8A11D20A6B90000B3F3 /* RCTAppleHealthKit+Utils.m in Sources */, 37837E7D1DCFE270000201A0 /* RCTAppleHealthKit+Methods_Sleep.m in Sources */, + B790AE8A22E5CCDE00F333FF /* RCTAppleHealthKit+Methods_ReproductiveHealth.m in Sources */, 3774C8D41D20C6390000B3F3 /* RCTAppleHealthKit+Methods_Body.m in Sources */, 61232F931E303758000A5026 /* RCTAppleHealthKit+Methods_Mindfulness.m in Sources */, + B79BEDD622E5AAFA008B6A34 /* RCTAppleHealthKit+Methods_Dietary.m in Sources */, 64E0E73F1D37947B00EAB905 /* RCTAppleHealthKit+Methods_Results.m in Sources */, 64C42D4A1D351A8800A0A8F7 /* RCTAppleHealthKit+Methods_Vitals.m in Sources */, 3774C8931D2092F20000B3F3 /* RCTAppleHealthKit.m in Sources */, diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_ReproductiveHealth.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_ReproductiveHealth.h new file mode 100644 index 00000000..30294fe5 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_ReproductiveHealth.h @@ -0,0 +1,10 @@ +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Methods_Vitals) + +- (void)reproductiveHealth_getBasalBodyTemperatureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)reproductiveHealth_getMenstruationFlowSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)reproductiveHealth_getOvulationTestResultSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)reproductiveHealth_getSexualActivitySamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_ReproductiveHealth.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_ReproductiveHealth.m new file mode 100644 index 00000000..c43c4332 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_ReproductiveHealth.m @@ -0,0 +1,130 @@ +#import "RCTAppleHealthKit+Methods_ReproductiveHealth.h" +#import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" + +@implementation RCTAppleHealthKit (Methods_Sleep) + +- (void)reproductiveHealth_getBasalBodyTemperatureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false]; + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit degreeCelsiusUnit]]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + + if (startDate == nil) { + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + + HKQuantityType *sampleType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalBodyTemperature]; + NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; + + [self fetchQuantitySamplesOfType:sampleType + unit:unit + predicate:predicate + ascending:ascending + limit:HKObjectQueryNoLimit + completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting basal body temperature samples: %@", error); + callback(@[RCTMakeError(@"error getting basal body temperature samples", nil, nil)]); + return; + } + }]; + +} + +- (void)reproductiveHealth_getMenstruationFlowSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; + + if (startDate == nil) { + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + + NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; + + [self fetchMenstruationFlowSamples:predicate + ascending:ascending + limit:limit + completion:^(NSArray *results, NSError *error) { + if (results) { + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"Error getting menstruation flow samples: %@", error); + callback(@[RCTMakeError(@"Error getting menstruation flow samples", error, nil)]); + return; + } + }]; + +} + +- (void)reproductiveHealth_getOvulationTestResultSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; + + if (startDate == nil) { + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + + NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; + + [self fetchOvulationTestResultSamples:predicate + ascending:ascending + limit:limit + completion:^(NSArray *results, NSError *error) { + if (results) { + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"Error getting ovulation test result samples: %@", error); + callback(@[RCTMakeError(@"Error getting ovulation test result samples", error, nil)]); + return; + } + }]; + +} + +- (void)reproductiveHealth_getSexualActivitySamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + BOOL ascending = [RCTAppleHealthKit boolFromOptions:input key:@"ascending" withDefault:false]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSUInteger limit = [RCTAppleHealthKit uintFromOptions:input key:@"limit" withDefault:HKObjectQueryNoLimit]; + + if (startDate == nil) { + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + + NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate]; + + [self fetchSexualActivitySamples:predicate + ascending:ascending + limit:limit + completion:^(NSArray *results, NSError *error) { + if (results) { + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"Error getting sexual activity samples: %@", error); + callback(@[RCTMakeError(@"Error getting sexual activity samples", error, nil)]); + return; + } + }]; + +} + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 357aa895..1842610a 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -11,38 +11,49 @@ @interface RCTAppleHealthKit (Queries) -- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error))completion; -- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit completion:(void (^)(double, NSError *))completionHandler; -- (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType unit:(HKUnit *)unit day:(NSDate *)day completion:(void (^)(double, NSDate *, NSDate *, NSError *))completionHandler; +- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType + predicate:(NSPredicate *)predicate + completion:(void (^)(HKQuantity *mostRecentQuantity, NSDate *startDate, NSDate *endDate, NSError *error))completion; + +- (void)fetchSumOfSamplesTodayForType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + completion:(void (^)(double, NSError *))completionHandler; + +- (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType + unit:(HKUnit *)unit + day:(NSDate *)day + completion:(void (^)(double, NSDate *, NSDate *, NSError *))completionHandler; + - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType unit:(HKUnit *)unit startDate:(NSDate *)startDate endDate:(NSDate *)endDate completion:(void (^)(NSArray *, NSError *))completionHandler; - - (void)fetchSamplesOfType:(HKSampleType *)quantityType - unit:(HKUnit *)unit - predicate:(NSPredicate *)predicate - ascending:(BOOL)asc - limit:(NSUInteger)lim - completion:(void (^)(NSArray *, NSError *))completion; + unit:(HKUnit *)unit + predicate:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion; + - (void)setObserverForType:(HKSampleType *)quantityType unit:(HKUnit *)unit; - - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType unit:(HKUnit *)unit predicate:(NSPredicate *)predicate ascending:(BOOL)asc limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; + - (void)fetchCorrelationSamplesOfType:(HKQuantityType *)quantityType unit:(HKUnit *)unit predicate:(NSPredicate *)predicate ascending:(BOOL)asc limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; + - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType unit:(HKUnit *)unit startDate:(NSDate *)startDate @@ -50,19 +61,34 @@ ascending:(BOOL)asc limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completionHandler; + - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType unit:(HKUnit *)unit - period:(NSUInteger)period + period:(NSUInteger)period startDate:(NSDate *)startDate endDate:(NSDate *)endDate ascending:(BOOL)asc limit:(NSUInteger)lim - includeManuallyAdded:(BOOL)includeManuallyAdded + includeManuallyAdded:(BOOL)includeManuallyAdded completion:(void (^)(NSArray *, NSError *))completionHandler; - - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate - limit:(NSUInteger)lim + limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; +- (void)fetchMenstruationFlowSamples:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion; + +- (void)fetchSexualActivitySamples:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion; + +- (void)fetchOvulationTestResultSamples:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)lim + completion:(void (^)(NSArray *, NSError *))completion; + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 325493bd..428d9e9b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -651,4 +651,169 @@ - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType [self.healthStore executeQuery:query]; } +- (void)fetchMenstruationFlowSamples:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)limit + completion:(void (^)(NSArray *, NSError *))completion { + + void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error) + = ^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { + if (completion) { + completion(nil, error); + } + return; + } + + if (completion) { + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + dispatch_async(dispatch_get_main_queue(), ^{ + for (HKCategorySample *sample in results) { + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + bool isTracked = [[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1; + + NSDictionary *elem = @{ + @"endDate" : endDateString, + @"sourceId" : [[[sample sourceRevision] source] bundleIdentifier], + @"sourceName" : [[[sample sourceRevision] source] name], + @"startDate" : startDateString, + @"tracked" : @(isTracked), + @"value" : @(sample.value), + }; + + [data addObject:elem]; + } + + completion(data, error); + }); + } + }; + + HKSampleType *sampleType = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierMenstrualFlow]; + + NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate + ascending:asc]; + + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:sampleType + predicate:predicate + limit:limit + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; + + [self.healthStore executeQuery:query]; +} + +- (void)fetchOvulationTestResultSamples:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)limit + completion:(void (^)(NSArray *, NSError *))completion { + + void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error) + = ^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { + if (completion) { + completion(nil, error); + } + return; + } + + if (completion) { + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + dispatch_async(dispatch_get_main_queue(), ^{ + for (HKCategorySample *sample in results) { + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + bool isTracked = [[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1; + + NSDictionary *elem = @{ + @"endDate" : endDateString, + @"sourceId" : [[[sample sourceRevision] source] bundleIdentifier], + @"sourceName" : [[[sample sourceRevision] source] name], + @"startDate" : startDateString, + @"tracked" : @(isTracked), + @"value" : @(sample.value), + }; + + [data addObject:elem]; + } + + completion(data, error); + }); + } + }; + + HKSampleType *sampleType = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierOvulationTestResult]; + + NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate + ascending:asc]; + + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:sampleType + predicate:predicate + limit:limit + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; + + [self.healthStore executeQuery:query]; +} + +- (void)fetchSexualActivitySamples:(NSPredicate *)predicate + ascending:(BOOL)asc + limit:(NSUInteger)limit + completion:(void (^)(NSArray *, NSError *))completion { + + void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error) + = ^(HKSampleQuery *query, NSArray *results, NSError *error) { + if (!results) { + if (completion) { + completion(nil, error); + } + return; + } + + if (completion) { + NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; + + dispatch_async(dispatch_get_main_queue(), ^{ + for (HKCategorySample *sample in results) { + NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; + NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; + bool isTracked = [[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1; + int protectionUsed = [[sample metadata][HKMetadataKeySexualActivityProtectionUsed] intValue]; + + NSDictionary *elem = @{ + @"endDate" : endDateString, + @"protectionUsed" : @(protectionUsed), + @"sourceId" : [[[sample sourceRevision] source] bundleIdentifier], + @"sourceName" : [[[sample sourceRevision] source] name], + @"startDate" : startDateString, + @"tracked" : @(isTracked), + @"value" : @(sample.value), + }; + + [data addObject:elem]; + } + + completion(data, error); + }); + } + }; + + HKSampleType *sampleType = [HKCategoryType categoryTypeForIdentifier: HKCategoryTypeIdentifierSexualActivity]; + + NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate + ascending:asc]; + + HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:sampleType + predicate:predicate + limit:limit + sortDescriptors:@[timeSortDescriptor] + resultsHandler:handlerBlock]; + + [self.healthStore executeQuery:query]; +} + + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index a54f03fb..2e235a7e 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -53,6 +53,12 @@ - (NSDictionary *)readPermsDict { @"MindfulSession" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMindfulSession], //workouts @"Workout" : [HKObjectType workoutType], + // Reproductive Health + @"BasalBodyTemperature" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalBodyTemperature], + @"MenstruationFlow" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierMenstrualFlow], + @"OvulationTestResult" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierOvulationTestResult], + @"SexualActivity" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSexualActivity], + }; return readPerms; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index e2b13587..0ef961c8 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -12,13 +12,14 @@ #import "RCTAppleHealthKit+Methods_Activity.h" #import "RCTAppleHealthKit+Methods_Body.h" -#import "RCTAppleHealthKit+Methods_Fitness.h" -#import "RCTAppleHealthKit+Methods_Dietary.h" #import "RCTAppleHealthKit+Methods_Characteristic.h" -#import "RCTAppleHealthKit+Methods_Vitals.h" +#import "RCTAppleHealthKit+Methods_Dietary.h" +#import "RCTAppleHealthKit+Methods_Fitness.h" +#import "RCTAppleHealthKit+Methods_Mindfulness.h" +#import "RCTAppleHealthKit+Methods_ReproductiveHealth.h" #import "RCTAppleHealthKit+Methods_Results.h" #import "RCTAppleHealthKit+Methods_Sleep.h" -#import "RCTAppleHealthKit+Methods_Mindfulness.h" +#import "RCTAppleHealthKit+Methods_Vitals.h" #import #import @@ -245,6 +246,26 @@ @implementation RCTAppleHealthKit [self mindfulness_saveMindfulSession:input callback:callback]; } +RCT_EXPORT_METHOD(getBasalBodyTemperatureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self reproductiveHealth_getBasalBodyTemperatureSamples:input callback:callback]; +} + +RCT_EXPORT_METHOD(getMenstruationFlowSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self reproductiveHealth_getMenstruationFlowSamples:input callback:callback]; +} + +RCT_EXPORT_METHOD(getOvulationTestResultSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self reproductiveHealth_getOvulationTestResultSamples:input callback:callback]; +} + +RCT_EXPORT_METHOD(getSexualActivitySamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self reproductiveHealth_getSexualActivitySamples:input callback:callback]; +} + - (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback { diff --git a/README.md b/README.md index 6ad3ead0..c0b24956 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,12 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => * [setObserver](/docs/setObserver().md) * Read Methods * [getActiveEnergyBurned](/docs/getActiveEnergyBurned().md) + * [getBasalBodyTemperatureSamples](/docs/getBasalBodyTemperatureSamples().md) * [getBasalEnergyBurned](/docs/getBasalEnergyBurned().md) * [getBiologicalSex](/docs/getBiologicalSex().md) * [getBloodGlucoseSamples](/docs/getBloodglucoseSamples().md) * [getBloodPressureSamples](/docs/getBloodPressureSamples().md) + * [getBodyFatPercentageSamples](/docs/getBodyFatPercentageSamples().md) * [getBodyTemperatureSamples](/docs/getBodyTemperatureSamples().md) * [getDailyDistanceCyclingSamples](/docs/getDailyDistanceCyclingSamples().md) * [getDailyDistanceWalkingRunningSamples](/docs/getDailyDistanceWalkingRunningSamples().md) @@ -130,16 +132,18 @@ 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) + * [getLeanBodyMassSamples](/docs/getLeanBodyMassSamples().md) + * [getMenstruationFlowSamples](/docs/getMenstruationFlowSamples().md) + * [getOvulationTestResultSamples](/docs/getOvulationTestResultSamples().md) * [getRespiratoryRateSamples](/docs/getRespiratoryRateSamples().md) + * [getSamples](docs/getSamples().md) + * [getSexualActivitySamples](/docs/getSexualActivitySamples().md) * [getSleepSamples](/docs/getSleepSamples().md) * [getStepCount](/docs/getStepCount().md) * [getWeightSamples](/docs/getWeightSamples().md) - * [getSamples](docs/getSamples().md) * Write Methods * [saveBmi](/docs/saveBmi().md) * [saveHeight](/docs/saveHeight().md) @@ -156,12 +160,14 @@ The available Healthkit permissions to use with `initHealthKit` | Permission | Healthkit Identifier Type | Read | Write | |------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-------| -| ActiveEnergyBurned | [HKQuantityTypeIdentifierActiveEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615771-activeenergyburned?language=objc) | ✓ | | -| BasalEnergyBurned | [HKQuantityTypeIdentifierBasalEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615512-basalenergyburned?language=objc) | ✓ | | +| ActiveEnergyBurned | [HKQuantityTypeIdentifierActiveEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615771-activeenergyburned?language=objc)| ✓ | | +| BasalBodyTemperature | [HKQuantityTypeIdentifierBasalBodyTemperature](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifierbasalbodytemperature?language=objc) | ✓ | | +| BasalEnergyBurned | [HKQuantityTypeIdentifierBasalEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615512-basalenergyburned?language=objc) | ✓ | | | BiologicalSex | [HKCharacteristicTypeIdentifierBiologicalSex](https://developer.apple.com/reference/Healthkit/hkcharacteristictypeidentifierbiologicalsex?language=objc) | ✓ | | | BloodGlucose | [HKQuantityTypeIdentifierBloodGlucose](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbloodglucose?language=objc) | ✓ | | | BloodPressureDiastolic | [HKQuantityTypeIdentifierBloodPressureDiastolic](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifierbloodpressurediastolic?language=objc) | ✓ | ✓ | | BloodPressureSystolic | [HKQuantityTypeIdentifierBloodPressureSystolic](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifierbloodpressuresystolic?language=objc) | ✓ | ✓ | +| BodyFatPercentage | [HKQuantityTypeIdentifierBodyFatPercentage](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodyfatpercentage?language=objc) | ✓ | ✓ | | BodyMassIndex | [HKQuantityTypeIdentifierBodyMassIndex](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodymassindex?language=objc) | ✓ | ✓ | | BodyTemperature | [HKQuantityTypeIdentifierBodyTemperature](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierbodytemperature?language=objc) | ✓ | | | DateOfBirth | [HKCharacteristicTypeIdentifierDateOfBirth](https://developer.apple.com/reference/Healthkit/hkcharacteristictypeidentifierdateofbirth?language=objc) | ✓ | | @@ -171,13 +177,15 @@ The available Healthkit permissions to use with `initHealthKit` | HeartRate | [HKQuantityTypeIdentifierHeartRate](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierheartrate?language=objc) | ✓ | | | Height | [HKQuantityTypeIdentifierHeight](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierheight?language=objc) | ✓ | ✓ | | LeanBodyMass | [HKQuantityTypeIdentifierLeanBodyMass](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierleanbodymass?language=objc) | ✓ | ✓ | +| MenstruationFlow | [HKCategoryTypeIdentifierMenstrualFlow](https://developer.apple.com/documentation/healthkit/hkcategorytypeidentifiermenstrualflow?language=occ) | ✓ | | | MindfulSession | [HKCategoryTypeIdentifierMindfulSession](https://developer.apple.com/reference/healthkit/hkcategorytypeidentifiermindfulsession?language=objc) | | ✓ | +| OvulationTestResult | [HKCategoryTypeIdentifierOvulationTestResult](https://developer.apple.com/documentation/healthkit/hkcategorytypeidentifierovulationtestresult?language=occ) | ✓ | | | RespiratoryRate | [HKQuantityTypeIdentifierRespiratoryRate](https://developer.apple.com/reference/Healthkit/hkquantitytypeidentifierrespiratoryrate?language=objc) | ✓ | | +| SexualActivity | [HKCategoryTypeIdentifierSexualActivity](https://developer.apple.com/documentation/healthkit/hkcategorytypeidentifiersexualactivity?language=occ) | ✓ | | | SleepAnalysis | [HKCategoryTypeIdentifierSleepAnalysis](https://developer.apple.com/reference/Healthkit/hkcategorytypeidentifiersleepanalysis?language=objc) | ✓ | | | 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. diff --git a/docs/getBasalBodyTemperatureSamples().md b/docs/getBasalBodyTemperatureSamples().md new file mode 100644 index 00000000..a95ecb0e --- /dev/null +++ b/docs/getBasalBodyTemperatureSamples().md @@ -0,0 +1,17 @@ +```javascript +let options = { + ascending: true, // optional; default true + endDate: new Date().toISOString(), // optional; default now + startDate: new Date(2019, 06, 21).toISOString(), // required + unit: 'celsius' // optional; default 'celsius' +}; +``` + +```javascript +AppleHealthKit.getBasalBodyTemperatureSamples(options: Object, (error: Object, results: Object) => { + if (error) { + console.error(error.message); + } + console.log(results); +}); +``` diff --git a/docs/getMenstruationFlowSamples().md b/docs/getMenstruationFlowSamples().md new file mode 100644 index 00000000..3e9f139b --- /dev/null +++ b/docs/getMenstruationFlowSamples().md @@ -0,0 +1,16 @@ +```javascript +let options = { + ascending: true, // optional; default true + endDate: new Date().toISOString(), // optional; default now + startDate: new Date(2019, 06, 21).toISOString() // required +}; +``` + +```javascript +AppleHealthKit.getMenstruationFlowSamples(options: Object, (error: Object, results: Object) => { + if (error) { + console.error(error.message); + } + console.log(results); +}); +``` diff --git a/docs/getOvulationTestResultSamples().md b/docs/getOvulationTestResultSamples().md new file mode 100644 index 00000000..980e5b22 --- /dev/null +++ b/docs/getOvulationTestResultSamples().md @@ -0,0 +1,16 @@ +```javascript +let options = { + ascending: true, // optional; default true + endDate: new Date().toISOString(), // optional; default now + startDate: new Date(2019, 06, 21).toISOString() // required +}; +``` + +```javascript +AppleHealthKit.getOvulationTestResultSamples(options: Object, (error: Object, results: Object) => { + if (error) { + console.error(error.message); + } + console.log(results); +}); +``` diff --git a/docs/getSexualActivitySamples().md b/docs/getSexualActivitySamples().md new file mode 100644 index 00000000..0bb8d7d5 --- /dev/null +++ b/docs/getSexualActivitySamples().md @@ -0,0 +1,16 @@ +```javascript +let options = { + ascending: true, // optional; default true + endDate: new Date().toISOString(), // optional; default now + startDate: new Date(2019, 06, 21).toISOString() // required +}; +``` + +```javascript +AppleHealthKit.getSexualActivitySamples(options: Object, (error: Object, results: Object) => { + if (error) { + console.error(error.message); + } + console.log(results); +}); +```