Commit f51978cc authored by Evgenii Evstropov's avatar Evgenii Evstropov

raw version of observers

parent e2aa87df
......@@ -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;
......
......@@ -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
{
......
......@@ -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
......
......@@ -9,6 +9,9 @@
#import "RCTAppleHealthKit+Queries.h"
#import "RCTAppleHealthKit+Utils.h"
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>
@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]);
}];
}
......
......@@ -15,6 +15,7 @@
@interface RCTAppleHealthKit : NSObject <RCTBridgeModule>
@property (nonatomic) HKHealthStore *healthStore;
@property BOOL isSync;
- (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback;
- (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
......
......@@ -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];
......
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