diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index 9f201735aaf2b06f00e7614cc897590b09152653..bf37c55c6b13f8ae4c3cd1df2cabc95bdd048408 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -12,6 +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_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 08226a5a55e69e8644d0383dba1f5975e0f0cc29..5e6fc0d7c4ecff9784bcfe180a28921fffc7b543 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -91,6 +91,31 @@ }]; } +- (void)fitness_setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +{ + HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]]; + NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"]; + + HKSampleType *samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + if ([type isEqual:@"Walking"]) { + samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]; + } else if ([type isEqual:@"StairClimbing"]) { + samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed]; + } else if ([type isEqual:@"Running"]){ + samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; + unit = [HKUnit mileUnit]; + } else if ([type isEqual:@"Cycling"]){ + samplesType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceCycling]; + unit = [HKUnit mileUnit]; + } else if ([type isEqual:@"Workout"]){ + samplesType = [HKObjectType workoutType]; + } + + [self setObserverForType:samplesType unit:unit completion:^(NSArray *results, NSError * error) { + callback(@[[NSNull null], results]); + }]; +} + - (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 39dc12cbd56adccca00a8c43b98b9e4751690764..8eec34b4a6e4739025ea621ed1759ed554315f5a 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -18,12 +18,19 @@ startDate:(NSDate *)startDate endDate:(NSDate *)endDate completion:(void (^)(NSArray *, NSError *))completionHandler; + + - (void)fetchSamplesOfType:(HKSampleType *)quantityType unit:(HKUnit *)unit predicate:(NSPredicate *)predicate ascending:(BOOL)asc limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; +- (void)setObserverForType:(HKSampleType *)quantityType + unit:(HKUnit *)unit + completion:(void (^)(NSArray *, NSError *))completion; + + - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType unit:(HKUnit *)unit predicate:(NSPredicate *)predicate diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index d28cb4beccf70bf79e53972757189d3f2de70d66..d84600ae890125f345b21d17dbff477e1e7d345b 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -9,6 +9,9 @@ #import "RCTAppleHealthKit+Queries.h" #import "RCTAppleHealthKit+Utils.h" +#import +#import + @implementation RCTAppleHealthKit (Queries) @@ -199,7 +202,65 @@ [self.healthStore executeQuery:query]; } - +- (void)setObserverForType:(HKSampleType *)type + unit:(HKUnit *)unit + completion:(void (^)(NSArray *, NSError *))completion { + 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]); + 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(@"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(@"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]; + + [self.healthStore executeQuery:query]; + [self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) { + NSLog(@"success %s print some error %@", success ? "true" : "false", [error localizedDescription]); + }]; +} diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.h b/RCTAppleHealthKit/RCTAppleHealthKit.h index 39b603415c06570ff35b15f7a7c63b7e44b37606..900c6e39ccd9c0d1e04119cd4948d57d39c634c8 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit.h @@ -15,6 +15,7 @@ @interface RCTAppleHealthKit : NSObject @property (nonatomic) HKHealthStore *healthStore; +@property BOOL isSync; - (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback; - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 04e3d575180d93a83a62a5fc11de34d00e8773d9..2d1fa49eb646e715a9a8053188273760da144c1e 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -111,6 +111,12 @@ RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBl [self fitness_getSamples:input callback:callback]; } +RCT_EXPORT_METHOD(setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +{ + [self fitness_setObserver:input callback:callback]; +} + + RCT_EXPORT_METHOD(getDailyStepCountSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) { [self fitness_getDailyStepSamples:input callback:callback];