Skip to content

Commit

Permalink
Fix generic percent unit-value mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
schaefba committed Mar 3, 2016
1 parent 514e5ff commit 9f55324
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Example/Tests/OMHSampleFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ + (HKSample*)typeIdentifier:(NSString*)sampleTypeIdentifier
HKQuantityTypeIdentifierBodyFatPercentage,
HKQuantityTypeIdentifierRespiratoryRate,
HKQuantityTypeIdentifierBodyTemperature,
HKQuantityTypeIdentifierBasalBodyTemperature
HKQuantityTypeIdentifierBasalBodyTemperature,
HKQuantityTypeIdentifierBloodAlcoholContent
] includes:sampleTypeIdentifier]) {

NSString* defaultUnitString = nil;
Expand All @@ -188,7 +189,8 @@ + (HKSample*)typeIdentifier:(NSString*)sampleTypeIdentifier
defaultUnitString = @"mcg";
} else if (sampleTypeIdentifier == HKQuantityTypeIdentifierDietaryWater) {
defaultUnitString = @"L";
} else if (sampleTypeIdentifier == HKQuantityTypeIdentifierOxygenSaturation || sampleTypeIdentifier == HKQuantityTypeIdentifierBodyFatPercentage) {
} else if (sampleTypeIdentifier == HKQuantityTypeIdentifierOxygenSaturation || sampleTypeIdentifier == HKQuantityTypeIdentifierBodyFatPercentage
|| sampleTypeIdentifier == HKQuantityTypeIdentifierBloodAlcoholContent) {
defaultUnitString = @"%";
} else if (sampleTypeIdentifier == HKQuantityTypeIdentifierBodyTemperature || sampleTypeIdentifier == HKQuantityTypeIdentifierBasalBodyTemperature) {
defaultUnitString = @"degC";
Expand Down
26 changes: 26 additions & 0 deletions Example/Tests/OMHSerializerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,32 @@
});
});

describe(@"Generic quantity sample with percent unit", ^{
itShouldBehaveLike(@"AnySerializerForSupportedSample",^{
NSDate *start = [NSDate date];
NSDate *end = start;
NSNumber *value = [NSNumber numberWithDouble:.127];
NSString *unitString = @"%";
HKSample *sample = [OMHSampleFactory typeIdentifier:HKQuantityTypeIdentifierBloodAlcoholContent
attrs:@{@"value":value,
@"unitString":unitString,
@"start":start,
@"end":end}];
return @{
@"sample":sample,
@"pathsToValues": @{
@"header.schema_id.name": @"hk-quantity-sample",
@"header.schema_id.namespace":@"granola",
@"header.schema_id.version": @"1.0",
@"body.quantity_type":[HKQuantityTypeIdentifierBloodAlcoholContent description],
@"body.unit_value.value":@12.7,
@"body.unit_value.unit":@"%",
@"body.effective_time_frame.date_time":[start RFC3339String]
}
};
});
});

describe(HKCorrelationTypeIdentifierFood,^{
itShouldBehaveLike(@"AnySerializerForSupportedSample", ^{
NSDate *sampleDate = [NSDate date];
Expand Down
16 changes: 15 additions & 1 deletion Pod/Classes/OMHSerializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,21 @@ - (id)bodyData {
HKQuantitySample *quantitySample = (HKQuantitySample*)self.sample;
HKQuantity *quantity = [quantitySample quantity];
NSMutableDictionary *serializedUnitValues = [NSMutableDictionary new];
if ([quantity isCompatibleWithUnit:[HKUnit unitFromString:@"count"]]) {

if ([[OMHSerializer parseUnitFromQuantity:quantity] isEqualToString:@"%"]) {

// Types that use "%" units are compatible with the "count" unit (in the next condition), so this condition to pre-empts that.
NSNumber* value = [NSNumber numberWithDouble:[quantity doubleValueForUnit:[HKUnit percentUnit]]];

[serializedUnitValues addEntriesFromDictionary:@{
@"unit_value":@{
@"value": @([value floatValue] * 100),
@"unit": @"%"
}
}
];
}
else if ([quantity isCompatibleWithUnit:[HKUnit unitFromString:@"count"]]) {
[serializedUnitValues addEntriesFromDictionary:@{
@"count": [NSNumber numberWithDouble:[quantity doubleValueForUnit:[HKUnit unitFromString:@"count"]]]
}
Expand Down

0 comments on commit 9f55324

Please sign in to comment.