Commit f4fb1a9c authored by Adam Ivancza's avatar Adam Ivancza

Slight updates for HealthKit observer

parent f51978cc
......@@ -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;
......
......@@ -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];
}
......
......@@ -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
......
......@@ -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 {
......
......@@ -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];
}
......
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