From 7c741d25a970ca451cdeaa5179858c86b2c7e5a4 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 28 Jun 2016 20:03:58 -0400 Subject: [PATCH] added fitness_getStepCountForDay method --- .../RCTAppleHealthKit+Methods_Fitness.h | 1 + .../RCTAppleHealthKit+Methods_Fitness.m | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 150f274..8f5fdd8 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -11,5 +11,6 @@ @interface RCTAppleHealthKit (Methods_Fitness) - (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_getStepCountForDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; @end diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 31ac748..abd9938 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -8,6 +8,7 @@ #import "RCTAppleHealthKit+Methods_Fitness.h" #import "RCTAppleHealthKit+Queries.h" +#import "RCTAppleHealthKit+Utils.h" @implementation RCTAppleHealthKit (Methods_Fitness) @@ -27,4 +28,27 @@ }]; } + +- (void)fitness_getStepCountForDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + NSDate *date = [RCTAppleHealthKit dateFromOptions:input]; + 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)]); + }]; +} + + @end -- 2.26.2