WIP #530 Badges refactoring
This commit is contained in:
		
							
								
								
									
										29
									
								
								lib/cards/badges.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								lib/cards/badges.dart
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | |||||||
|  | part of '../main.dart'; | ||||||
|  |  | ||||||
|  | class Badges extends StatelessWidget { | ||||||
|  |   final BadgesData badges; | ||||||
|  |  | ||||||
|  |   const Badges({Key key, this.badges}) : super(key: key); | ||||||
|  |  | ||||||
|  |   @override | ||||||
|  |   Widget build(BuildContext context) { | ||||||
|  |     List<EntityWrapper> entitiesToShow = badges.getEntitiesToShow(); | ||||||
|  |      | ||||||
|  |     if (entitiesToShow.isNotEmpty) { | ||||||
|  |       return Wrap( | ||||||
|  |         alignment: WrapAlignment.center, | ||||||
|  |         spacing: 10.0, | ||||||
|  |         runSpacing: 1.0, | ||||||
|  |         children: entitiesToShow.map((entity) => | ||||||
|  |             EntityModel( | ||||||
|  |               entityWrapper: entity, | ||||||
|  |               child: BadgeWidget(), | ||||||
|  |               handleTap: true, | ||||||
|  |             )).toList(), | ||||||
|  |       ); | ||||||
|  |     } | ||||||
|  |     return Container(height: 0.0, width: 0.0,); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |    | ||||||
|  | } | ||||||
| @@ -75,6 +75,9 @@ class CardData { | |||||||
|           case CardType.MEDIA_CONTROL: |           case CardType.MEDIA_CONTROL: | ||||||
|             return MediaControlCardData(rawData); |             return MediaControlCardData(rawData); | ||||||
|             break; |             break; | ||||||
|  |           case CardType.BADGES: | ||||||
|  |             return BadgesData(rawData); | ||||||
|  |             break; | ||||||
|           default: |           default: | ||||||
|             return CardData(null); |             return CardData(null); | ||||||
|         } |         } | ||||||
| @@ -172,6 +175,64 @@ class CardData { | |||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | class BadgesData extends CardData { | ||||||
|  |  | ||||||
|  |   String title; | ||||||
|  |   String icon; | ||||||
|  |   bool showHeaderToggle; | ||||||
|  |  | ||||||
|  |   @override | ||||||
|  |   Widget buildCardWidget() { | ||||||
|  |     return Badges(badges: this); | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   BadgesData(rawData) : super(rawData) { | ||||||
|  |     if (rawData['badges'] is List) { | ||||||
|  |       rawData['badges'].forEach((dynamic rawBadge) { | ||||||
|  |         if (rawBadge is String && HomeAssistant().entities.isExist(rawBadge)) {   | ||||||
|  |           entities.add(EntityWrapper(entity: HomeAssistant().entities.get(rawBadge))); | ||||||
|  |         } else if (rawBadge is Map && rawBadge.containsKey('entity') && HomeAssistant().entities.isExist(rawBadge['entity'])) { | ||||||
|  |           entities.add( | ||||||
|  |             EntityWrapper( | ||||||
|  |               entity: HomeAssistant().entities.get(rawBadge['entity']), | ||||||
|  |               overrideName: rawBadge["name"], | ||||||
|  |               overrideIcon: rawBadge["icon"], | ||||||
|  |             ) | ||||||
|  |           ); | ||||||
|  |         } else if (rawBadge is Map && rawBadge.containsKey('entities')) { | ||||||
|  |           _parseEntities(rawBadge); | ||||||
|  |         } | ||||||
|  |       });     | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   void _parseEntities(rawData) { | ||||||
|  |     var rawEntities = rawData['entities'] ?? []; | ||||||
|  |     rawEntities.forEach((rawEntity) { | ||||||
|  |       if (rawEntity is String) { | ||||||
|  |         if (HomeAssistant().entities.isExist(rawEntity)) { | ||||||
|  |           entities.add(EntityWrapper( | ||||||
|  |             entity: HomeAssistant().entities.get(rawEntity), | ||||||
|  |             stateFilter: rawData['state_filter'] ?? [], | ||||||
|  |           )); | ||||||
|  |         } | ||||||
|  |       } else if (HomeAssistant().entities.isExist('${rawEntity['entity']}')) { | ||||||
|  |         Entity e = HomeAssistant().entities.get(rawEntity["entity"]); | ||||||
|  |         entities.add( | ||||||
|  |           EntityWrapper( | ||||||
|  |               entity: e, | ||||||
|  |               overrideName: rawEntity["name"], | ||||||
|  |               overrideIcon: rawEntity["icon"], | ||||||
|  |               stateFilter: rawEntity['state_filter'] ?? (rawData['state_filter'] ?? []), | ||||||
|  |               uiAction: EntityUIAction(rawEntityData: rawEntity) | ||||||
|  |           ) | ||||||
|  |         ); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
| class EntitiesCardData extends CardData { | class EntitiesCardData extends CardData { | ||||||
|  |  | ||||||
|   String title; |   String title; | ||||||
|   | |||||||
| @@ -63,6 +63,7 @@ class CardType { | |||||||
|   static const UNKNOWN = "unknown"; |   static const UNKNOWN = "unknown"; | ||||||
|   static const HISTORY_GRAPH = "history-graph"; |   static const HISTORY_GRAPH = "history-graph"; | ||||||
|   static const PICTURE_GLANCE = "picture-glance"; |   static const PICTURE_GLANCE = "picture-glance"; | ||||||
|  |   static const BADGES = "badges"; | ||||||
| } | } | ||||||
|  |  | ||||||
| class Sizes { | class Sizes { | ||||||
|   | |||||||
| @@ -4,7 +4,6 @@ class BadgeWidget extends StatelessWidget { | |||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     final entityModel = EntityModel.of(context); |     final entityModel = EntityModel.of(context); | ||||||
|     double iconSize = 26.0; |  | ||||||
|     Widget badgeIcon; |     Widget badgeIcon; | ||||||
|     String onBadgeTextValue; |     String onBadgeTextValue; | ||||||
|     Color iconColor = HAClientTheme().getBadgeColor(entityModel.entityWrapper.entity.domain); |     Color iconColor = HAClientTheme().getBadgeColor(entityModel.entityWrapper.entity.domain); | ||||||
| @@ -14,11 +13,9 @@ class BadgeWidget extends StatelessWidget { | |||||||
|           badgeIcon = entityModel.entityWrapper.entity.state == "below_horizon" |           badgeIcon = entityModel.entityWrapper.entity.state == "below_horizon" | ||||||
|               ? Icon( |               ? Icon( | ||||||
|             MaterialDesignIcons.getIconDataFromIconCode(0xf0dc), |             MaterialDesignIcons.getIconDataFromIconCode(0xf0dc), | ||||||
|             size: iconSize, |  | ||||||
|           ) |           ) | ||||||
|               : Icon( |               : Icon( | ||||||
|             MaterialDesignIcons.getIconDataFromIconCode(0xf5a8), |             MaterialDesignIcons.getIconDataFromIconCode(0xf5a8), | ||||||
|             size: iconSize, |  | ||||||
|           ); |           ); | ||||||
|           break; |           break; | ||||||
|         } |         } | ||||||
| @@ -28,7 +25,6 @@ class BadgeWidget extends StatelessWidget { | |||||||
|         { |         { | ||||||
|           badgeIcon = EntityIcon( |           badgeIcon = EntityIcon( | ||||||
|             padding: EdgeInsets.all(0.0), |             padding: EdgeInsets.all(0.0), | ||||||
|             size: iconSize, |  | ||||||
|             color: Theme.of(context).textTheme.body1.color |             color: Theme.of(context).textTheme.body1.color | ||||||
|           ); |           ); | ||||||
|           break; |           break; | ||||||
| @@ -38,7 +34,6 @@ class BadgeWidget extends StatelessWidget { | |||||||
|         { |         { | ||||||
|           badgeIcon = EntityIcon( |           badgeIcon = EntityIcon( | ||||||
|               padding: EdgeInsets.all(0.0), |               padding: EdgeInsets.all(0.0), | ||||||
|               size: iconSize, |  | ||||||
|               color: Theme.of(context).textTheme.body1.color |               color: Theme.of(context).textTheme.body1.color | ||||||
|           ); |           ); | ||||||
|           onBadgeTextValue = entityModel.entityWrapper.entity.displayState; |           onBadgeTextValue = entityModel.entityWrapper.entity.displayState; | ||||||
| @@ -46,27 +41,13 @@ class BadgeWidget extends StatelessWidget { | |||||||
|         } |         } | ||||||
|       default: |       default: | ||||||
|         { |         { | ||||||
|           double stateFontSize; |  | ||||||
|           if (entityModel.entityWrapper.entity.displayState.length <= 3) { |  | ||||||
|             stateFontSize = 18.0; |  | ||||||
|           } else if (entityModel.entityWrapper.entity.displayState.length <= 4) { |  | ||||||
|             stateFontSize = 15.0; |  | ||||||
|           } else if (entityModel.entityWrapper.entity.displayState.length <= 6) { |  | ||||||
|             stateFontSize = 10.0; |  | ||||||
|           } else if (entityModel.entityWrapper.entity.displayState.length <= 10) { |  | ||||||
|             stateFontSize = 8.0; |  | ||||||
|           } |  | ||||||
|           onBadgeTextValue = entityModel.entityWrapper.unitOfMeasurement; |           onBadgeTextValue = entityModel.entityWrapper.unitOfMeasurement; | ||||||
|           badgeIcon = Center( |           badgeIcon = Text( | ||||||
|             child: Text( |             "${entityModel.entityWrapper.entity.displayState}", | ||||||
|               "${entityModel.entityWrapper.entity.displayState}", |             overflow: TextOverflow.fade, | ||||||
|               overflow: TextOverflow.fade, |             softWrap: false, | ||||||
|               softWrap: false, |             textAlign: TextAlign.center, | ||||||
|               textAlign: TextAlign.center, |             style: Theme.of(context).textTheme.body1 | ||||||
|               style: Theme.of(context).textTheme.body1.copyWith( |  | ||||||
|                 fontSize: stateFontSize |  | ||||||
|               ) |  | ||||||
|             ), |  | ||||||
|           ); |           ); | ||||||
|           break; |           break; | ||||||
|         } |         } | ||||||
| @@ -85,8 +66,6 @@ class BadgeWidget extends StatelessWidget { | |||||||
|               softWrap: false, |               softWrap: false, | ||||||
|               overflow: TextOverflow.fade), |               overflow: TextOverflow.fade), | ||||||
|           decoration: new BoxDecoration( |           decoration: new BoxDecoration( | ||||||
|             // Circle shape |  | ||||||
|             //shape: BoxShape.circle, |  | ||||||
|             color: iconColor, |             color: iconColor, | ||||||
|             borderRadius: BorderRadius.circular(9.0), |             borderRadius: BorderRadius.circular(9.0), | ||||||
|           )); |           )); | ||||||
| @@ -95,9 +74,9 @@ class BadgeWidget extends StatelessWidget { | |||||||
|         child: Column( |         child: Column( | ||||||
|           children: <Widget>[ |           children: <Widget>[ | ||||||
|             Container( |             Container( | ||||||
|               margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), |               //margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), | ||||||
|               width: 50.0, |               width: 45, | ||||||
|               height: 50.0, |               height: 45, | ||||||
|               decoration: new BoxDecoration( |               decoration: new BoxDecoration( | ||||||
|                 // Circle shape |                 // Circle shape | ||||||
|                 shape: BoxShape.circle, |                 shape: BoxShape.circle, | ||||||
| @@ -112,20 +91,24 @@ class BadgeWidget extends StatelessWidget { | |||||||
|                 overflow: Overflow.visible, |                 overflow: Overflow.visible, | ||||||
|                 children: <Widget>[ |                 children: <Widget>[ | ||||||
|                   Positioned( |                   Positioned( | ||||||
|                     width: 46.0, |                     width: 45, | ||||||
|                     height: 46.0, |                     height: 45, | ||||||
|                     top: 0.0, |                     top: 0.0, | ||||||
|                     left: 0.0, |                     left: 0.0, | ||||||
|                     child: badgeIcon, |                     child: FittedBox( | ||||||
|  |                       fit: BoxFit.contain, | ||||||
|  |                       alignment: Alignment.center, | ||||||
|  |                       child: badgeIcon, | ||||||
|  |                     ), | ||||||
|                   ), |                   ), | ||||||
|                   Positioned( |                   Positioned( | ||||||
|                     //width: 50.0, |                     bottom: -9.0, | ||||||
|                       bottom: -9.0, |                     left: -10.0, | ||||||
|                       left: -10.0, |                     right: -10.0, | ||||||
|                       right: -10.0, |                     child: Center( | ||||||
|                       child: Center( |                       child: onBadgeText, | ||||||
|                         child: onBadgeText, |                     ) | ||||||
|                       )) |                   ) | ||||||
|                 ], |                 ], | ||||||
|               ), |               ), | ||||||
|             ), |             ), | ||||||
| @@ -142,7 +125,9 @@ class BadgeWidget extends StatelessWidget { | |||||||
|             ), |             ), | ||||||
|           ], |           ], | ||||||
|         ), |         ), | ||||||
|         onTap: () => |         onTap: () => entityModel.entityWrapper.handleTap(), | ||||||
|             eventBus.fire(new ShowEntityPageEvent(entityId: entityModel.entityWrapper.entity.entityId))); |         onDoubleTap: () => entityModel.entityWrapper.handleDoubleTap(), | ||||||
|  |         onLongPress: () => entityModel.entityWrapper.handleHold(), | ||||||
|  |     ); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -210,14 +210,6 @@ class Entity { | |||||||
|     ); |     ); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Widget buildBadgeWidget(BuildContext context) { |  | ||||||
|     return EntityModel( |  | ||||||
|       entityWrapper: EntityWrapper(entity: this), |  | ||||||
|       child: BadgeWidget(), |  | ||||||
|       handleTap: true, |  | ||||||
|     ); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   String getAttribute(String attributeName) { |   String getAttribute(String attributeName) { | ||||||
|     if (attributes != null) { |     if (attributes != null) { | ||||||
|       return attributes["$attributeName"].toString(); |       return attributes["$attributeName"].toString(); | ||||||
|   | |||||||
| @@ -153,6 +153,7 @@ part 'entities/media_player/widgets/media_player_progress_bar.widget.dart'; | |||||||
| part 'pages/whats_new.page.dart'; | part 'pages/whats_new.page.dart'; | ||||||
| part 'pages/fullscreen.page.dart'; | part 'pages/fullscreen.page.dart'; | ||||||
| part 'popups.dart'; | part 'popups.dart'; | ||||||
|  | part 'cards/badges.dart'; | ||||||
|  |  | ||||||
| EventBus eventBus = new EventBus(); | EventBus eventBus = new EventBus(); | ||||||
| final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); | final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ part of 'main.dart'; | |||||||
|  |  | ||||||
| class HAView { | class HAView { | ||||||
|   List<CardData> cards = []; |   List<CardData> cards = []; | ||||||
|   List<Entity> badges = []; |  | ||||||
|   Entity linkedEntity; |   Entity linkedEntity; | ||||||
|   String name; |   String name; | ||||||
|   String id; |   String id; | ||||||
| @@ -16,28 +15,16 @@ class HAView { | |||||||
|     iconName = rawData['icon']; |     iconName = rawData['icon']; | ||||||
|     isPanel = rawData['panel'] ?? false; |     isPanel = rawData['panel'] ?? false; | ||||||
|  |  | ||||||
|     if (rawData['badges'] != null && rawData['badges'] is List) { |     if (rawData['badges'] != null && !isPanel) { | ||||||
|         rawData['badges'].forEach((entity) { |         cards.add(CardData.parse({ | ||||||
|           if (entity is String) { |           'type': CardType.BADGES, | ||||||
|             if (HomeAssistant().entities.isExist(entity)) { |           'badges': rawData['badges'] | ||||||
|               Entity e = HomeAssistant().entities.get(entity); |         })); | ||||||
|               badges.add(e); |  | ||||||
|             } |  | ||||||
|           } else { |  | ||||||
|             String eId = '${entity['entity']}'; |  | ||||||
|             if (HomeAssistant().entities.isExist(eId)) { |  | ||||||
|               Entity e = HomeAssistant().entities.get(eId); |  | ||||||
|               badges.add(e); |  | ||||||
|             } |  | ||||||
|           } |  | ||||||
|         }); |  | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       (rawData["cards"] ?? []).forEach((rawCardData) { |       (rawData['cards'] ?? []).forEach((rawCardData) { | ||||||
|         cards.add(CardData.parse(rawCardData)); |         cards.add(CardData.parse(rawCardData)); | ||||||
|       }); |       }); | ||||||
|  |  | ||||||
|       //cards.addAll(_createLovelaceCards(rawData["cards"] ?? [], 1)); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Widget buildTab() { |   Widget buildTab() { | ||||||
|   | |||||||
| @@ -44,13 +44,10 @@ class ViewWidget extends StatelessWidget { | |||||||
|       } else { |       } else { | ||||||
|         cardsContainer = Container(); |         cardsContainer = Container(); | ||||||
|       } |       } | ||||||
|       return ListView( |       return SingleChildScrollView( | ||||||
|           shrinkWrap: true, |           scrollDirection: Axis.vertical, | ||||||
|           padding: EdgeInsets.all(0), |           padding: EdgeInsets.all(0), | ||||||
|           children: <Widget>[ |           child: cardsContainer | ||||||
|             _buildBadges(context), |  | ||||||
|             cardsContainer |  | ||||||
|           ] |  | ||||||
|       ); |       ); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| @@ -63,18 +60,4 @@ class ViewWidget extends StatelessWidget { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   Widget _buildBadges(BuildContext context) { |  | ||||||
|     if (this.view.badges.isNotEmpty) { |  | ||||||
|       return Wrap( |  | ||||||
|         alignment: WrapAlignment.center, |  | ||||||
|         spacing: 10.0, |  | ||||||
|         runSpacing: 1.0, |  | ||||||
|         children: this.view.badges.map((badge) => |  | ||||||
|             badge.buildBadgeWidget(context)).toList(), |  | ||||||
|       ); |  | ||||||
|     } else { |  | ||||||
|       return Container(width: 0, height: 0,); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|  |  | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user