Skip to content

Commit 0b1792d

Browse files
authored
Merge pull request #386 from cbxalves/feat/statistics-sources-metadata
feat: Add sources metadata to statistics collection query
2 parents 855ba90 + 0084499 commit 0b1792d

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Queries.m

+25-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
935935
// Create the query
936936
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType
937937
quantitySamplePredicate:predicate
938-
options:HKStatisticsOptionCumulativeSum
938+
options:HKStatisticsOptionCumulativeSum | HKStatisticsOptionSeparateBySource
939939
anchorDate:anchorDate
940940
intervalComponents:interval];
941941

@@ -961,10 +961,33 @@ - (void)fetchCumulativeSumStatisticsCollection:(HKQuantityType *)quantityType
961961
NSString *startDateString = [RCTAppleHealthKit buildISO8601StringFromDate:startDate];
962962
NSString *endDateString = [RCTAppleHealthKit buildISO8601StringFromDate:endDate];
963963

964+
NSMutableArray *metadata = [NSMutableArray arrayWithCapacity:1];
965+
966+
for (HKSource *source in result.sources) {
967+
968+
NSString *bundleIdentifier = source.bundleIdentifier;
969+
NSString *name = source.name;
970+
HKQuantity *sourceQuantity = [result sumQuantityForSource:source];
971+
double quantity = [sourceQuantity doubleValueForUnit:unit];
972+
973+
974+
if (quantity != 0) {
975+
NSDictionary *sourceItem = @{
976+
@"sourceId" : bundleIdentifier,
977+
@"sourceName" : name,
978+
@"quantity" : @(quantity),
979+
};
980+
981+
[metadata addObject:sourceItem];
982+
}
983+
}
984+
985+
964986
NSDictionary *elem = @{
965987
@"value" : @(value),
966988
@"startDate" : startDateString,
967989
@"endDate" : endDateString,
990+
@"metadata" : metadata,
968991
};
969992
[data addObject:elem];
970993
}
@@ -1214,3 +1237,4 @@ - (void)fetchActivitySummary:(NSDate *)startDate
12141237
}
12151238

12161239
@end
1240+

docs/getDailyStepCountSamples.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ Example output, value is on count unit:
3232
{
3333
"endDate": "2021-03-22T17:00:00.000-0300",
3434
"startDate": "2021-03-22T16:00:00.000-0300",
35-
"value": 3978
35+
"value": 3978,
36+
"metadata": [
37+
{
38+
"sourceId": "com.apple.Health",
39+
"sourceName": "Health",
40+
"quantity": 3978
41+
}
42+
]
3643
}
3744
]
3845
```

0 commit comments

Comments
 (0)