Permalink
Browse files

Update unit and value classes from app (string for numeric value)

  • Loading branch information...
p2 committed Feb 20, 2013
1 parent 4ace9a3 commit 8801b55fe56a34d81987f8aad5f8cfe5c39e6ee3
@@ -36,6 +36,7 @@
- (NSString *)stringValueForNumber:(NSDecimalNumber *)number;
- (NSString *)stringValueForNumber:(NSDecimalNumber *)number withSize:(CHValueStringSize)size;
+- (NSString *)stringValueForNumberOnly:(NSDecimalNumber *)number;
- (NSDecimalNumber *)numberInBaseUnit:(NSDecimalNumber *)number;
- (NSDecimalNumber *)convertNumber:(NSDecimalNumber *)number toUnit:(CHUnit *)unit;
@@ -59,7 +59,7 @@ - (id)jsonObject
#pragma mark - String Value
/**
- * Returns the string value for a number in the receiver's unit.
+ * Returns the string value for a number and the unit label in the receiver's unit.
*
* This method calls stringValueForNumber:withSize: with "CHValueStringSizeSmall" as parameter.
*/
@@ -69,12 +69,16 @@ - (NSString *)stringValueForNumber:(NSDecimalNumber *)number
}
/**
- * Returns the string value for a number in the receiver's unit, with the given size.
+ * Returns the string value for a number and the unit label in the receiver's unit, with the given size.
*
* Subclasses should override this method.
*/
- (NSString *)stringValueForNumber:(NSDecimalNumber *)number withSize:(CHValueStringSize)size
{
+ if (!number) {
+ return nil;
+ }
+
if (CHValueStringSizeCompact == size) {
return [NSString stringWithFormat:@"%@%@", [self roundedNumber:number], (_label ? _label : @"")];
}
@@ -85,6 +89,14 @@ - (NSString *)stringValueForNumber:(NSDecimalNumber *)number withSize:(CHValueSt
return [NSString stringWithFormat:@"%@ %@", [self roundedNumber:number], (_label ? _label : @"")];
}
+/**
+ * Returns the string value for a number in the receiver's unit, but the number only.
+ */
+- (NSString *)stringValueForNumberOnly:(NSDecimalNumber *)number
+{
+ return [[self roundedNumber:number] description];
+}
+
#pragma mark - Conversion
@@ -249,7 +261,7 @@ + (id)newWithPath:(NSString *)aPath
// set dimension properties
unit.isBaseUnit = [baseName isEqualToString:name];
[unit setMinPlausibleFromBaseUnit:plausMin];
- [unit setMinPlausibleFromBaseUnit:plausMax];
+ [unit setMaxPlausibleFromBaseUnit:plausMax];
return unit;
}
@@ -282,6 +294,10 @@ + (id)newFromDictionary:(NSDictionary *)dict withName:(NSString *)name inDimensi
unit.name = ([name length] > 0) ? name : [dict objectForKey:@"name"];
unit.label = [dict objectForKey:@"label"];
unit.baseMultiplier = [dict objectForKey:@"baseMultiplier"] ? [NSDecimalNumber decimalNumberWithString:[dict objectForKey:@"baseMultiplier"]] : nil;
+ NSNumber *precision = [dict objectForKey:@"precision"];
+ if (precision) {
+ unit.precision = [precision shortValue];
+ }
return unit;
}
@@ -360,6 +376,9 @@ + (NSArray *)unitsForDataType:(NSString *)dataType
if ([@"age" isEqualToString:dataType]) {
return [self unitsOfDimension:@"age" baseUnit:nil];
}
+ if ([@"bmi" isEqualToString:dataType]) {
+ return [self unitsOfDimension:@"bmi" baseUnit:nil];
+ }
DLog(@"No data type or none that we understand: %@", dataType);
return nil;
@@ -32,6 +32,7 @@
- (NSString *)stringValue;
- (NSString *)stringValueWithSize:(CHValueStringSize)size;
+- (NSString *)numericStringValue;
- (NSInteger)checkPlausibility;
- (BOOL)isNull;
@@ -118,6 +118,17 @@ - (NSString *)stringValueWithSize:(CHValueStringSize)size
return _number ? [_number stringValue] : @"";
}
+/**
+ * Returns a string for the numerical part of the value, usually rounded.
+ */
+- (NSString *)numericStringValue
+{
+ if (_unit) {
+ return [_unit stringValueForNumberOnly:_number];
+ }
+ return _number ? [_number stringValue] : @"";
+}
+
#pragma mark - NSCopying

0 comments on commit 8801b55

Please sign in to comment.