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 { class EntityCollection {
Map<String, Entity> _entities; Map<String, Entity> _allEntities;
List<String> viewList; Map<String, Entity> views;
bool get isEmpty => _entities.isEmpty; bool get isEmpty => _allEntities.isEmpty;
EntityCollection() { EntityCollection() {
_entities = {}; _allEntities = {};
viewList = []; views = {};
} }
bool get hasDefaultView => _entities["group.default_view"] != null; bool get hasDefaultView => _allEntities["group.default_view"] != null;
void parse(List rawData) { void parse(List rawData) {
_entities.clear(); _allEntities.clear();
viewList.clear(); views.clear();
TheLogger.log("Debug","Parsing ${rawData.length} Home Assistant entities"); TheLogger.log("Debug","Parsing ${rawData.length} Home Assistant entities");
rawData.forEach((rawEntityData) { rawData.forEach((rawEntityData) {
Entity newEntity = addFromRaw(rawEntityData); addFromRaw(rawEntityData);
});
if (newEntity.isView) { _allEntities.forEach((entityId, entity){
viewList.add(newEntity.entityId); 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) { void add(Entity entity) {
_entities[entity.entityId] = entity; _allEntities[entity.entityId] = entity;
} }
Entity addFromRaw(Map rawEntityData) { Entity addFromRaw(Map rawEntityData) {
Entity entity = _createEntityInstance(rawEntityData); Entity entity = _createEntityInstance(rawEntityData);
_entities[entity.entityId] = entity; _allEntities[entity.entityId] = entity;
return entity; return entity;
} }
@ -92,7 +96,7 @@ class EntityCollection {
} }
Entity get(String entityId) { Entity get(String entityId) {
return _entities[entityId]; return _allEntities[entityId];
} }
List<Entity> getAll(List ids) { List<Entity> getAll(List ids) {
@ -107,13 +111,13 @@ class EntityCollection {
} }
bool isExist(String entityId) { bool isExist(String entityId) {
return _entities[entityId] != null; return _allEntities[entityId] != null;
} }
Map<String,List<String>> getDefaultViewTopLevelEntities() { Map<String,List<String>> getDefaultViewTopLevelEntities() {
Map<String,List<String>> result = {"userGroups": [], "notGroupedEntities": []}; Map<String,List<String>> result = {"userGroups": [], "notGroupedEntities": []};
List<String> entities = []; List<String> entities = [];
_entities.forEach((id, entity){ _allEntities.forEach((id, entity){
if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) { if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) {
result["userGroups"].add(id); result["userGroups"].add(id);
} }
@ -125,7 +129,7 @@ class EntityCollection {
entities.forEach((entiyId) { entities.forEach((entiyId) {
bool foundInGroup = false; bool foundInGroup = false;
result["userGroups"].forEach((userGroupId) { result["userGroups"].forEach((userGroupId) {
if (_entities[userGroupId].childEntityIds.contains(entiyId)) { if (_allEntities[userGroupId].childEntityIds.contains(entiyId)) {
foundInGroup = true; foundInGroup = true;
} }
}); });

View File

@ -38,7 +38,7 @@ class HomeAssistant {
String get locationName => _instanceConfig["location_name"] ?? ""; String get locationName => _instanceConfig["location_name"] ?? "";
String get userName => _userName ?? locationName; String get userName => _userName ?? locationName;
String get userAvatarText => userName.length > 0 ? userName[0] : ""; 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; EntityCollection get entities => _entities;

View File

@ -207,6 +207,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
//_instanceConfig = _homeAssistant.instanceConfig; //_instanceConfig = _homeAssistant.instanceConfig;
_entities = _homeAssistant.entities; _entities = _homeAssistant.entities;
_uiViewsCount = _homeAssistant.viewsCount; _uiViewsCount = _homeAssistant.viewsCount;
TheLogger.log("Debug","_uiViewsCount=$_uiViewsCount");
_isLoading = 0; _isLoading = 0;
}); });
}).catchError((e) { }).catchError((e) {
@ -239,7 +240,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
} }
List<Tab> buildUIViewTabs() { List<Tab> buildUIViewTabs() {
//TODO move somewhere to ViewBuilder
List<Tab> result = []; List<Tab> result = [];
if (!_entities.isEmpty) { if (!_entities.isEmpty) {
if (!_entities.hasDefaultView) { 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( result.add(
Tab( Tab(
icon: MaterialDesignIcons.createIconWidgetFromEntityData(_entities.get(viewId), 24.0, null) ?? icon: MaterialDesignIcons.createIconWidgetFromEntityData(groupEntity, 24.0, null) ??
Icon( Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"), MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"),
size: 24.0, size: 24.0,

View File

@ -32,9 +32,6 @@ class ViewBuilder{
List<Entity> entitiesForView = []; List<Entity> entitiesForView = [];
userGroupsList["userGroups"].forEach((groupId){ userGroupsList["userGroups"].forEach((groupId){
Entity en = entityCollection.get(groupId); Entity en = entityCollection.get(groupId);
if (en.isGroup) {
en.childEntities = entityCollection.getAll(en.childEntityIds);
}
entitiesForView.add(en); entitiesForView.add(en);
}); });
userGroupsList["notGroupedEntities"].forEach((entityId){ userGroupsList["notGroupedEntities"].forEach((entityId){
@ -49,26 +46,12 @@ class ViewBuilder{
List<View> _composeViews() { List<View> _composeViews() {
List<View> result = []; List<View> result = [];
int counter = 0; int counter = 0;
entityCollection.viewList.forEach((viewId) { entityCollection.views.forEach((viewId, viewGroupEntity) {
counter += 1; counter += 1;
//try { //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( result.add(View(
count: counter, count: counter,
entities: entitiesForView entities: viewGroupEntity.childEntities
)); ));
/*} catch (error) { /*} catch (error) {
TheLogger.log("Error","Error parsing view: $viewId"); TheLogger.log("Error","Error parsing view: $viewId");