Skip to content

Commit

Permalink
Merge pull request #91 from iradkot/source-info
Browse files Browse the repository at this point in the history
feat: add source id and name to fetchQuantitySamplesOfType func
  • Loading branch information
EJohnF committed May 26, 2019
2 parents 5b51ff0 + 4710db8 commit 5533d12
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions RCTAppleHealthKit/RCTAppleHealthKit+Queries.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType

NSDictionary *elem = @{
@"value" : @(value),
@"sourceName" : [[[sample sourceRevision] source] name],
@"sourceId" : [[[sample sourceRevision] source] bundleIdentifier],
@"startDate" : startDateString,
@"endDate" : endDateString,
};
Expand Down Expand Up @@ -115,7 +117,7 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
completion:(void (^)(NSArray *, NSError *))completion {
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierEndDate
ascending:asc];

// declare the block
void (^handlerBlock)(HKSampleQuery *query, NSArray *results, NSError *error);
// create and assign the block
Expand All @@ -126,25 +128,25 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
}
return;
}

if (completion) {
NSMutableArray *data = [NSMutableArray arrayWithCapacity:1];

dispatch_async(dispatch_get_main_queue(), ^{
if (type == [HKObjectType workoutType]) {
for (HKWorkout *sample in results) {
double energy = [[sample totalEnergyBurned] doubleValueForUnit:[HKUnit kilocalorieUnit]];
double distance = [[sample totalDistance] doubleValueForUnit:[HKUnit mileUnit]];
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];

NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];

bool isTracked = true;
if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) {
isTracked = false;
}

NSString* device = @"";
if (@available(iOS 11.0, *)) {
device = [[sample sourceRevision] productType];
Expand All @@ -154,7 +156,7 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
device = @"iPhone";
}
}

NSDictionary *elem = @{
@"activityId" : [NSNumber numberWithInt:[sample workoutActivityType]],
@"activityName" : type,
Expand All @@ -167,27 +169,27 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
@"start" : startDateString,
@"end" : endDateString
};

[data addObject:elem];
}
} else {
for (HKQuantitySample *sample in results) {
HKQuantity *quantity = sample.quantity;
double value = [quantity doubleValueForUnit:unit];

NSString * valueType = @"quantity";
if (unit == [HKUnit mileUnit]) {
valueType = @"distance";
}

NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.startDate];
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:sample.endDate];

bool isTracked = true;
if ([[sample metadata][HKMetadataKeyWasUserEntered] intValue] == 1) {
isTracked = false;
}

NSString* device = @"";
if (@available(iOS 11.0, *)) {
device = [[sample sourceRevision] productType];
Expand All @@ -197,7 +199,7 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
device = @"iPhone";
}
}

NSDictionary *elem = @{
valueType : @(value),
@"tracked" : @(isTracked),
Expand All @@ -207,22 +209,22 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
@"start" : startDateString,
@"end" : endDateString
};

[data addObject:elem];
}
}

completion(data, error);
});
}
};

HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:type
predicate:predicate
limit:lim
sortDescriptors:@[timeSortDescriptor]
resultsHandler:handlerBlock];

[self.healthStore executeQuery:query];
}

Expand All @@ -234,16 +236,16 @@ - (void)setObserverForType:(HKSampleType *)type
return;
}
[self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""];

// 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

// dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 5);
// dispatch_after(delay, dispatch_get_main_queue(), ^(void){
// completionHandler();
// });
}];

[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]);
Expand Down
2 changes: 1 addition & 1 deletion docs/getBloodGlucoseSamples().md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let options = {
```
Available units are: `'mmolPerL'`, `'mgPerdL'`.

The callback function will be called with a `samples` array containing objects with *value*, *startDate*, and *endDate* fields
The callback function will be called with a `samples` array containing objects with *value*, *sourceId*, *sourceName*, *startDate*, and *endDate* fields

```javascript
AppleHealthKit.getBloodGlucoseSamples(options, (err: Object, results: Array<Object>) => {
Expand Down

0 comments on commit 5533d12

Please sign in to comment.