Commit 9357e8c2 authored by Irad's avatar Irad

feat: add source id and name to fetchQuantitySamplesOfType func

parent 5b51ff08
...@@ -86,6 +86,8 @@ ...@@ -86,6 +86,8 @@
NSDictionary *elem = @{ NSDictionary *elem = @{
@"value" : @(value), @"value" : @(value),
@"sourceName" : [[[sample sourceRevision] source] name],
@"sourceId" : [[[sample sourceRevision] source] bundleIdentifier],
@"startDate" : startDateString, @"startDate" : startDateString,
@"endDate" : endDateString, @"endDate" : endDateString,
}; };
...@@ -115,7 +117,7 @@ ...@@ -115,7 +117,7 @@
completion:(void (^)(NSArray *, NSError *))completion { completion:(void (^)(NSArray *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
ascending:asc]; ascending:asc];
// declare the block // declare the block
void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error); void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
// create and assign the block // create and assign the block
...@@ -126,25 +128,25 @@ ...@@ -126,25 +128,25 @@
} }
return; return;
} }
if (completion) { if (completion) {
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1]; NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if (type == [HKObjectType workoutType]) { if (type == [HKObjectType workoutType]) {
for (HKWorkout *sample in results) { for (HKWorkout *sample in results) {
double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]]; double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]];
double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]]; double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]];
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]]; NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
bool isTracked = true; bool isTracked = true;
if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) { if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) {
isTracked = false; isTracked = false;
} }
NSString* device = @""; NSString* device = @"";
if (@available(iOS 11.0, *)) { if (@available(iOS 11.0, *)) {
device = [[sample sourceRevision] productType]; device = [[sample sourceRevision] productType];
...@@ -154,7 +156,7 @@ ...@@ -154,7 +156,7 @@
device = @"iPhone"; device = @"iPhone";
} }
} }
NSDictionary *elem = @{ NSDictionary *elem = @{
@"activityId" : [NSNumber numberWithInt:[sample workoutActivityType]], @"activityId" : [NSNumber numberWithInt:[sample workoutActivityType]],
@"activityName" : type, @"activityName" : type,
...@@ -167,27 +169,27 @@ ...@@ -167,27 +169,27 @@
@"start" : startDateString, @"start" : startDateString,
@"end" : endDateString @"end" : endDateString
}; };
[data addObject:elem]; [data addObject:elem];
} }
} else { } else {
for (HKQuantitySample *sample in results) { for (HKQuantitySample *sample in results) {
HKQuantity *quantity = sample.quantity; HKQuantity *quantity = sample.quantity;
double value = [quantity doubleValueForUnit:unit]; double value = [quantity doubleValueForUnit:unit];
NSString * valueType = @"quantity"; NSString * valueType = @"quantity";
if (unit == [HKUnit mileUnit]) { if (unit == [HKUnit mileUnit]) {
valueType = @"distance"; valueType = @"distance";
} }
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate]; NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate]; NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];
bool isTracked = true; bool isTracked = true;
if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) { if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) {
isTracked = false; isTracked = false;
} }
NSString* device = @""; NSString* device = @"";
if (@available(iOS 11.0, *)) { if (@available(iOS 11.0, *)) {
device = [[sample sourceRevision] productType]; device = [[sample sourceRevision] productType];
...@@ -197,7 +199,7 @@ ...@@ -197,7 +199,7 @@
device = @"iPhone"; device = @"iPhone";
} }
} }
NSDictionary *elem = @{ NSDictionary *elem = @{
valueType : @(value), valueType : @(value),
@"tracked" : @(isTracked), @"tracked" : @(isTracked),
...@@ -207,22 +209,22 @@ ...@@ -207,22 +209,22 @@
@"start" : startDateString, @"start" : startDateString,
@"end" : endDateString @"end" : endDateString
}; };
[data addObject:elem]; [data addObject:elem];
} }
} }
completion(data, error); completion(data, error);
}); });
} }
}; };
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type
predicate:predicate predicate:predicate
limit:lim limit:lim
sortDescriptors:@[timeSortDescriptor] sortDescriptors:@[timeSortDescriptor]
resultsHandler:handlerBlock]; resultsHandler:handlerBlock];
[self.healthStore executeQuery:query]; [self.healthStore executeQuery:query];
} }
...@@ -234,16 +236,16 @@ ...@@ -234,16 +236,16 @@
return; return;
} }
[self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""]; [self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""];
// Theoretically, HealthKit expect that copletionHandler would be called at the end of query process, // Theoretically, HealthKit expect that copletionHandler would be called at the end of query process,
// but it's unclear how to do in in event paradigm // but it's unclear how to do in in event paradigm
// dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 5); // dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 5);
// dispatch_after(delay, dispatch_get_main_queue(), ^(void){ // dispatch_after(delay, dispatch_get_main_queue(), ^(void){
// completionHandler(); // completionHandler();
// }); // });
}]; }];
[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) {
NSLog(@"success %s print some error %@", success ? "true" : "false", [error localizedDescription]); NSLog(@"success %s print some error %@", success ? "true" : "false", [error localizedDescription]);
......
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