Permalink
Browse files

feature: Made ObservableProperty more data-type tolerant

  • Loading branch information...
MikeMitterer committed Aug 6, 2015
1 parent 44a98a1 commit 20d2b067c154621a350e697c17dd5be72c5b5d66
@@ -20,11 +20,15 @@
part of mdlapplication;
Object mdlRootContext() {
final Logger _logger = new Logger('mdlapplication.mdlRootContext');
Object rootContext;
try {
rootContext = componentFactory().injector.get(MaterialApplication);
}
on Error {
on Error catch(e,stack) {
_logger.shout(e,stack);
throw new ArgumentError("Could not find rootContext.\n"
"Please define something like this: \n"
"class Applicaiton extends MaterialApplication { ... } \n"
@@ -51,7 +51,7 @@ class FormatterPipeline {
* HTML: <span mdl-observe="pi | number(value,2) | decorate(value)"></span>
* DART: final List<String> parts = element.attributes[_MaterialObserveConstant.WIDGET_SELECTOR].trim().split("|");
*/
FormatterPipeline.fromList(this._formatter,final List<String> parts) {
FormatterPipeline.fromList(this._formatter,final Iterable<String> parts) {
Validate.notNull(_formatter);
Validate.notNull(parts);
@@ -75,7 +75,7 @@ class FormatterPipeline {
//- private -----------------------------------------------------------------------------------
/// Generates PipeCommands from [parts]
void _addCommands(final List<String> parts) {
void _addCommands(final Iterable<String> parts) {
//_logger.info("P ${parts.length}");
parts.forEach((final String part) {
@@ -60,12 +60,28 @@ class ObservableProperty<T> {
return _onChange.stream;
}
void set value(final T val) {
void set value(final val) {
final T old = _value;
_value = val;
//_logger.info("Value: $val");
_fire(new PropertyChangeEvent(val,old));
if(_value.runtimeType == bool) {
_value = _toBool(val) as T;
} else if(_value.runtimeType == int) {
_value = _toInt(val) as T;
} else if(_value.runtimeType == double) {
_value = _toDouble(val) as T;
} else {
_value = val;
}
_logger.fine("Input-Value: '$val' (${val.runtimeType}) -> '${_value}' (${_value.runtimeType})");
_fire(new PropertyChangeEvent(_value,old));
}
T get value => _value;
@@ -115,15 +131,7 @@ class ObservableProperty<T> {
/// Converts [value] to bool. If [value] is a String, "true", "on", "1" are valid boolean values
bool toBool() {
if(value is bool) {
return value as bool;
}
if(value is num) {
return (value as num).toInt() == 1;
}
final String stringvalue = "$value".toLowerCase();
return stringvalue == "true" || stringvalue == "on" || stringvalue == "1" || stringvalue == "yes";
return _toBool(value);
}
// - private ----------------------------------------------------------------------------------
@@ -144,4 +152,39 @@ class ObservableProperty<T> {
_onChange.add(event);
}
}
bool _toBool(final value) {
if(value is bool) {
return value as bool;
}
if(value is num) {
return (value as num).toInt() == 1;
}
final String stringvalue = "$value".toLowerCase();
return stringvalue == "true" || stringvalue == "on" || stringvalue == "1" || stringvalue == "yes";
}
int _toInt(final value) {
if(value is int) {
return value;
}
if(value is num) {
return (value as num).toInt();
}
final String stringvalue = "$value".toLowerCase();
return int.parse(stringvalue);
}
double _toDouble(final value) {
if(value is double) {
return value;
}
if(value is num) {
return (value as num).toDouble();
}
final String stringvalue = "$value".toLowerCase();
return double.parse(stringvalue);
}
}

0 comments on commit 20d2b06

Please sign in to comment.