Commit f4fb1a9c authored by Adam Ivancza's avatar Adam Ivancza

Slight updates for HealthKit observer

parent f51978cc
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
- (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_getSamples:(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_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
- (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
......
...@@ -91,7 +91,7 @@ ...@@ -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]]; HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]];
NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"]; NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"];
...@@ -111,9 +111,7 @@ ...@@ -111,9 +111,7 @@
samplesType = [HKObjectType workoutType]; samplesType = [HKObjectType workoutType];
} }
[self setObserverForType:samplesType unit:unit completion:^(NSArray *results, NSError * error) { [self setObserverForType:samplesType unit:unit];
callback(@[[NSNull null], results]);
}];
} }
......
...@@ -27,8 +27,7 @@ ...@@ -27,8 +27,7 @@
limit:(NSUInteger)lim limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion; completion:(void (^)(NSArray *, NSError *))completion;
- (void)setObserverForType:(HKSampleType *)quantityType - (void)setObserverForType:(HKSampleType *)quantityType
unit:(HKUnit *)unit unit:(HKUnit *)unit;
completion:(void (^)(NSArray *, NSError *))completion;
- (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
@implementation RCTAppleHealthKit (Queries) @implementation RCTAppleHealthKit (Queries)
- (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType
predicate:(NSPredicate *)predicate predicate:(NSPredicate *)predicate
completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion {
...@@ -203,58 +202,25 @@ ...@@ -203,58 +202,25 @@
} }
- (void)setObserverForType:(HKSampleType *)type - (void)setObserverForType:(HKSampleType *)type
unit:(HKUnit *)unit unit:(HKUnit *)unit {
completion:(void (^)(NSArray *, NSError *))completion {
NSLog(@"set observer"); NSLog(@"set observer");
void(^handler)(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error); HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error){
handler = ^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error) {
NSLog(@"enter to a handler %@", [error localizedDescription]);
UIApplication *app = [UIApplication sharedApplication]; UIApplication *app = [UIApplication sharedApplication];
UIApplicationState appState = [app applicationState]; NSLog(@"observer fired");
[self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""];
if (!self.isSync){ completionHandler();
// switch (appState) { // self.isSync = true;
// case UIApplicationStateActive: // __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{
// 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(@"call event background"); // NSLog(@"observer fired from bg");
//// [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" // [self.bridge.eventDispatcher sendAppEventWithName:@"observer"
//// body:@{@"name": @"change:steps"}]; // body:@""];
// [self fetchSamplesOfType:type unit:unit predicate:nil ascending:true limit:1 completion:completion];
// [app endBackgroundTask:backgroundTaskIdentifier];
// self.isSync = false;
// }];
// }
// //
// }; // [app endBackgroundTask:backgroundTaskIdentifier];
// completionHandler();
NSLog(@"enter to background case"); // self.isSync = false;
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];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
[self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) { [self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) {
...@@ -262,9 +228,6 @@ ...@@ -262,9 +228,6 @@
}]; }];
} }
- (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
limit:(NSUInteger)lim limit:(NSUInteger)lim
completion:(void (^)(NSArray *, NSError *))completion { completion:(void (^)(NSArray *, NSError *))completion {
......
...@@ -111,9 +111,9 @@ RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBl ...@@ -111,9 +111,9 @@ RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBl
[self fitness_getSamples:input callback:callback]; [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];
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment