From b3c187be648f0d6d44d0de1d64e7e329372067a2 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Tue, 11 Oct 2016 18:47:08 -0400 Subject: [PATCH] implemented fitness_initializeStepEventObserver and exported RCT initStepCountObserver method --- .../RCTAppleHealthKit+Methods_Fitness.h | 2 ++ .../RCTAppleHealthKit+Methods_Fitness.m | 36 +++++++++++++++++++ RCTAppleHealthKit/RCTAppleHealthKit.m | 15 ++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 3a859b0..a396875 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -18,6 +18,8 @@ - (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_getDistanceCyclingOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getFlightsClimbedOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index f885df4..3b5ae88 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -10,8 +10,12 @@ #import "RCTAppleHealthKit+Queries.h" #import "RCTAppleHealthKit+Utils.h" +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" + @implementation RCTAppleHealthKit (Methods_Fitness) + //- (void)fitness_getStepCountForToday:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback //{ // HKQuantityType *stepCountType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; @@ -156,6 +160,38 @@ +- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKSampleType *sampleType = + [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + + HKObserverQuery *query = + [[HKObserverQuery alloc] + initWithSampleType:sampleType + predicate:nil + updateHandler:^(HKObserverQuery *query, + HKObserverQueryCompletionHandler completionHandler, + NSError *error) { + + if (error) { + // Perform Proper Error Handling Here... + NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***", error.localizedDescription); + callback(@[RCTMakeError(@"An error occured while setting up the stepCount observer", error, nil)]); + return; + } + + [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" + body:@{@"name": @"change:steps"}]; + + // If you have subscribed for background updates you must call the completion handler here. + // completionHandler(); + + }]; + + [self.healthStore executeQuery:query]; +} + + - (void)fitness_getDistanceWalkingRunningOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index f016e75..036881b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -17,8 +17,14 @@ #import "RCTAppleHealthKit+Methods_Vitals.h" #import "RCTAppleHealthKit+Methods_Results.h" +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" + @implementation RCTAppleHealthKit +@synthesize bridge = _bridge; + + RCT_EXPORT_MODULE(); @@ -32,6 +38,11 @@ RCT_EXPORT_METHOD(initHealthKit:(NSDictionary *)input callback:(RCTResponseSende [self initializeHealthKit:input callback:callback]; } +RCT_EXPORT_METHOD(initStepCountObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_initializeStepEventObserver:input callback:callback]; +} + RCT_EXPORT_METHOD(getBiologicalSex:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self characteristic_getBiologicalSex:input callback:callback]; @@ -181,7 +192,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { self.healthStore = [[HKHealthStore alloc] init]; - + if ([HKHealthStore isHealthDataAvailable]) { NSSet *writeDataTypes; NSSet *readDataTypes; @@ -193,7 +204,7 @@ RCT_EXPORT_METHOD(getInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock NSArray* writePermsArray = [permissions objectForKey:@"write"]; NSSet* readPerms = [self getReadPermsFromOptions:readPermsArray]; NSSet* writePerms = [self getWritePermsFromOptions:writePermsArray]; - + if(readPerms != nil) { readDataTypes = readPerms; } -- 2.26.2