From af3a5bc611f1000128acbca0e4e2a9e35cbb87ba Mon Sep 17 00:00:00 2001 From: estevez Date: Fri, 28 Sep 2018 13:33:15 +0300 Subject: [PATCH] Resolves #70 Build default_view automatically --- lib/entity_collection.class.dart | 29 +++++++++++++++++++++++++++++ lib/main.dart | 31 ++++++++++++++++++------------- lib/mdi.class.dart | 5 ++++- lib/ui_builder_class.dart | 17 +++++++++++++++++ 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/lib/entity_collection.class.dart b/lib/entity_collection.class.dart index e587eb4..0ea32dd 100644 --- a/lib/entity_collection.class.dart +++ b/lib/entity_collection.class.dart @@ -10,6 +10,8 @@ class EntityCollection { viewList = []; } + bool get hasDefaultView => _entities["group.default_view"] != null; + void parse(List rawData) { _entities.clear(); viewList.clear(); @@ -55,4 +57,31 @@ class EntityCollection { return _entities[entityId] != null; } + Map> getDefaultViewTopLevelEntities() { + Map> result = {"userGroups": [], "notGroupedEntities": []}; + List entities = []; + _entities.forEach((id, entity){ + if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) { + result["userGroups"].add(id); + } + if (!entity.isGroup) { + entities.add(id); + } + }); + + entities.forEach((entiyId) { + bool foundInGroup = false; + result["userGroups"].forEach((userGroupId) { + if (_entities[userGroupId].childEntities.contains(entiyId)) { + foundInGroup = true; + } + }); + if (!foundInGroup) { + result["notGroupedEntities"].add(entiyId); + } + }); + + return result; + } + } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 85f2cf1..9680479 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -214,8 +214,8 @@ class _MainPageState extends State with WidgetsBindingObserver { result.add( Wrap( alignment: WrapAlignment.center, - spacing: 12.0, - runSpacing: 4.0, + spacing: 10.0, + runSpacing: 1.0, children: _buildBadges(view.badges), ) ); @@ -265,32 +265,32 @@ class _MainPageState extends State with WidgetsBindingObserver { badgeTextValue = data.unitOfMeasurement; badgeIcon = Center( child: Text( - "${data.state}", + "${data.state == 'unknown' ? '-' : data.state}", overflow: TextOverflow.fade, softWrap: false, textAlign: TextAlign.center, - style: TextStyle(fontSize: 18.0), + style: TextStyle(fontSize: 17.0), ), ); break; } case "device_tracker": { - badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, iconSize,Colors.black); + badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(data, iconSize,Colors.black); badgeTextValue = data.state; break; } default: { - badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, iconSize,Colors.black); + badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(data, iconSize,Colors.black); } } Widget badgeText; - if (badgeTextValue == null) { + if (badgeTextValue == null || badgeTextValue.length == 0) { badgeText = Container(width: 0.0, height: 0.0); } else { badgeText = Container( padding: EdgeInsets.fromLTRB(6.0, 2.0, 6.0, 2.0), child: Text("$badgeTextValue", - style: TextStyle(fontSize: 13.0, color: Colors.white), + style: TextStyle(fontSize: 12.0, color: Colors.white), textAlign: TextAlign.center, softWrap: false, overflow: TextOverflow.fade), decoration: new BoxDecoration( // Circle shape @@ -329,8 +329,8 @@ class _MainPageState extends State with WidgetsBindingObserver { Positioned( //width: 50.0, bottom: -9.0, - left: -15.0, - right: -15.0, + left: -10.0, + right: -10.0, child: Center( child: badgeText, ) @@ -343,8 +343,9 @@ class _MainPageState extends State with WidgetsBindingObserver { child: Text( "${data.displayName}", textAlign: TextAlign.center, + style: TextStyle(fontSize: 12.0), softWrap: true, - maxLines: 2, + maxLines: 3, overflow: TextOverflow.ellipsis, ), ), @@ -386,7 +387,7 @@ class _MainPageState extends State with WidgetsBindingObserver { var data = _entities.get(id); if (data != null) { entities.add(new ListTile( - leading: MaterialDesignIcons.createIconFromEntityData(data, 28.0, _stateIconColors[data.state] ?? Colors.blueGrey), + leading: MaterialDesignIcons.createIconWidgetFromEntityData(data, 28.0, _stateIconColors[data.state] ?? Colors.blueGrey), //subtitle: Text("${data['entity_id']}"), trailing: _buildEntityActionWidget(data), title: Text( @@ -468,7 +469,11 @@ class _MainPageState extends State with WidgetsBindingObserver { _homeAssistant.uiBuilder.views.forEach((viewId, view) { result.add( Tab( - icon: MaterialDesignIcons.createIconFromEntityData(_entities.get(viewId), 24.0, null) + icon: MaterialDesignIcons.createIconWidgetFromEntityData(_entities.get(viewId), 24.0, null) ?? + Icon( + MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"), + size: 24.0, + ) ) ); }); diff --git a/lib/mdi.class.dart b/lib/mdi.class.dart index 5aecc00..3bd9c1c 100644 --- a/lib/mdi.class.dart +++ b/lib/mdi.class.dart @@ -2868,7 +2868,10 @@ class MaterialDesignIcons { "mdi:blank": 0xf68c }; - static Widget createIconFromEntityData(Entity data, double size, Color color) { + static Widget createIconWidgetFromEntityData(Entity data, double size, Color color) { + if (data == null) { + return null; + } if (data.entityPicture != null) { if (homeAssistantWebHost != null) { return CircleAvatar( diff --git a/lib/ui_builder_class.dart b/lib/ui_builder_class.dart index 949628c..1491914 100644 --- a/lib/ui_builder_class.dart +++ b/lib/ui_builder_class.dart @@ -19,9 +19,26 @@ class UIBuilder { void build(EntityCollection entitiesCollection) { _entities = entitiesCollection; _views.clear(); + if (!_entities.hasDefaultView) { + _createDefaultView(); + } _createViews(entitiesCollection.viewList); } + void _createDefaultView() { + Map> userGroupsList = _entities.getDefaultViewTopLevelEntities(); + TheLogger.log("RESULT", "${userGroupsList["userGroups"]}"); + TheLogger.log("RESULT", "${userGroupsList["notGroupedEntities"]}"); + View view = View("group.default_view", 0); + userGroupsList["userGroups"].forEach((groupId){ + view.add(_entities.get(groupId)); + }); + userGroupsList["notGroupedEntities"].forEach((entityId){ + view.add(_entities.get(entityId)); + }); + _views["group.default_view"] = view; + } + void _createViews(List viewsList) { int counter = 0; viewsList.forEach((viewId) {