WIP #308 Move entity icon generation into EntityIcon widget
This commit is contained in:
		| @@ -22,7 +22,7 @@ class ButtonEntityContainer extends StatelessWidget { | |||||||
|               fit: BoxFit.fitHeight, |               fit: BoxFit.fitHeight, | ||||||
|               child: EntityIcon( |               child: EntityIcon( | ||||||
|                 padding: EdgeInsets.fromLTRB(2.0, 6.0, 2.0, 2.0), |                 padding: EdgeInsets.fromLTRB(2.0, 6.0, 2.0, 2.0), | ||||||
|                 iconSize: Sizes.iconSize, |                 size: Sizes.iconSize, | ||||||
|               ) |               ) | ||||||
|             ), |             ), | ||||||
|           ), |           ), | ||||||
|   | |||||||
| @@ -27,14 +27,20 @@ class BadgeWidget extends StatelessWidget { | |||||||
|       case "media_player": |       case "media_player": | ||||||
|       case "binary_sensor": |       case "binary_sensor": | ||||||
|         { |         { | ||||||
|           badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData( |           badgeIcon = EntityIcon( | ||||||
|               entityModel.entityWrapper, iconSize, Colors.black); |             padding: EdgeInsets.all(0.0), | ||||||
|  |             size: iconSize, | ||||||
|  |             color: Colors.black | ||||||
|  |           ); | ||||||
|           break; |           break; | ||||||
|         } |         } | ||||||
|       case "device_tracker": |       case "device_tracker": | ||||||
|         { |         { | ||||||
|           badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData( |           badgeIcon = EntityIcon( | ||||||
|               entityModel.entityWrapper, iconSize, Colors.black); |               padding: EdgeInsets.all(0.0), | ||||||
|  |               size: iconSize, | ||||||
|  |               color: Colors.black | ||||||
|  |           ); | ||||||
|           onBadgeTextValue = entityModel.entityWrapper.entity.state; |           onBadgeTextValue = entityModel.entityWrapper.entity.state; | ||||||
|           break; |           break; | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -3,20 +3,64 @@ part of '../main.dart'; | |||||||
| class EntityIcon extends StatelessWidget { | class EntityIcon extends StatelessWidget { | ||||||
|  |  | ||||||
|   final EdgeInsetsGeometry padding; |   final EdgeInsetsGeometry padding; | ||||||
|   final double iconSize; |   final double size; | ||||||
|  |   final Color color; | ||||||
|  |  | ||||||
|   const EntityIcon({Key key, this.iconSize: Sizes.iconSize, this.padding: const EdgeInsets.fromLTRB( |   const EntityIcon({Key key, this.color, this.size: Sizes.iconSize, this.padding: const EdgeInsets.fromLTRB( | ||||||
|       Sizes.leftWidgetPadding, 0.0, 12.0, 0.0)}) : super(key: key); |       Sizes.leftWidgetPadding, 0.0, 12.0, 0.0)}) : super(key: key); | ||||||
|  |  | ||||||
|  |   int getDefaultIconByEntityId(String entityId, String deviceClass, String state) { | ||||||
|  |     String domain = entityId.split(".")[0]; | ||||||
|  |     String iconNameByDomain = MaterialDesignIcons.defaultIconsByDomains["$domain.$state"] ?? MaterialDesignIcons.defaultIconsByDomains["$domain"]; | ||||||
|  |     String iconNameByDeviceClass; | ||||||
|  |     if (deviceClass != null) { | ||||||
|  |       iconNameByDeviceClass = MaterialDesignIcons.defaultIconsByDeviceClass["$domain.$deviceClass.$state"] ?? MaterialDesignIcons.defaultIconsByDeviceClass["$domain.$deviceClass"]; | ||||||
|  |     } | ||||||
|  |     String iconName = iconNameByDeviceClass ?? iconNameByDomain; | ||||||
|  |     if (iconName != null) { | ||||||
|  |       return MaterialDesignIcons.iconsDataMap[iconName] ?? 0; | ||||||
|  |     } else { | ||||||
|  |       return 0; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   Widget buildIcon(EntityWrapper data, Color color) { | ||||||
|  |     if (data == null) { | ||||||
|  |       return null; | ||||||
|  |     } | ||||||
|  |     if (data.entity.entityPicture != null) { | ||||||
|  |       return CircleAvatar( | ||||||
|  |         radius: size/2, | ||||||
|  |         backgroundColor: Colors.white, | ||||||
|  |         backgroundImage: CachedNetworkImageProvider( | ||||||
|  |           "$homeAssistantWebHost${data.entity.entityPicture}", | ||||||
|  |         ), | ||||||
|  |       ); | ||||||
|  |     } else { | ||||||
|  |       String iconName = data.icon; | ||||||
|  |       int iconCode = 0; | ||||||
|  |       if (iconName.length > 0) { | ||||||
|  |         iconCode = MaterialDesignIcons.getIconCodeByIconName(iconName); | ||||||
|  |       } else { | ||||||
|  |         iconCode = getDefaultIconByEntityId(data.entity.entityId, | ||||||
|  |             data.entity.deviceClass, data.entity.state); // | ||||||
|  |       } | ||||||
|  |       return Icon( | ||||||
|  |         IconData(iconCode, fontFamily: 'Material Design Icons'), | ||||||
|  |         size: size, | ||||||
|  |         color: color, | ||||||
|  |       ); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper; |     final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper; | ||||||
|     return Padding( |     return Padding( | ||||||
|       padding: padding, |       padding: padding, | ||||||
|       child: MaterialDesignIcons.createIconWidgetFromEntityData( |       child: buildIcon( | ||||||
|           entityWrapper, |           entityWrapper, | ||||||
|           iconSize, |           color ?? EntityColor.stateColor(entityWrapper.entity.state) | ||||||
|           EntityColor.stateColor(entityWrapper.entity.state) |  | ||||||
|       ), |       ), | ||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -35,7 +35,7 @@ class GlanceEntityContainer extends StatelessWidget { | |||||||
|     result.add( |     result.add( | ||||||
|       EntityIcon( |       EntityIcon( | ||||||
|         padding: EdgeInsets.all(0.0), |         padding: EdgeInsets.all(0.0), | ||||||
|         iconSize: iconSize, |         size: iconSize, | ||||||
|       ) |       ) | ||||||
|     ); |     ); | ||||||
|     if (!nameInTheBottom) { |     if (!nameInTheBottom) { | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| part of 'main.dart'; | part of 'main.dart'; | ||||||
|  |  | ||||||
| class MaterialDesignIcons { | class MaterialDesignIcons { | ||||||
|   static Map _defaultIconsByDomains = { |   static final Map defaultIconsByDomains = { | ||||||
|     "light": "mdi:lightbulb", |     "light": "mdi:lightbulb", | ||||||
|     "switch": "mdi:flash", |     "switch": "mdi:flash", | ||||||
|     "binary_sensor": "mdi:checkbox-blank-circle-outline", |     "binary_sensor": "mdi:checkbox-blank-circle-outline", | ||||||
| @@ -34,7 +34,7 @@ class MaterialDesignIcons { | |||||||
|     "alarm_control_panel" : "mdi:bell" |     "alarm_control_panel" : "mdi:bell" | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   static Map _defaultIconsByDeviceClass = { |   static final Map defaultIconsByDeviceClass = { | ||||||
|     //"binary_sensor.battery": "mdi:", //TODO |     //"binary_sensor.battery": "mdi:", //TODO | ||||||
|     "binary_sensor.cold.on": "mdi:snowflake", |     "binary_sensor.cold.on": "mdi:snowflake", | ||||||
|     "binary_sensor.cold.off": "mdi:thermometer", |     "binary_sensor.cold.off": "mdi:thermometer", | ||||||
| @@ -92,7 +92,7 @@ class MaterialDesignIcons { | |||||||
|     "cover.window.closing": "mdi:window-open", |     "cover.window.closing": "mdi:window-open", | ||||||
|     "cover.window.opening": "mdi:window-open", |     "cover.window.opening": "mdi:window-open", | ||||||
|   }; |   }; | ||||||
|   static Map _iconsDataMap = { |   static final Map iconsDataMap = { | ||||||
|     "mdi:access-point": 0xf002, |     "mdi:access-point": 0xf002, | ||||||
|     "mdi:access-point-network": 0xf003, |     "mdi:access-point-network": 0xf003, | ||||||
|     "mdi:account": 0xf004, |     "mdi:account": 0xf004, | ||||||
| @@ -2890,7 +2890,7 @@ class MaterialDesignIcons { | |||||||
|     "mdi:blank": 0xf68c |     "mdi:blank": 0xf68c | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   static Widget createIconWidgetFromEntityData(EntityWrapper data, double size, Color color) { |   /*static Widget createIconWidgetFromEntityData(EntityWrapper data, double size, Color color) { | ||||||
|     if (data == null) { |     if (data == null) { | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
| @@ -2921,7 +2921,7 @@ class MaterialDesignIcons { | |||||||
|         color: color, |         color: color, | ||||||
|       ); |       ); | ||||||
|     } |     } | ||||||
|   } |   }*/ | ||||||
|  |  | ||||||
|   static IconData createIconDataFromIconCode(int code) { |   static IconData createIconDataFromIconCode(int code) { | ||||||
|     return IconData(code, fontFamily: 'Material Design Icons'); |     return IconData(code, fontFamily: 'Material Design Icons'); | ||||||
| @@ -2932,10 +2932,10 @@ class MaterialDesignIcons { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   static int getIconCodeByIconName(String name) { |   static int getIconCodeByIconName(String name) { | ||||||
|     return _iconsDataMap[name] ?? 0; |     return iconsDataMap[name] ?? 0; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   static int getDefaultIconByEntityId(String entityId, String deviceClass, String state) { |   /*static int getDefaultIconByEntityId(String entityId, String deviceClass, String state) { | ||||||
|     String domain = entityId.split(".")[0]; |     String domain = entityId.split(".")[0]; | ||||||
|     String iconNameByDomain = _defaultIconsByDomains["$domain.$state"] ?? _defaultIconsByDomains["$domain"]; |     String iconNameByDomain = _defaultIconsByDomains["$domain.$state"] ?? _defaultIconsByDomains["$domain"]; | ||||||
|     String iconNameByDeviceClass; |     String iconNameByDeviceClass; | ||||||
| @@ -2948,6 +2948,6 @@ class MaterialDesignIcons { | |||||||
|     } else { |     } else { | ||||||
|       return 0; |       return 0; | ||||||
|     } |     } | ||||||
|   } |   }*/ | ||||||
|  |  | ||||||
| } | } | ||||||
| @@ -149,7 +149,7 @@ class CardWidget extends StatelessWidget { | |||||||
|           mainAxisAlignment: MainAxisAlignment.end, |           mainAxisAlignment: MainAxisAlignment.end, | ||||||
|           children: [ |           children: [ | ||||||
|             EntityIcon( |             EntityIcon( | ||||||
|               iconSize: 50.0, |               size: 50.0, | ||||||
|             ), |             ), | ||||||
|             Container( |             Container( | ||||||
|               width: 26.0, |               width: 26.0, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user