// // RCTAppleHealthKit+Methods_Fitness.m // RCTAppleHealthKit // // Created by Greg Wilson on 2016-06-26. // Copyright © 2016 Greg Wilson. All rights reserved. // #import "RCTAppleHealthKit+Methods_Fitness.h" #import "RCTAppleHealthKit+Queries.h" #import "RCTAppleHealthKit+Utils.h" @implementation RCTAppleHealthKit (Methods_Fitness) - (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKUnit *stepsUnit = [HKUnit countUnit]; [self fetchSumOfSamplesTodayForType:stepCountType unit:stepsUnit completion:^(double totalSteps, NSError *error) { if (!totalSteps) { NSLog(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully."); callback(@[RCTMakeError(@"Either an error occured fetching the user's step count information or none has been stored yet. In your app, try to handle this gracefully.", nil, nil)]); return; } callback(@[[NSNull null], @(totalSteps)]); }]; } - (void)fitness_getStepCountForDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { NSDate *date = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; if(date == nil) { callback(@[RCTMakeError(@"could not parse date from options.date", nil, nil)]); return; } HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKUnit *stepsUnit = [HKUnit countUnit]; [self fetchSumOfSamplesOnDayForType:stepCountType unit:stepsUnit day:date completion:^(double totalSteps, NSError *error) { if (!totalSteps) { NSLog(@"could not fetch step count for day: %@", error); callback(@[RCTMakeError(@"could not fetch step count for day", error, nil)]); return; } callback(@[[NSNull null], @(totalSteps)]); }]; } - (void)fitness_getDailyStepCounts:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { NSDate *startDate = [RCTAppleHealthKit startDateFromOptions:input]; NSDate *endDate = [RCTAppleHealthKit endDateFromOptionsDefaultNow:input]; if(startDate == nil) { callback(@[RCTMakeError(@"could not parse required startDate from options.startDate", nil, nil)]); return; } HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKUnit *stepsUnit = [HKUnit countUnit]; [self fetchCumulativeSumStatisticsCollection:stepCountType unit:stepsUnit startDate:startDate endDate:endDate 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_getDailyStepSamples:(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 *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; [self fetchCumulativeSumStatisticsCollection:stepCountType 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_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { double value = [RCTAppleHealthKit doubleFromOptions:input key:@"value" withDefault:(double)0]; NSDate *startDate = [RCTAppleHealthKit dateFromOptions:input key:@"startDate" withDefault:nil]; NSDate *endDate = [RCTAppleHealthKit dateFromOptions:input key:@"endDate" withDefault:[NSDate date]]; if(startDate == nil || endDate == nil){ callback(@[RCTMakeError(@"startDate and endDate are required in options", nil, nil)]); return; } HKUnit *unit = [HKUnit countUnit]; HKQuantity *quantity = [HKQuantity quantityWithUnit:unit doubleValue:value]; HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:type quantity:quantity startDate:startDate endDate:endDate]; [self.healthStore saveObject:sample withCompletion:^(BOOL success, NSError *error) { if (!success) { NSLog(@"An error occured saving the step count sample %@. The error was: %@.", sample, error); callback(@[RCTMakeError(@"An error occured saving the step count sample", error, nil)]); return; } callback(@[[NSNull null], @(value)]); }]; } - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; NSDate *date = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; [self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSError *error) { if (!distance) { NSLog(@"ERROR getting DistanceWalkingRunning: %@", error); callback(@[RCTMakeError(@"ERROR getting DistanceWalkingRunning", error, nil)]); return; } callback(@[[NSNull null], @(distance)]); }]; } - (void)fitness_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit meterUnit]]; NSDate *date = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; [self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double distance, NSError *error) { if (!distance) { NSLog(@"ERROR getting DistanceCycling: %@", error); callback(@[RCTMakeError(@"ERROR getting DistanceCycling", error, nil)]); return; } callback(@[[NSNull null], @(distance)]); }]; } - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { HKUnit *unit = [HKUnit countUnit]; NSDate *date = [RCTAppleHealthKit dateFromOptions:input key:@"date" withDefault:[NSDate date]]; HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed]; [self fetchSumOfSamplesOnDayForType:quantityType unit:unit day:date completion:^(double count, NSError *error) { if (!count) { NSLog(@"ERROR getting FlightsClimbed: %@", error); callback(@[RCTMakeError(@"ERROR getting FlightsClimbed", error, nil), @(count)]); return; } callback(@[[NSNull null], @(count)]); }]; } @end