View entities in entityCollection. Child entities in parse

This commit is contained in:
Yegor Vialov 2018-10-21 14:43:52 +03:00
parent fafa8f43f4
commit 9ad6d92ccd
4 changed files with 28 additions and 41 deletions

View File

@ -2,28 +2,32 @@ part of 'main.dart';
class EntityCollection {
Map<String, Entity> _entities;
List<String> viewList;
Map<String, Entity> _allEntities;
Map<String, Entity> 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<Entity> getAll(List ids) {
@ -107,13 +111,13 @@ class EntityCollection {
}
bool isExist(String entityId) {
return _entities[entityId] != null;
return _allEntities[entityId] != null;
}
Map<String,List<String>> getDefaultViewTopLevelEntities() {
Map<String,List<String>> result = {"userGroups": [], "notGroupedEntities": []};
List<String> 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;
}
});

View File

@ -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;

View File

@ -207,6 +207,7 @@ class _MainPageState extends State<MainPage> 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<MainPage> with WidgetsBindingObserver {
}
List<Tab> buildUIViewTabs() {
//TODO move somewhere to ViewBuilder
List<Tab> result = [];
if (!_entities.isEmpty) {
if (!_entities.hasDefaultView) {
@ -253,10 +253,10 @@ class _MainPageState extends State<MainPage> 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,

View File

@ -32,9 +32,6 @@ class ViewBuilder{
List<Entity> 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<View> _composeViews() {
List<View> result = [];
int counter = 0;
entityCollection.viewList.forEach((viewId) {
entityCollection.views.forEach((viewId, viewGroupEntity) {
counter += 1;
//try {
Entity viewGroupEntity = entityCollection.get(viewId);
List<Entity> 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");