diff --git a/Constants/Permissions.js b/Constants/Permissions.js index 9d44aec53c9d069fe984b0824e45cbf00c820a4f..4d2fdc273e2821d338a08d05673543ae930f71ff 100644 --- a/Constants/Permissions.js +++ b/Constants/Permissions.js @@ -16,7 +16,44 @@ export const Permissions = { BodyMassIndex: "BodyMassIndex", BodyTemperature: "BodyTemperature", DateOfBirth: "DateOfBirth", - DietaryEnergy: "DietaryEnergy", + 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", DistanceWalkingRunning: "DistanceWalkingRunning", FlightsClimbed: "FlightsClimbed", diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h index 52685003704620e12eee8f4147d0bd9789c935b8..235e9c6b3887a22d211c0784dc9fd15a62e39511 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.h @@ -10,5 +10,6 @@ @interface RCTAppleHealthKit (Methods_Activity) - (void)activity_getActiveEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)activity_getBasalEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m index 0d6275146294aee0af0f97955f55c374d40b3785..03b2724c1d5085c90cfbc049994b66f6fa235b56 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Activity.m @@ -42,4 +42,35 @@ }]; } +- (void)activity_getBasalEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKQuantityType *basalEnergyType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned]; + NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; + NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; + HKUnit *cal = [HKUnit kilocalorieUnit]; + + if(startDate == nil){ + callback(@[RCTMakeError(@"startDate is required in options", nil, nil)]); + return; + } + NSPredicate * predicate = [RCTAppleHealthKit predicateForSamplesBetweenDates:startDate endDate:endDate]; + + [self fetchQuantitySamplesOfType:basalEnergyType + unit:cal + predicate:predicate + ascending:false + limit:HKObjectQueryNoLimit + completion:^(NSArray *results, NSError *error) { + if(results){ + callback(@[[NSNull null], results]); + return; + } else { + NSLog(@"error getting basal energy burned samples: %@", error); + callback(@[RCTMakeError(@"error getting basal energy burned samples", nil, nil)]); + return; + } + }]; + +} + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m index 30788ab2256b161313a0f12db181dd9a787d7c5b..7e8b7c6990acabea53f8fc1769138cddcbeb627c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Body.m @@ -80,7 +80,7 @@ - (void)body_saveWeight:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { double weight = [RCTAppleHealthKit doubleValueFromOptions:input]; - NSDate *sampleDate = [RCTAppleHealthKit dateFromOptionsDefaultNow:input]; + NSDate *sampleDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:[NSDate date]]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit poundUnit]]; HKQuantity *weightQuantity = [HKQuantity quantityWithUnit:unit doubleValue:weight]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h new file mode 100644 index 0000000000000000000000000000000000000000..f1477ff6be5331055564e82005068cbca1c0a386 --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.h @@ -0,0 +1,16 @@ +// +// RCTAppleHealthKit+Methods_Dietary.h +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit.h" + +@interface RCTAppleHealthKit (Methods_Dietary) + +- (void)saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m new file mode 100644 index 0000000000000000000000000000000000000000..490e29548705e438b7e6e1685d3742659992cd4f --- /dev/null +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Dietary.m @@ -0,0 +1,409 @@ +// +// RCTAppleHealthKit+Methods_Dietary.m +// RCTAppleHealthKit +// +// Created by Greg Wilson on 2016-06-26. +// Copyright © 2016 Greg Wilson. All rights reserved. +// + +#import "RCTAppleHealthKit+Methods_Dietary.h" +#import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" + +#import +#import + +@implementation RCTAppleHealthKit (Methods_Dietary) + +- (void)saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + NSString *foodNameValue = [RCTAppleHealthKit stringFromOptions:input key:@"foodName" withDefault:nil]; + NSString *mealNameValue = [RCTAppleHealthKit stringFromOptions:input key:@"mealType" withDefault:nil]; + NSDate *timeFoodWasConsumed = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; + double biotinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"biotin" withDefault:(double)0]; + double caffeineValue = [RCTAppleHealthKit doubleFromOptions:input key:@"caffeine" withDefault:(double)0]; + double calciumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"calcium" withDefault:(double)0]; + double carbohydratesValue = [RCTAppleHealthKit doubleFromOptions:input key:@"carbohydrates" withDefault:(double)0]; + double chlorideValue = [RCTAppleHealthKit doubleFromOptions:input key:@"chloride" withDefault:(double)0]; + double cholesterolValue = [RCTAppleHealthKit doubleFromOptions:input key:@"cholesterol" withDefault:(double)0]; + double copperValue = [RCTAppleHealthKit doubleFromOptions:input key:@"copper" withDefault:(double)0]; + double energyConsumedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"energy" withDefault:(double)0]; + double fatMonounsaturatedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatMonounsaturated" withDefault:(double)0]; + double fatPolyunsaturatedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatPolyunsaturated" withDefault:(double)0]; + double fatSaturatedValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatSaturated" withDefault:(double)0]; + double fatTotalValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fatTotal" withDefault:(double)0]; + double fiberValue = [RCTAppleHealthKit doubleFromOptions:input key:@"fiber" withDefault:(double)0]; + double folateValue = [RCTAppleHealthKit doubleFromOptions:input key:@"folate" withDefault:(double)0]; + double iodineValue = [RCTAppleHealthKit doubleFromOptions:input key:@"iodine" withDefault:(double)0]; + double ironValue = [RCTAppleHealthKit doubleFromOptions:input key:@"iron" withDefault:(double)0]; + double magnesiumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"magnesium" withDefault:(double)0]; + double manganeseValue = [RCTAppleHealthKit doubleFromOptions:input key:@"manganese" withDefault:(double)0]; + double molybdenumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"molybdenum" withDefault:(double)0]; + double niacinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"niacin" withDefault:(double)0]; + double pantothenicAcidValue = [RCTAppleHealthKit doubleFromOptions:input key:@"pantothenicAcid" withDefault:(double)0]; + double phosphorusValue = [RCTAppleHealthKit doubleFromOptions:input key:@"phosphorus" withDefault:(double)0]; + double potassiumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"potassium" withDefault:(double)0]; + double proteinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"protein" withDefault:(double)0]; + double riboflavinValue = [RCTAppleHealthKit doubleFromOptions:input key:@"riboflavin" withDefault:(double)0]; + double seleniumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"selenium" withDefault:(double)0]; + double sodiumValue = [RCTAppleHealthKit doubleFromOptions:input key:@"sodium" withDefault:(double)0]; + double sugarValue = [RCTAppleHealthKit doubleFromOptions:input key:@"sugar" withDefault:(double)0]; + double thiaminValue = [RCTAppleHealthKit doubleFromOptions:input key:@"thiamin" withDefault:(double)0]; + double vitaminAValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminA" withDefault:(double)0]; + double vitaminB12Value = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminB12" withDefault:(double)0]; + double vitaminB6Value = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminB6" withDefault:(double)0]; + double vitaminCValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminC" withDefault:(double)0]; + double vitaminDValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminD" withDefault:(double)0]; + double vitaminEValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminE" withDefault:(double)0]; + double vitaminKValue = [RCTAppleHealthKit doubleFromOptions:input key:@"vitaminK" withDefault:(double)0]; + double zincValue = [RCTAppleHealthKit doubleFromOptions:input key:@"zinc" withDefault:(double)0]; + + // Metadata including some new food-related keys // + NSDictionary *metadata = @{ + HKMetadataKeyFoodType:foodNameValue, + //@"HKFoodBrandName":@"FoodBrandName", // Restaurant name or packaged food brand name + //@"HKFoodTypeUUID":@"FoodTypeUUID", // Identifier for this food + @"HKFoodMeal":mealNameValue//, // Breakfast, Lunch, Dinner, or Snacks + //@"HKFoodImageName":@"FoodImageName" // Food icon name + }; + + // Create nutrtional data for food // + NSMutableSet *mySet = [[NSMutableSet alloc] init]; + if (biotinValue > 0){ + HKQuantitySample* biotin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryBiotin] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:biotinValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:biotin]; + } + if (caffeineValue > 0){ + HKQuantitySample* caffeine = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:caffeineValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + + [mySet addObject:caffeine]; + } + if (calciumValue > 0){ + HKQuantitySample* calcium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:calciumValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:calcium]; + } + if (carbohydratesValue > 0){ + HKQuantitySample* carbohydrates = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCarbohydrates] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:carbohydratesValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:carbohydrates]; + } + if (chlorideValue > 0){ + HKQuantitySample* chloride = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:chlorideValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:chloride]; + } + if (cholesterolValue > 0){ + HKQuantitySample* cholesterol = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCholesterol] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:cholesterolValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:cholesterol]; + } + if (copperValue > 0){ + HKQuantitySample* copper = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCopper] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:copperValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:copper]; + } + if (energyConsumedValue > 0){ + HKQuantitySample* energyConsumed = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed] + quantity:[HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:energyConsumedValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:energyConsumed]; + } + if (fatMonounsaturatedValue > 0){ + HKQuantitySample* fatMonounsaturated = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatMonounsaturated] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatMonounsaturatedValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:fatMonounsaturated]; + } + if (fatPolyunsaturatedValue > 0){ + HKQuantitySample* fatPolyunsaturated = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatPolyunsaturated] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatPolyunsaturatedValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:fatPolyunsaturated]; + } + if (fatSaturatedValue > 0){ + HKQuantitySample* fatSaturated = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatSaturated] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatSaturatedValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:fatSaturated]; + } + if (fatTotalValue > 0){ + HKQuantitySample* fatTotal = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatTotal] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fatTotalValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:fatTotal]; + } + if (fiberValue > 0){ + HKQuantitySample* fiber = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:fiberValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:fiber]; + } + if (folateValue > 0){ + HKQuantitySample* folate = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFolate] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:folateValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:folate]; + } + if (iodineValue > 0){ + HKQuantitySample* iodine = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIodine] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:iodineValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:iodine]; + } + if (ironValue > 0){ + HKQuantitySample* iron = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:ironValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:iron]; + } + if (magnesiumValue > 0){ + HKQuantitySample* magnesium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMagnesium] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:magnesiumValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:magnesium]; + } + if (manganeseValue > 0){ + HKQuantitySample* manganese = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryManganese] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:manganeseValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:manganese]; + } + if (molybdenumValue > 0){ + HKQuantitySample* molybdenum = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMolybdenum] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:molybdenumValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:molybdenum]; + } + if (niacinValue > 0){ + HKQuantitySample* niacin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryNiacin] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:niacinValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:niacin]; + } + if (pantothenicAcidValue > 0){ + HKQuantitySample* pantothenicAcid = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPantothenicAcid] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:pantothenicAcidValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:pantothenicAcid]; + } + if (phosphorusValue > 0){ + HKQuantitySample* phosphorus = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPhosphorus] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:phosphorusValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:phosphorus]; + } + if (potassiumValue > 0){ + HKQuantitySample* potassium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPotassium] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:potassiumValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:potassium]; + } + if (proteinValue > 0){ + HKQuantitySample* protein = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryProtein] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:proteinValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:protein]; + } + if (riboflavinValue > 0){ + HKQuantitySample* riboflavin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryRiboflavin] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:riboflavinValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:riboflavin]; + } + if (seleniumValue > 0){ + HKQuantitySample* selenium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySelenium] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:seleniumValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:selenium]; + } + if (sodiumValue > 0){ + HKQuantitySample* sodium = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySodium] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:sodiumValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:sodium]; + } + if (sugarValue > 0){ + HKQuantitySample* sugar = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySugar] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:sugarValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:sugar]; + } + if (thiaminValue > 0){ + HKQuantitySample* thiamin = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryThiamin] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:thiaminValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:thiamin]; + } + if (vitaminAValue > 0){ + HKQuantitySample* vitaminA = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminA] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminAValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:vitaminA]; + } + if (vitaminB12Value > 0){ + HKQuantitySample* vitaminB12 = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB12] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminB12Value] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:vitaminB12]; + } + if (vitaminB6Value > 0){ + HKQuantitySample* vitaminB6 = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB6] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminB6Value] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:vitaminB6]; + } + if (vitaminCValue > 0){ + HKQuantitySample* vitaminC = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminC] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminCValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:vitaminC]; + } + if (vitaminDValue > 0){ + HKQuantitySample* vitaminD = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminD] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminDValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:vitaminD]; + } + if (vitaminEValue > 0){ + HKQuantitySample* vitaminE = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminE] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminEValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + + [mySet addObject:vitaminE]; + } + if (vitaminKValue > 0){ + HKQuantitySample* vitaminK = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminK] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:vitaminKValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:vitaminK]; + } + if (zincValue > 0){ + HKQuantitySample* zinc = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryZinc] + quantity:[HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:zincValue] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + metadata:metadata]; + [mySet addObject:zinc]; + } + // Combine nutritional data into a food correlation // + HKCorrelation* food = [HKCorrelation correlationWithType:[HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierFood] + startDate:timeFoodWasConsumed + endDate:timeFoodWasConsumed + objects:mySet + metadata:metadata]; + // Save the food correlation to HealthKit // + [self.healthStore saveObject:food withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"An error occured saving the food sample %@. The error was: ", error); + callback(@[RCTMakeError(@"An error occured saving the food sample", error, nil)]); + return; + } + callback(@[[NSNull null], @true]); + }]; +} + +- (void)saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + NSDate *timeWaterWasConsumed = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; + double waterValue = [RCTAppleHealthKit doubleFromOptions:input key:@"water" withDefault:(double)0]; + + HKQuantitySample* water = [HKQuantitySample quantitySampleWithType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater] + quantity:[HKQuantity quantityWithUnit:[HKUnit literUnit] doubleValue:waterValue] + startDate:timeWaterWasConsumed + endDate:timeWaterWasConsumed + metadata:nil]; + + // Save the water Sample to HealthKit // + [self.healthStore saveObject:water withCompletion:^(BOOL success, NSError *error) { + if (!success) { + NSLog(@"An error occured saving the water sample %@. The error was: ", error); + callback(@[RCTMakeError(@"An error occured saving the water sample", error, nil)]); + return; + } + callback(@[[NSNull null], @true]); + }]; +} + +@end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 32fb563c2ceabfc5cddb294b56aa0bc92060c903..dcc0238f8720ab05a569cdc4bdfd9336ffcb11cc 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -17,7 +17,10 @@ - (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_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getDailyFlightsClimbedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 294c9f1603ae4ada4ce42838816c08d4fa89fa44..8fd935ca2f57b787589e7daff14b72ca048b4cde 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -230,6 +230,35 @@ }]; } +- (void)fitness_getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; + 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; + } + + HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; + + [self fetchCumulativeSumStatisticsCollection:quantityType + unit:unit + startDate:startDate + endDate:endDate + ascending:ascending + limit:limit + completion:^(NSArray *arr, NSError *err){ + if (err != nil) { + NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); + callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); + return; + } + callback(@[[NSNull null], arr]); + }]; +} - (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { @@ -255,6 +284,35 @@ }]; } +- (void)fitness_getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; + 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; + } + + HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; + + [self fetchCumulativeSumStatisticsCollection:quantityType + unit:unit + startDate:startDate + endDate:endDate + ascending:ascending + limit:limit + completion:^(NSArray *arr, NSError *err){ + if (err != nil) { + NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); + callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); + return; + } + callback(@[[NSNull null], arr]); + }]; +} - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { @@ -280,4 +338,34 @@ }]; } +- (void)fitness_getDailyFlightsClimbedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]]; + 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; + } + + HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed]; + + [self fetchCumulativeSumStatisticsCollection:quantityType + unit:unit + startDate:startDate + endDate:endDate + ascending:ascending + limit:limit + completion:^(NSArray *arr, NSError *err){ + if (err != nil) { + NSLog(@"error with fetchCumulativeSumStatisticsCollection: %@", err); + callback(@[RCTMakeError(@"error with fetchCumulativeSumStatisticsCollection", err, nil)]); + return; + } + callback(@[[NSNull null], arr]); + }]; +} + @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m index 3d43badb2335273babba1f9fc93fd10e389ff452..39e880f6a0e5e6abcb1d6719596894882fad9e2a 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+TypesAndPermissions.m @@ -74,7 +74,44 @@ @"ActiveEnergyBurned" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned], @"FlightsClimbed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed], // Nutrition Identifiers - @"DietaryEnergy" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], + @"Biotin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryBiotin], + @"Caffeine" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine], + @"Calcium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium], + @"Carbohydrates" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCarbohydrates], + @"Chloride" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride], + @"Cholesterol" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCholesterol], + @"Copper" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCopper], + @"EnergyConsumed" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed], + @"FatMonounsaturated" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatMonounsaturated], + @"FatPolyunsaturated" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatPolyunsaturated], + @"FatSaturated" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatSaturated], + @"FatTotal" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFatTotal], + @"Fiber" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFiber], + @"Folate" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryFolate], + @"Iodine" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIodine], + @"Iron" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron], + @"Magnesium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMagnesium], + @"Manganese" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryManganese], + @"Molybdenum" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryMolybdenum], + @"Niacin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryNiacin], + @"PantothenicAcid" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPantothenicAcid], + @"Phosphorus" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPhosphorus], + @"Potassium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryPotassium], + @"Protein" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryProtein], + @"Riboflavin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryRiboflavin], + @"Selenium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySelenium], + @"Sodium" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySodium], + @"Sugar" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietarySugar], + @"Thiamin" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryThiamin], + @"VitaminA" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminA], + @"VitaminB12" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB12], + @"VitaminB6" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminB6], + @"VitaminC" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminC], + @"VitaminD" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminD], + @"VitaminE" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminE], + @"VitaminK" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryVitaminK], + @"Zinc" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryZinc], + @"Water" : [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater], // Sleep @"SleepAnalysis" : [HKObjectType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis], // Mindfulness diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 41cd34e110052b41cd706ad9ae241244162b9540..90e7872de145f1622c5f71205760f3c066715f21 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -12,6 +12,7 @@ #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_Results.h" @@ -138,16 +139,41 @@ RCT_EXPORT_METHOD(getDistanceWalkingRunning:(NSDictionary *)input callback:(RCTR [self fitness_getDistanceWalkingRunningOnDay:input callback:callback]; } +RCT_EXPORT_METHOD(getDailyDistanceWalkingRunningSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_getDailyDistanceWalkingRunningSamples:input callback:callback]; +} + RCT_EXPORT_METHOD(getDistanceCycling:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getDistanceCyclingOnDay:input callback:callback]; } +RCT_EXPORT_METHOD(getDailyDistanceCyclingSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_getDailyDistanceCyclingSamples:input callback:callback]; +} + RCT_EXPORT_METHOD(getFlightsClimbed:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getFlightsClimbedOnDay:input callback:callback]; } +RCT_EXPORT_METHOD(getDailyFlightsClimbedSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_getDailyFlightsClimbedSamples:input callback:callback]; +} + +RCT_EXPORT_METHOD(saveFood:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self saveFood:input callback:callback]; +} + +RCT_EXPORT_METHOD(saveWater:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self saveWater:input callback:callback]; +} + RCT_EXPORT_METHOD(getHeartRateSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self vitals_getHeartRateSamples:input callback:callback]; @@ -158,6 +184,11 @@ RCT_EXPORT_METHOD(getActiveEnergyBurned:(NSDictionary *)input callback:(RCTRespo [self activity_getActiveEnergyBurned:input callback:callback]; } +RCT_EXPORT_METHOD(getBasalEnergyBurned:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self activity_getBasalEnergyBurned:input callback:callback]; +} + RCT_EXPORT_METHOD(getBodyTemperatureSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self vitals_getBodyTemperatureSamples:input callback:callback]; diff --git a/README.md b/README.md index 2339ca4fa5acd6cdb870dbf0cb38745e8421b081..0a385f8afe85979246b146061e6cafbba626e510 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # React Native Apple Healthkit -A React Native bridge module for interacting with Apple Healthkit data. Checkout the [full documentation](https://github.com/terrillo/rn-apple-healthkit/wiki) +A React Native bridge module for interacting with Apple Healthkit data. Checkout the [full documentation](https://github.com/terrillo/rn-apple-healthkit/tree/master/docs) ## Installation @@ -28,7 +28,6 @@ Update `info.plist` in your React Native project ![](https://i.imgur.com/eOCCCyv.png "Xcode Capabilities Section") 7. Compile and run - ## Get Started Initialize Healthkit. This will show the Healthkit permissions prompt for any read/write permissions set in the required `options` object. @@ -44,8 +43,8 @@ If new read/write permissions are added to the options object then the app user ```javascript let options = { permissions: { - read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex"], - write: ["Weight", "StepCount", "BodyMassIndex"] + read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex", "ActiveEnergyBurned"], + write: ["Height", "Weight", "StepCount", "BodyMassIndex", "Biotin", "Caffeine", "Calcium", "Carbohydrates", "Chloride", "Cholesterol", "Copper", "EnergyConsumed", "FatMonounsaturated", "FatPolyunsaturated", "FatSaturated", "FatTotal", "Fiber", "Folate", "Iodine", "Iron", "Magnesium", "Manganese", "Molybdenum", "Niacin", "PantothenicAcid", "Phosphorus", "Potassium", "Protein", "Riboflavin", "Selenium", "Sodium", "Sugar", "Thiamin", "VitaminA", "VitaminB12", "VitaminB6", "VitaminC", "VitaminD", "VitaminE", "VitaminK", "Zinc", "Water"] } }; ``` @@ -77,48 +76,64 @@ AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => } ``` -## Changelog +## Changelog +0.6.4v +- Basal energy ([#23](https://github.com/terrillo/rn-apple-healthkit/pull/23)) +- Fixed issues with saving weight in the past +- Commited the docs to increase pull request support +- Add daily samples for: + - Flights Climbed + - WalkingRunning Distance + - Cycling Distance + +0.6.3v +- Food and Water ([#19](https://github.com/terrillo/rn-apple-healthkit/pull/19)) + 0.6.1v - HKQuantityTypeIdentifierActiveEnergyBurned ## Wiki - * [Installation](https://github.com/terrillo/rn-apple-healthkit/wiki/Install) + * [Installation](/docs/Install) * [Documentation](#documentation) * [Permissions](#permissions) * [Units](#units) * Base Methods - * [isAvailable](https://github.com/terrillo/rn-apple-healthkit/wiki/isAvailable()) - * [initHealthKit](https://github.com/terrillo/rn-apple-healthkit/wiki/initHealthKit()) + * [isAvailable](/docs/isAvailable().md) + * [initHealthKit](/docs/initHealthKit().md) * Realtime Methods - * [initStepCountObserver](https://github.com/terrillo/rn-apple-healthkit/wiki/initStepCountObserver()) + * [initStepCountObserver](/docs/initStepCountObserver().md) * Read Methods - * [getActiveEnergyBurned](https://github.com/terrillo/rn-apple-healthkit/wiki/getActiveEnergyBurned()) - * [getBiologicalSex](https://github.com/terrillo/rn-apple-healthkit/wiki/getBiologicalSex()) - * [getBloodGlucoseSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getbloodglucosesamples()) - * [getBloodPressureSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getbloodpressuresamples()) - * [getBodyTemperatureSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getbodytemperaturesamples()) - * [getDailyStepCountSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getDailyStepCountSamples()) - * [getDateOfBirth](https://github.com/terrillo/rn-apple-healthkit/wiki/getDateOfBirth()) - * [getDistanceCycling](https://github.com/terrillo/rn-apple-healthkit/wiki/getdistancecycling()) - * [getDistanceWalkingRunning](https://github.com/terrillo/rn-apple-healthkit/wiki/getDistanceWalkingRunning()) - * [getFlightsClimbed](https://github.com/terrillo/rn-apple-healthkit/wiki/getflightsclimbed()) - * [getHeartRateSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getheartratesamples()) - * [getHeightSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getheightsamples()) - * [getLatestBmi](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestbmi()) - * [getLatestBodyFatPercentage](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestbodyfatpercentage()) - * [getLatestHeight](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestheight()) - * [getLatestLeanBodyMass](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestleanbodymass()) - * [getLatestWeight](https://github.com/terrillo/rn-apple-healthkit/wiki/getlatestweight()) - * [getRespiratoryRateSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getrespiratoryratesamples()) - * [getSleepSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getsleepsamples()) - * [getStepCount](https://github.com/terrillo/rn-apple-healthkit/wiki/getStepCount()) - * [getWeightSamples](https://github.com/terrillo/rn-apple-healthkit/wiki/getweightsamples()) + * [getActiveEnergyBurned](/docs/getActiveEnergyBurned().md) + * [getBasalEnergyBurned](/docs/getBasalEnergyBurned().md) + * [getBiologicalSex](/docs/getBiologicalSex().md) + * [getBloodGlucoseSamples](/docs/getbloodglucosesamples().md) + * [getBloodPressureSamples](/docs/getbloodpressuresamples().md) + * [getBodyTemperatureSamples](/docs/getbodytemperaturesamples().md) + * [getDailyDistanceCyclingSamples]() + * [getDailyDistanceWalkingRunningSamples](/docs/getDailyDistanceWalkingRunningSamples().md) + * [getDailyFlightsClimbedSamples](/docs/getDailyFlightsClimbedSamples().md) + * [getDailyStepCountSamples](/docs/getDailyStepCountSamples().md) + * [getDateOfBirth](/docs/getDateOfBirth().md) + * [getDistanceCycling](/docs/getdistancecycling().md) + * [getDistanceWalkingRunning](/docs/getDistanceWalkingRunning().md) + * [getFlightsClimbed](/docs/getflightsclimbed().md) + * [getHeartRateSamples](/docs/getheartratesamples().md) + * [getHeightSamples](/docs/getheightsamples().md) + * [getLatestBmi](/docs/getlatestbmi().md) + * [getLatestBodyFatPercentage](/docs/getlatestbodyfatpercentage().md) + * [getLatestHeight](/docs/getlatestheight().md) + * [getLatestLeanBodyMass](/docs/getlatestleanbodymass().md) + * [getLatestWeight](/docs/getlatestweight().md) + * [getRespiratoryRateSamples](/docs/getrespiratoryratesamples().md) + * [getSleepSamples](/docs/getsleepsamples().md) + * [getStepCount](/docs/getStepCount().md) + * [getWeightSamples](/docs/getweightsamples().md) * Write Methods - * [saveBmi](https://github.com/terrillo/rn-apple-healthkit/wiki/savebmi()) - * [saveHeight](https://github.com/terrillo/rn-apple-healthkit/wiki/saveheight()) - * [saveMindfulSession](https://github.com/terrillo/rn-apple-healthkit/wiki/saveMindfulSession()) - * [saveWeight](https://github.com/terrillo/rn-apple-healthkit/wiki/saveweight()) - * [saveSteps](https://github.com/terrillo/rn-apple-healthkit/wiki/saveSteps()) + * [saveBmi](/docs/savebmi().md) + * [saveHeight](/docs/saveheight().md) + * [saveMindfulSession](/docs/saveMindfulSession().md) + * [saveWeight](/docs/saveweight().md) + * [saveSteps](/docs/saveSteps().md) * [References](#references) ## Supported Apple Permissions @@ -127,7 +142,8 @@ The available Healthkit permissions to use with `initHealthKit` | Permission | Healthkit Identifier Type | Read | Write | |------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-------| -| ActiveEnergyBurned | [HKCharacteristicTypeIdentifierActiveEnergyBurned](https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615771-activeenergyburned?language=objc) | ✓ | | +| 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) | ✓ | | | 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) | ✓ | ✓ | diff --git a/docs/Installation.md b/docs/Installation.md new file mode 100644 index 0000000000000000000000000000000000000000..a03d374740fb1fe5e68aeefc34f2dfaa2fa52eeb --- /dev/null +++ b/docs/Installation.md @@ -0,0 +1,12 @@ +Install the `rn-apple-healthkit` npm package + +- Run `npm install rn-apple-healthkit --save` +- Run `react-native link rn-apple-healthkit` + +Update `info.plist` in your React Native project +``` +NSHealthShareUsageDescription +Read and understand health data. +NSHealthUpdateUsageDescription +Share workout data with other apps. +``` diff --git a/docs/Permissions.md b/docs/Permissions.md new file mode 100644 index 0000000000000000000000000000000000000000..9756c54fe4ca39d06660b99cbc3577ba2982cb56 --- /dev/null +++ b/docs/Permissions.md @@ -0,0 +1,21 @@ +These permissions are exported as constants of the `rn-apple-healthkit` module. + +```javascript +import AppleHealthKit from 'rn-apple-healthkit'; + +// get the available permissions from AppleHealthKit.Constants object +const PERMS = AppleHealthKit.Constants.Permissions; + +// setup healthkit read/write permissions using PERMS +const healthKitOptions = { + permissions: { + read: [ + PERMS.StepCount, + PERMS.Height, + ], + write: [ + PERMS.StepCount + ], + } +}; +``` \ No newline at end of file diff --git a/docs/getActiveEnergyBurned().md b/docs/getActiveEnergyBurned().md new file mode 100644 index 0000000000000000000000000000000000000000..17b31e1e5ff79319a9bad51dff9265105f9943c2 --- /dev/null +++ b/docs/getActiveEnergyBurned().md @@ -0,0 +1,18 @@ +A quantity sample type that measures the amount of active energy the user has burned. + +```javascript +let d = new Date(2016,1,1); +let options = { + startDate: (new Date(2016,10,1)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now +}; +``` + +```javascript +AppleHealthKit.getActiveEnergyBurned(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` \ No newline at end of file diff --git a/docs/getBasalEnergyBurned().md b/docs/getBasalEnergyBurned().md new file mode 100644 index 0000000000000000000000000000000000000000..32f4521ab755eab24a3da44613fed4a23953a4c9 --- /dev/null +++ b/docs/getBasalEnergyBurned().md @@ -0,0 +1,16 @@ +```javascript +let d = new Date(2016,1,1); +let options = { + startDate: (new Date(2018,10,1)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now +}; +``` + +```javascript +AppleHealthKit.getBasalEnergyBurned(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getBiologicalSex().md b/docs/getBiologicalSex().md new file mode 100644 index 0000000000000000000000000000000000000000..358a8dc29d23f0758c9dcb57cb3554437cbcf70c --- /dev/null +++ b/docs/getBiologicalSex().md @@ -0,0 +1,23 @@ +Get the biological sex (gender). If the `BiologicalSex` read permission is missing or the user has denied it then the value will be `unknown`. The possible values are: + +| Value | HKBiologicalSex | +|---------|-----------------------| +| unknown | HKBiologicalSexNotSet | +| male | HKBiologicalSexMale | +| female | HKBiologicalSexFemale | +| other | HKBiologicalSexOther | + +```javascript +AppleHealthKit.getBiologicalSex(null, (err: Object, results: Object) => { + if (this._handleHealthkitError(err, 'getBiologicalSex')) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 'female', +} +``` \ No newline at end of file diff --git a/docs/getBloodGlucoseSamples().md b/docs/getBloodGlucoseSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..5b2f86f34ca386cca585d5299ec81192fd7782b2 --- /dev/null +++ b/docs/getBloodGlucoseSamples().md @@ -0,0 +1,22 @@ +Query for blood glucose samples. the options object is used to setup a query to retrieve relevant samples. +```javascript +let options = { + unit: 'mmolPerL', // optional; default 'mmolPerL' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` +Available units are: `'mmolPerL'`, `'mgPerdL'`. + +The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields + +```javascript +AppleHealthKit.getBloodGlucoseSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getBloodPressureSamples().md b/docs/getBloodPressureSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..29708a580a58eea854d58634a2abee18c8eb9e13 --- /dev/null +++ b/docs/getBloodPressureSamples().md @@ -0,0 +1,28 @@ +Query for blood pressure samples. the options object is used to setup a query to retrieve relevant samples. +```javascript +let options = { + unit: 'mmhg', // optional; default 'mmhg' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +The callback function will be called with a `samples` array containing objects with *bloodPressureSystolicValue*, *bloodPressureDiastolicValue*, *startDate*, and *endDate* fields + +```javascript +AppleHealthKit.getBloodPressureSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +[ + { bloodPressureSystolicValue: 120, bloodPressureDiastolicValue: 81, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' }, + { bloodPressureSystolicValue: 119, bloodPressureDiastolicValue: 77, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' }, +] +``` \ No newline at end of file diff --git a/docs/getBodyTemperatureSamples().md b/docs/getBodyTemperatureSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..2d32c7d20736dced1cc225bb42ec6597aa2708a9 --- /dev/null +++ b/docs/getBodyTemperatureSamples().md @@ -0,0 +1,30 @@ +Query for body temperature samples. the options object is used to setup a query to retrieve relevant samples. + +```javascript +let options = { + unit: 'celsius', // optional; default 'celsius' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` +Available units are: `'fahrenheit'`, `'celsius'`. + +The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields. + +```javascript +AppleHealthKit.getBodyTemperatureSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +[ + { value: 74.02, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' }, + { value: 74, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' }, +] +``` \ No newline at end of file diff --git a/docs/getDailyDistanceCyclingSamples().md b/docs/getDailyDistanceCyclingSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..43cedb3537c565b74733d4e29351443a0b22c120 --- /dev/null +++ b/docs/getDailyDistanceCyclingSamples().md @@ -0,0 +1,17 @@ +```javascript +let options = { + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +```javascript +AppleHealthKit.getDailyDistanceCyclingSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyDistanceWalkingRunningSamples().md b/docs/getDailyDistanceWalkingRunningSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..b6a288058d52619d9ba342b1a18d6e277db9e5dd --- /dev/null +++ b/docs/getDailyDistanceWalkingRunningSamples().md @@ -0,0 +1,17 @@ +```javascript +let options = { + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +```javascript +AppleHealthKit.getDailyDistanceWalkingRunningSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyFlightsClimbedSamples().md b/docs/getDailyFlightsClimbedSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..ce9a6fd16476d0d49849ff1374af16865e9b958b --- /dev/null +++ b/docs/getDailyFlightsClimbedSamples().md @@ -0,0 +1,17 @@ +```javascript +let options = { + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +```javascript +AppleHealthKit.getDailyFlightsClimbedSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getDailyStepCountSamples().md b/docs/getDailyStepCountSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..c8a0c6530cb533a3803615023ac109c8a5aa8314 --- /dev/null +++ b/docs/getDailyStepCountSamples().md @@ -0,0 +1,18 @@ +Get the total steps per day over a specified date range. + +`getDailyStepCountSamples` accepts an options object containing required *`startDate: ISO8601Timestamp`* and optional *`endDate: ISO8601Timestamp`*. If `endDate` is not provided it will default to the current time +```javascript +let options = { + startDate: (new Date(2016,1,1)).toISOString() // required + endDate: (new Date()).toISOString() // optional; default now +}; +``` + +```javascript + AppleHealthKit.getDailyStepCountSamples(options: Object, (err: Object, results: Array) => { + if (this._handleHealthkitError(err, 'getDailyStepCountSamples')) { + return; + } + console.log(results) +}); +``` \ No newline at end of file diff --git a/docs/getDateOfBirth().md b/docs/getDateOfBirth().md new file mode 100644 index 0000000000000000000000000000000000000000..cb8d5933f19ae53dfa7877de1d67c965863e63e2 --- /dev/null +++ b/docs/getDateOfBirth().md @@ -0,0 +1,18 @@ +Get the date of birth. + +On success, the callback function will be provided with a `res` object containing dob `value: string` (ISO timestamp), and `age: number` (age in years): +```javascript +AppleHealthKit.getDateOfBirth(null, (err: Object, results: Object) => { + if (this._handleHealthkitError(err, 'getDateOfBirth')) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: '1986-09-01T00:00:00.000-0400', + age: 29 +} +``` \ No newline at end of file diff --git a/docs/getDistanceCycling().md b/docs/getDistanceCycling().md new file mode 100644 index 0000000000000000000000000000000000000000..ce5e1a602d9d5675c4f53d60a1b4fba05f7869d7 --- /dev/null +++ b/docs/getDistanceCycling().md @@ -0,0 +1,26 @@ +Get the total distance cycling on a specific day. + +`getDistanceCycling` accepts an options object containing optional *`date: ISO8601Timestamp`* and *`unit: string`*. If `date` is not provided it will default to the current time. `unit` defaults to `meter` +```javascript +let options = { + unit: 'mile', // optional; default 'meter' + date: (new Date(2016,5,1)).toISOString(), // optional; default now +}; +``` + +```javascript +AppleHealthKit.getDistanceCycling(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 11.45, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` \ No newline at end of file diff --git a/docs/getDistanceWalkingRunning().md b/docs/getDistanceWalkingRunning().md new file mode 100644 index 0000000000000000000000000000000000000000..308f7855e602df6e728e5e92de133138468738b2 --- /dev/null +++ b/docs/getDistanceWalkingRunning().md @@ -0,0 +1,27 @@ +Get the total distance walking/running on a specific day. + +`getDistanceWalkingRunning` accepts an options object containing optional *`date: ISO8601Timestamp`* and *`unit: string`*. If `date` is not provided it will default to the current time. `unit` defaults to `meter`. + +```javascript +let options = { + unit: 'mile', // optional; default 'meter' + date: (new Date(2016,5,1)).toISOString(), // optional; default now +}; +``` + +```javascript +AppleHealthKit.getDistanceWalkingRunning(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 1.45, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` \ No newline at end of file diff --git a/docs/getFlightsClimbed().md b/docs/getFlightsClimbed().md new file mode 100644 index 0000000000000000000000000000000000000000..b9f4f4a359f3409246a240fc98fe1820ea53d8c9 --- /dev/null +++ b/docs/getFlightsClimbed().md @@ -0,0 +1,25 @@ +get the total flights climbed (1 flight is ~10ft of elevation) on a specific day. + +`getFlightsClimbed` accepts an options object containing optional *`date: ISO8601Timestamp`*. if `date` is not provided it will default to the current time. +```javascript +let options = { + date: (new Date(2016,5,1)).toISOString(), // optional; default now +}; +``` + +```javascript +AppleHealthKit.getFlightsClimbed(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 15, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` \ No newline at end of file diff --git a/docs/getHeartRateSamples().md b/docs/getHeartRateSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..05abf47cd5ae858fb31b7d116ed70c591e93126b --- /dev/null +++ b/docs/getHeartRateSamples().md @@ -0,0 +1,26 @@ +Query for heart rate samples. the options object is used to setup a query to retrieve relevant samples. +```javascript +let options = { + unit: 'bpm', // optional; default 'bpm' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields +```javascript +AppleHealthKit.getHeartRateSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +[ + { value: 74.02, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' }, + { value: 74, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' }, +] \ No newline at end of file diff --git a/docs/getHeightSamples().md b/docs/getHeightSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..ffb11c266a82b5c0f7cd9554cbd0fd224ff4584e --- /dev/null +++ b/docs/getHeightSamples().md @@ -0,0 +1,27 @@ +query for height samples. the options object is used to setup a query to retrieve relevant samples. +```javascript +let options = { + unit: 'inch', // optional; default 'inch' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +```javascript +AppleHealthKit.getHeightSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` + +The callback function will be called with a `samples` array containing objects with `value`, `startDate`, and `endDate` fields +```javascript +[ + { value: 74.02, startDate:'2016-06-29T17:55:00.000-0400', endDate:'2016-06-29T17:55:00.000-0400' }, + { value: 74, startDate:'2016-03-12T13:22:00.000-0400', endDate:'2016-03-12T13:22:00.000-0400' }, +] +``` \ No newline at end of file diff --git a/docs/getLatestBmi().md b/docs/getLatestBmi().md new file mode 100644 index 0000000000000000000000000000000000000000..2fd194071605909e0da58bc4cc70e0359e03f2d7 --- /dev/null +++ b/docs/getLatestBmi().md @@ -0,0 +1,20 @@ +Get the most recent BMI sample. + +On success, the callback function will be provided with a `bmi` object containing the BMI `value`, and the `startDate` and `endDate` of the sample. *Note: startDate and endDate will be the same as bmi samples are saved at a specific point in time.* +```javascript +AppleHealthKit.getLatestBmi(null, (err: string, results: Object) => { + if (err) { + console.log("error getting latest bmi data: ", err); + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 27.2, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` diff --git a/docs/getLatestBodyFatPercentage().md b/docs/getLatestBodyFatPercentage().md new file mode 100644 index 0000000000000000000000000000000000000000..7288387c59b0a626319569b4a7381ac379efcf34 --- /dev/null +++ b/docs/getLatestBodyFatPercentage().md @@ -0,0 +1,20 @@ +Get the most recent body fat percentage. The percentage value is a number between 0 and 100. + +On success, the callback function will be provided with a `bodyFatPercentage` object containing the body fat percentage `value`, and the `startDate` and `endDate` of the sample. *Note: startDate and endDate will be the same as bodyFatPercentage samples are saved at a specific point in time.* + +```javascript +AppleHealthKit.getLatestBodyFatPercentage(null, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 20, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` \ No newline at end of file diff --git a/docs/getLatestHeight().md b/docs/getLatestHeight().md new file mode 100644 index 0000000000000000000000000000000000000000..3e317814a91d59e1d6fe4c0081951829d210542a --- /dev/null +++ b/docs/getLatestHeight().md @@ -0,0 +1,21 @@ +Get the most recent height value. + +On success, the callback function will be provided with a `height` object containing the height `value`, and the `startDate` and `endDate` of the height sample. *Note: startDate and endDate will be the same as height samples are saved at a specific point in time.* + +```javascript +AppleHealthKit.getLatestHeight(null, (err: string, results: Object) => { + if (err) { + console.log("error getting latest height: ", err); + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 72, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` \ No newline at end of file diff --git a/docs/getLatestLeanBodyMass().md b/docs/getLatestLeanBodyMass().md new file mode 100644 index 0000000000000000000000000000000000000000..b6a35b73a4429b747760886206467deac7e37b5b --- /dev/null +++ b/docs/getLatestLeanBodyMass().md @@ -0,0 +1,20 @@ +Get the most recent lean body mass. The value is a number representing the weight in pounds (lbs) + +On success, the callback function will be provided with a `leanBodyMass` object containing the leanBodyMass `value`, and the `startDate` and `endDate` of the sample. *Note: startDate and endDate will be the same as leanBodyMass samples are saved at a specific point in time.* + +```javascript + AppleHealthKit.getLatestLeanBodyMass(null, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 176, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` \ No newline at end of file diff --git a/docs/getLatestWeight().md b/docs/getLatestWeight().md new file mode 100644 index 0000000000000000000000000000000000000000..4374328254f0a78f40f1558629ddb918770dde0b --- /dev/null +++ b/docs/getLatestWeight().md @@ -0,0 +1,27 @@ +Get the most recent weight sample. + +On success, the callback function will be provided with a `weight` object containing the weight `value`, and the `startDate` and `endDate` of the weight sample. *Note: startDate and endDate will be the same as weight samples are saved at a specific point in time.* + +```javascript +let options = { + unit: 'pound' +}; +``` + +```javascript +AppleHealthKit.getLatestWeight(options, (err: string, results: Object) => { + if (err) { + console.log("error getting latest weight: ", err); + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 200, + startDate: '2016-07-08T12:00:00.000-0400', + endDate: '2016-07-08T12:00:00.000-0400' +} +``` diff --git a/docs/getRespiratoryRateSamples().md b/docs/getRespiratoryRateSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..67bbe86d62958454119def2466a6ed7036c17a6d --- /dev/null +++ b/docs/getRespiratoryRateSamples().md @@ -0,0 +1,22 @@ +Query for respiratory rate samples. the options object is used to setup a query to retrieve relevant samples. + +```javascript +let options = { + unit: 'bpm', // optional; default 'bpm' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields + +```javascript +AppleHealthKit.getRespiratoryRateSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` diff --git a/docs/getSleepSamples().md b/docs/getSleepSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..2fe71331293a7488fce37896c2566402ecd99dce --- /dev/null +++ b/docs/getSleepSamples().md @@ -0,0 +1,29 @@ +Query for sleep samples. + +Each sleep sample represents a period of time with a startDate and an endDate. +the sample's value will be either `INBED` or `ASLEEP`. these values should overlap, +meaning that two (or more) samples represent a single nights sleep activity. see +[Healthkit SleepAnalysis] reference documentation + +The options object is used to setup a query to retrieve relevant samples. +The options must contain `startDate` and may also optionally include `endDate` +and `limit` options +```javascript +let options = { + startDate: (new Date(2016,10,1)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + limit:10, // optional; default no limit +}; +``` + +The callback function will be called with a `samples` array containing objects +with *value*, *startDate*, and *endDate* fields + +```javascript +AppleHealthKit.getSleepSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results). +}); +``` \ No newline at end of file diff --git a/docs/getStepCount().md b/docs/getStepCount().md new file mode 100644 index 0000000000000000000000000000000000000000..f24f81af12f977c60c59529f260b9b0d538891e5 --- /dev/null +++ b/docs/getStepCount().md @@ -0,0 +1,24 @@ +Get the aggregated total steps for a specific day (starting and ending at midnight). + +An optional options object may be provided containing `date` field representing the selected day. If `date` is not set or an options object is not provided then the current day will be used. +```javascript +let d = new Date(2016,1,1); +let options = { + date: d.toISOString() +}; +``` + +```javascript +AppleHealthKit.getStepCount(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +{ + value: 213, +} +``` \ No newline at end of file diff --git a/docs/getWeightSamples().md b/docs/getWeightSamples().md new file mode 100644 index 0000000000000000000000000000000000000000..e23e4b860b75fa1ef4fe5828aeabb411c85c0f91 --- /dev/null +++ b/docs/getWeightSamples().md @@ -0,0 +1,28 @@ +Query for weight samples. the options object is used to setup a query to retrieve relevant samples. + +```javascript +let options = { + unit: 'pound', // optional; default 'pound' + startDate: (new Date(2016,4,27)).toISOString(), // required + endDate: (new Date()).toISOString(), // optional; default now + ascending: false, // optional; default false + limit:10, // optional; default no limit +}; +``` + +```javascript +AppleHealthKit.getWeightSamples(options, (err: Object, results: Array) => { + if (err) { + return; + } + console.log(results) +}); +``` + +```javascript +[ + { value: 160, startDate: '2016-07-09T00:00:00.000-0400', endDate: '2016-07-10T00:00:00.000-0400' }, + { value: 161, startDate: '2016-07-08T00:00:00.000-0400', endDate: '2016-07-09T00:00:00.000-0400' }, + { value: 165, startDate: '2016-07-07T00:00:00.000-0400', endDate: '2016-07-08T00:00:00.000-0400' }, +] +``` \ No newline at end of file diff --git a/docs/initHealthKit().md b/docs/initHealthKit().md new file mode 100644 index 0000000000000000000000000000000000000000..dc0882e62d8bdc7da003f74ca21eaa84d7f6fa12 --- /dev/null +++ b/docs/initHealthKit().md @@ -0,0 +1,30 @@ +Initialize Healthkit. This will show the Healthkit permissions prompt for any read/write permissions set in the required `options` object. + +Due to Apple's privacy model if an app user has previously denied a specific permission then they can not be prompted again for that same permission. The app user would have to go into the Apple Health app and grant the permission to your react-native app under *sources* tab. + +For any data that is read from Healthkit the status/error is the same for both. This privacy restriction results in having no knowledge of whether the permission was denied (make sure it's added to the permissions options object), or the data for the specific request was nil (ex. no steps recorded today). + +For any data written to Healthkit an authorization error can be caught. If an authorization error occurs you can prompt the user to set the specific permission or add the permission to the options object if not present. + +If new read/write permissions are added to the options object then the app user will see the Healthkit permissions prompt with the new permissions to allow. + +`initHealthKit` requires an options object with Healthkit permission settings +```javascript +let options = { + permissions: { + read: ["Height", "Weight", "StepCount", "DateOfBirth", "BodyMassIndex"], + write: ["Weight", "StepCount", "BodyMassIndex"] + } +}; +``` + +```javascript +AppleHealthKit.initHealthKit(options: Object, (err: string, results: Object) => { + if (err) { + console.log("error initializing Healthkit: ", err); + return; + } + // Healthkit is initialized... + // now safe to read and write Healthkit data... +}); +``` \ No newline at end of file diff --git a/docs/initStepCountObserver().md b/docs/initStepCountObserver().md new file mode 100644 index 0000000000000000000000000000000000000000..118b014443c387a97cfd741a30aa2c1ad2a2bf18 --- /dev/null +++ b/docs/initStepCountObserver().md @@ -0,0 +1,64 @@ +Setup an HKObserverQuery for step count (HKQuantityTypeIdentifierStepCount) that will +trigger an event listenable on react-native `NativeAppEventEmitter` when the +Healthkit step count has changed. + +The `initStepCountObserver` method must be called before adding a listener to +NativeAppEventEmitter. After the step count observer has been initialized you can +listen to the NativeAppEventEmitter `change:steps` event and re-fetch relevent +step count data in the event handler. + +The `initStepCountObserver` method should be called after Healthkit has been +successfully initialized (AppleHealthKit.initHealthKit has been called without +error). + +```javascript +// import NativeAppEventEmitter from react-native +import { + Navigator, + View, + NativeAppEventEmitter, +} from 'react-native'; +``` + +```javascript +AppleHealthKit.initHealthKit(HKOPTIONS, (err, res) => { + if (this._handleHKError(err, 'initHealthKit')) { + return; + } + + // initialize the step count observer + AppleHealthKit.initStepCountObserver({}, () => {}); + + // add event listener for 'change:steps' and handle the + // event in the event handler function. + // + // when adding a listener, a 'subscription' object is + // returned that must be used to remove the listener + // when the component unmounts. The subscription object + // must be accessible to any function/method/instance + // that will be unsubscribing from the event. + this.sub = NativeAppEventEmitter.addListener( + 'change:steps', + (evt) => { + // a 'change:steps' event has been received. step + // count data should be re-fetched from Healthkit. + this._fetchStepCountData(); + } + ); + + // other tasks to perform after Healthkit has been + // initialized (fetch relevant Healthkit data). + this._fetchStepCountData(); + this._fetchOtherRelevantHealthkitData(); + // ... +}); + +... + +// when the component where the listener was added unmounts +// (or whenever the listener should be removed), call the +// 'remove' method of the subscription object. +componentWillUnmount() { + this.sub.remove(); +} +``` diff --git a/docs/isAvailable().md b/docs/isAvailable().md new file mode 100644 index 0000000000000000000000000000000000000000..2cdb4f0b6e745f4368d77254803e0f22f1f23333 --- /dev/null +++ b/docs/isAvailable().md @@ -0,0 +1,13 @@ +Check for Healthkit availability + +```javascript +import AppleHealthKit from 'rn-apple-healthkit'; + +AppleHealthKit.isAvailable((err: Object, available: boolean) => { + if (err) { + console.log("error initializing Healthkit: ", err); + return; + } + // Healthkit is available +}); +``` \ No newline at end of file diff --git a/docs/saveBmi().md b/docs/saveBmi().md new file mode 100644 index 0000000000000000000000000000000000000000..ce7ddb6a7fd41c4dc82f88c7e1bd6c96bc25fb00 --- /dev/null +++ b/docs/saveBmi().md @@ -0,0 +1,16 @@ +save a numeric BMI value to Healthkit + +`saveBmi` accepts an options object containing a numeric BMI value: +```javascript +let options = { + value: 27.2 +} +``` +```javascript +AppleHealthKit.saveBmi(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + // BMI successfully saved +}); +``` \ No newline at end of file diff --git a/docs/saveHeight().md b/docs/saveHeight().md new file mode 100644 index 0000000000000000000000000000000000000000..1e8292b1a2ec6378220148d724f48c4ef9633353 --- /dev/null +++ b/docs/saveHeight().md @@ -0,0 +1,17 @@ +save a numeric height value to Healthkit + +`saveHeight` accepts an options object containing a numeric height value: +```javascript +let options = { + value: 200 // Inches +} +``` + +```javascript +AppleHealthKit.saveHeight(options: Object, (err: Object, results: Object) => { + if (err) { + return; + } + // height successfully saved +}); +``` \ No newline at end of file diff --git a/docs/saveMindfulSession().md b/docs/saveMindfulSession().md new file mode 100644 index 0000000000000000000000000000000000000000..4166912a732b5821c345d6abc91e46b5470a828d --- /dev/null +++ b/docs/saveMindfulSession().md @@ -0,0 +1,19 @@ +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 +}; + +``` + +``` +AppleHealthKit.saveMindfulSession(options, (err, res) => { + if (err) return { + return + } + console.log('Mindful session saved') +}); +``` \ No newline at end of file diff --git a/docs/saveSteps().md b/docs/saveSteps().md new file mode 100644 index 0000000000000000000000000000000000000000..a990b1248dc99bf2469cc203472cdf92d249461c --- /dev/null +++ b/docs/saveSteps().md @@ -0,0 +1,23 @@ +Save a step count sample. + +A step count sample represents the number of steps during a specific period of time. A sample should be a precise as possible, with startDate and endDate representing the range of time the steps were taken in. + +`saveSteps` accepts an options object containing required *`value: number`*, *`startDate: ISO8601Timestamp`*, and *`endDate: ISO8601Timestamp`*. +```javascript +// startDate and endDate are 30 minutes apart. +// this means the step count value occurred within those 30 minutes. +let options = { + value: 100, + startDate: (new Date(2016,6,2,6,0,0)).toISOString(), + endDate: (new Date(2016,6,2,6,30,0)).toISOString() +}; +``` + +```javascript +AppleHealthKit.saveSteps(options, (err, res) => { + if (this._handleHKError(err, 'saveSteps')) { + return; + } + // step count sample successfully saved +}); +``` \ No newline at end of file diff --git a/docs/saveWeight().md b/docs/saveWeight().md new file mode 100644 index 0000000000000000000000000000000000000000..d0c235b7b9151187e1db2d6fbc44c92253250f63 --- /dev/null +++ b/docs/saveWeight().md @@ -0,0 +1,18 @@ +save a numeric weight value to Healthkit + +`saveWeight` accepts an options object containing a numeric weight value: +```javascript +let options = { + value: 200 +} +``` + +```javascript +AppleHealthKit.saveWeight(options: Object, (err: Object, results: Object) => { + if (err) { + console.log("error saving weight to Healthkit: ", err); + return; + } + // Done +}); +``` \ No newline at end of file diff --git a/package.json b/package.json index 83246639db8df61631421821e97a64f00baada30..39bcf851edb858e24784b5704652f865c13031c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rn-apple-healthkit", - "version": "0.6.1", + "version": "0.6.4", "description": "A React Native package for interacting with Apple HealthKit", "main": "index.js", "repository": {