Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of a set of units instead of single unit in query correlation #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions src/ios/HealthKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
static NSString *const HKPluginKeySampleType = @"sampleType";
static NSString *const HKPluginKeyAggregation = @"aggregation";
static NSString *const HKPluginKeyUnit = @"unit";
static NSString *const HKPluginKeyUnits = @"units";
static NSString *const HKPluginKeyAmount = @"amount";
static NSString *const HKPluginKeyValue = @"value";
static NSString *const HKPluginKeyCorrelationType = @"correlationType";
Expand Down Expand Up @@ -1522,14 +1523,19 @@ - (void)queryCorrelationType:(CDVInvokedUrlCommand *)command {
NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:[args[HKPluginKeyStartDate] longValue]];
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:[args[HKPluginKeyEndDate] longValue]];
NSString *correlationTypeString = args[HKPluginKeyCorrelationType];
NSString *unitString = args[HKPluginKeyUnit];
NSArray<NSString *> *unitsString = args[HKPluginKeyUnits];

HKCorrelationType *type = (HKCorrelationType *) [HealthKit getHKSampleType:correlationTypeString];
if (type == nil) {
[HealthKit triggerErrorCallbackWithMessage:@"sampleType was invalid" command:command delegate:self.commandDelegate];
return;
}
HKUnit *unit = ((unitString != nil) ? [HKUnit unitFromString:unitString] : nil);
NSMutableArray *units = [[NSMutableArray alloc] init];
for (NSString *unitString in unitsString) {
HKUnit *unit = ((unitString != nil) ? [HKUnit unitFromString:unitString] : nil);
[units addObject:unit];
}

// TODO check that unit is compatible with sampleType if sample type of HKQuantityType
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];

Expand All @@ -1551,6 +1557,8 @@ - (void)queryCorrelationType:(CDVInvokedUrlCommand *)command {

// common indices
entry[HKPluginKeyUUID] = sample.UUID.UUIDString;
entry[HKPluginKeySourceName] = sample.source.name;
entry[HKPluginKeySourceBundleId] = sample.source.bundleIdentifier;
if (sample.metadata == nil || ![NSJSONSerialization isValidJSONObject:sample.metadata]) {
entry[HKPluginKeyMetadata] = @{};
} else {
Expand All @@ -1572,26 +1580,37 @@ - (void)queryCorrelationType:(CDVInvokedUrlCommand *)command {

NSMutableArray *samples = [NSMutableArray arrayWithCapacity:correlation.objects.count];
for (HKQuantitySample *quantitySample in correlation.objects) {
// if an incompatible unit was passed, the sample is not included
if ([quantitySample.quantity isCompatibleWithUnit:unit]) {
[samples addObject:@{
HKPluginKeyStartDate: [HealthKit stringFromDate:quantitySample.startDate],
HKPluginKeyEndDate: [HealthKit stringFromDate:quantitySample.endDate],
HKPluginKeySampleType: quantitySample.sampleType.identifier,
HKPluginKeyValue: @([quantitySample.quantity doubleValueForUnit:unit]),
HKPluginKeyUnit: unit.unitString,
HKPluginKeyMetadata: ((quantitySample.metadata != nil) ? quantitySample.metadata : @{}),
HKPluginKeyUUID: quantitySample.UUID.UUIDString
for (int i=0; i<[units count]; i++) {
HKUnit *unit = units[i];
NSString *unitS = unitsString[i];
if ([quantitySample.quantity isCompatibleWithUnit:unit]) {
[samples addObject:@{
HKPluginKeyStartDate: [HealthKit stringFromDate:quantitySample.startDate],
HKPluginKeyEndDate: [HealthKit stringFromDate:quantitySample.endDate],
HKPluginKeySampleType: quantitySample.sampleType.identifier,
HKPluginKeyValue: @([quantitySample.quantity doubleValueForUnit:unit]),
HKPluginKeyUnit: unitS,
HKPluginKeyMetadata: ((quantitySample.metadata != nil) ? quantitySample.metadata : @{}),
HKPluginKeyUUID: quantitySample.UUID.UUIDString
}
];
break;
}
];
}
}
entry[HKPluginKeyObjects] = samples;

} else if ([sample isKindOfClass:[HKQuantitySample class]]) {

HKQuantitySample *qsample = (HKQuantitySample *) sample;
entry[@"quantity"] = @([qsample.quantity doubleValueForUnit:unit]);
for (int i=0; i<[units count]; i++) {
HKUnit *unit = units[i];
if ([qsample.quantity isCompatibleWithUnit:unit]) {
double quantity = [qsample.quantity doubleValueForUnit:unit];
entry[@"quantity"] = [NSString stringWithFormat:@"%f", quantity];
break;
}
}

} else if ([sample isKindOfClass:[HKWorkout class]]) {

Expand Down Expand Up @@ -1701,4 +1720,4 @@ - (void)deleteObject:(CDVInvokedUrlCommand *)command {

@end

#pragma clang diagnostic pop
#pragma clang diagnostic pop