From f4fb1a9ca89f3ad4f949a50483609080245d3251 Mon Sep 17 00:00:00 2001 From: Adam Ivancza Date: Fri, 9 Feb 2018 19:19:13 +0100 Subject: [PATCH] Slight updates for HealthKit observer --- .../RCTAppleHealthKit+Methods_Fitness.h | 2 +- .../RCTAppleHealthKit+Methods_Fitness.m | 6 +- RCTAppleHealthKit/RCTAppleHealthKit+Queries.h | 3 +- RCTAppleHealthKit/RCTAppleHealthKit+Queries.m | 67 +++++-------------- RCTAppleHealthKit/RCTAppleHealthKit.m | 4 +- 5 files changed, 21 insertions(+), 61 deletions(-) diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index bf37c55..32fb563 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -12,7 +12,7 @@ - (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)fitness_setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_setObserver:(NSDictionary *)input; - (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 5e6fc0d..294c9f1 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -91,7 +91,7 @@ }]; } -- (void)fitness_setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +- (void)fitness_setObserver:(NSDictionary *)input { HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]]; NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"]; @@ -111,9 +111,7 @@ samplesType = [HKObjectType workoutType]; } - [self setObserverForType:samplesType unit:unit completion:^(NSArray *results, NSError * error) { - callback(@[[NSNull null], results]); - }]; + [self setObserverForType:samplesType unit:unit]; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 8eec34b..381a82c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -27,8 +27,7 @@ limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; - (void)setObserverForType:(HKSampleType *)quantityType - unit:(HKUnit *)unit - completion:(void (^)(NSArray *, NSError *))completion; + unit:(HKUnit *)unit; - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index d84600a..7e73b65 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -14,7 +14,6 @@ @implementation RCTAppleHealthKit (Queries) - - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { @@ -203,58 +202,25 @@ } - (void)setObserverForType:(HKSampleType *)type - unit:(HKUnit *)unit - completion:(void (^)(NSArray *, NSError *))completion { + unit:(HKUnit *)unit { NSLog(@"set observer"); - void(^handler)(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error); - handler = ^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error) { - NSLog(@"enter to a handler %@", [error localizedDescription]); + HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error){ UIApplication *app = [UIApplication sharedApplication]; - UIApplicationState appState = [app applicationState]; - - if (!self.isSync){ -// switch (appState) { -// case UIApplicationStateActive: -// NSLog(@"app Active try to make sync"); -// break; -// case UIApplicationStateInactive: -// { -// NSLog(@"enter to inactive case"); -// break; -// } -// case UIApplicationStateBackground: -// { -// NSLog(@"enter to background case"); -// self.isSync = true; -// __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ + NSLog(@"observer fired"); + [self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""]; + completionHandler(); +// self.isSync = true; +// __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ // -// NSLog(@"call event background"); -//// [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" -//// body:@{@"name": @"change:steps"}]; -// [self fetchSamplesOfType:type unit:unit predicate:nil ascending:true limit:1 completion:completion]; -// [app endBackgroundTask:backgroundTaskIdentifier]; -// self.isSync = false; -// }]; -// } +// NSLog(@"observer fired from bg"); +// [self.bridge.eventDispatcher sendAppEventWithName:@"observer" +// body:@""]; // -// }; - - NSLog(@"enter to background case"); - self.isSync = true; - __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ - - NSLog(@"call event background"); - [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" - body:@{@"name": @"change:steps"}]; - [self fetchSamplesOfType:type unit:unit predicate:nil ascending:true limit:1 completion:completion]; - [app endBackgroundTask:backgroundTaskIdentifier]; - completionHandler(); - self.isSync = false; - }]; - } - - }; - HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:handler]; +// [app endBackgroundTask:backgroundTaskIdentifier]; +// completionHandler(); +// self.isSync = false; +// }]; + }]; [self.healthStore executeQuery:query]; [self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) { @@ -262,9 +228,6 @@ }]; } - - - - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 2d1fa49..bbc9c17 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -111,9 +111,9 @@ RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBl [self fitness_getSamples:input callback:callback]; } -RCT_EXPORT_METHOD(setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +RCT_EXPORT_METHOD(setObserver:(NSDictionary *)input) { - [self fitness_setObserver:input callback:callback]; + [self fitness_setObserver:input]; } -- 2.26.2