From 9ad6d92ccdecad20406fbdb2414049b297145c43 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Sun, 21 Oct 2018 14:43:52 +0300 Subject: [PATCH] View entities in entityCollection. Child entities in parse --- lib/entity_collection.class.dart | 40 ++++++++++++++++++-------------- lib/home_assistant.class.dart | 2 +- lib/main.dart | 6 ++--- lib/view_builder.class.dart | 21 ++--------------- 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/lib/entity_collection.class.dart b/lib/entity_collection.class.dart index 0893848..0b580a1 100644 --- a/lib/entity_collection.class.dart +++ b/lib/entity_collection.class.dart @@ -2,28 +2,32 @@ part of 'main.dart'; class EntityCollection { - Map _entities; - List viewList; + Map _allEntities; + Map views; - bool get isEmpty => _entities.isEmpty; + bool get isEmpty => _allEntities.isEmpty; EntityCollection() { - _entities = {}; - viewList = []; + _allEntities = {}; + views = {}; } - bool get hasDefaultView => _entities["group.default_view"] != null; + bool get hasDefaultView => _allEntities["group.default_view"] != null; void parse(List rawData) { - _entities.clear(); - viewList.clear(); + _allEntities.clear(); + views.clear(); TheLogger.log("Debug","Parsing ${rawData.length} Home Assistant entities"); rawData.forEach((rawEntityData) { - Entity newEntity = addFromRaw(rawEntityData); - - if (newEntity.isView) { - viewList.add(newEntity.entityId); + addFromRaw(rawEntityData); + }); + _allEntities.forEach((entityId, entity){ + if ((entity.isGroup) && (entity.childEntityIds != null)) { + entity.childEntities = getAll(entity.childEntityIds); + } + if (entity.isView) { + views[entityId] = entity; } }); } @@ -78,12 +82,12 @@ class EntityCollection { } void add(Entity entity) { - _entities[entity.entityId] = entity; + _allEntities[entity.entityId] = entity; } Entity addFromRaw(Map rawEntityData) { Entity entity = _createEntityInstance(rawEntityData); - _entities[entity.entityId] = entity; + _allEntities[entity.entityId] = entity; return entity; } @@ -92,7 +96,7 @@ class EntityCollection { } Entity get(String entityId) { - return _entities[entityId]; + return _allEntities[entityId]; } List getAll(List ids) { @@ -107,13 +111,13 @@ class EntityCollection { } bool isExist(String entityId) { - return _entities[entityId] != null; + return _allEntities[entityId] != null; } Map> getDefaultViewTopLevelEntities() { Map> result = {"userGroups": [], "notGroupedEntities": []}; List entities = []; - _entities.forEach((id, entity){ + _allEntities.forEach((id, entity){ if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) { result["userGroups"].add(id); } @@ -125,7 +129,7 @@ class EntityCollection { entities.forEach((entiyId) { bool foundInGroup = false; result["userGroups"].forEach((userGroupId) { - if (_entities[userGroupId].childEntityIds.contains(entiyId)) { + if (_allEntities[userGroupId].childEntityIds.contains(entiyId)) { foundInGroup = true; } }); diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index d8a9fd2..affcf51 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -38,7 +38,7 @@ class HomeAssistant { String get locationName => _instanceConfig["location_name"] ?? ""; String get userName => _userName ?? locationName; String get userAvatarText => userName.length > 0 ? userName[0] : ""; - int get viewsCount => _entities.viewList.length ?? 0; + int get viewsCount => _entities.views.length ?? 0; EntityCollection get entities => _entities; diff --git a/lib/main.dart b/lib/main.dart index fe3982c..205d058 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -207,6 +207,7 @@ class _MainPageState extends State with WidgetsBindingObserver { //_instanceConfig = _homeAssistant.instanceConfig; _entities = _homeAssistant.entities; _uiViewsCount = _homeAssistant.viewsCount; + TheLogger.log("Debug","_uiViewsCount=$_uiViewsCount"); _isLoading = 0; }); }).catchError((e) { @@ -239,7 +240,6 @@ class _MainPageState extends State with WidgetsBindingObserver { } List buildUIViewTabs() { - //TODO move somewhere to ViewBuilder List result = []; if (!_entities.isEmpty) { if (!_entities.hasDefaultView) { @@ -253,10 +253,10 @@ class _MainPageState extends State with WidgetsBindingObserver { ) ); } - _entities.viewList.forEach((viewId) { + _entities.views.forEach((viewId, groupEntity) { result.add( Tab( - icon: MaterialDesignIcons.createIconWidgetFromEntityData(_entities.get(viewId), 24.0, null) ?? + icon: MaterialDesignIcons.createIconWidgetFromEntityData(groupEntity, 24.0, null) ?? Icon( MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"), size: 24.0, diff --git a/lib/view_builder.class.dart b/lib/view_builder.class.dart index 1f6ed36..766e1d2 100644 --- a/lib/view_builder.class.dart +++ b/lib/view_builder.class.dart @@ -32,9 +32,6 @@ class ViewBuilder{ List entitiesForView = []; userGroupsList["userGroups"].forEach((groupId){ Entity en = entityCollection.get(groupId); - if (en.isGroup) { - en.childEntities = entityCollection.getAll(en.childEntityIds); - } entitiesForView.add(en); }); userGroupsList["notGroupedEntities"].forEach((entityId){ @@ -49,26 +46,12 @@ class ViewBuilder{ List _composeViews() { List result = []; int counter = 0; - entityCollection.viewList.forEach((viewId) { + entityCollection.views.forEach((viewId, viewGroupEntity) { counter += 1; //try { - Entity viewGroupEntity = entityCollection.get(viewId); - List entitiesForView = []; - viewGroupEntity.childEntityIds.forEach(( - entityId) { //Each entity or group in view - if (entityCollection.isExist(entityId)) { - Entity en = entityCollection.get(entityId); - if (en.isGroup) { - en.childEntities = entityCollection.getAll(en.childEntityIds); - } - entitiesForView.add(en); - } else { - TheLogger.log("Warning", "Unknown entity inside view: $entityId"); - } - }); result.add(View( count: counter, - entities: entitiesForView + entities: viewGroupEntity.childEntities )); /*} catch (error) { TheLogger.log("Error","Error parsing view: $viewId");