Resolves #70 Build default_view automatically
This commit is contained in:
parent
b935a0e372
commit
af3a5bc611
@ -10,6 +10,8 @@ class EntityCollection {
|
|||||||
viewList = [];
|
viewList = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get hasDefaultView => _entities["group.default_view"] != null;
|
||||||
|
|
||||||
void parse(List rawData) {
|
void parse(List rawData) {
|
||||||
_entities.clear();
|
_entities.clear();
|
||||||
viewList.clear();
|
viewList.clear();
|
||||||
@ -55,4 +57,31 @@ class EntityCollection {
|
|||||||
return _entities[entityId] != null;
|
return _entities[entityId] != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String,List<String>> getDefaultViewTopLevelEntities() {
|
||||||
|
Map<String,List<String>> result = {"userGroups": [], "notGroupedEntities": []};
|
||||||
|
List<String> 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -214,8 +214,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
result.add(
|
result.add(
|
||||||
Wrap(
|
Wrap(
|
||||||
alignment: WrapAlignment.center,
|
alignment: WrapAlignment.center,
|
||||||
spacing: 12.0,
|
spacing: 10.0,
|
||||||
runSpacing: 4.0,
|
runSpacing: 1.0,
|
||||||
children: _buildBadges(view.badges),
|
children: _buildBadges(view.badges),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -265,32 +265,32 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
badgeTextValue = data.unitOfMeasurement;
|
badgeTextValue = data.unitOfMeasurement;
|
||||||
badgeIcon = Center(
|
badgeIcon = Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
"${data.state}",
|
"${data.state == 'unknown' ? '-' : data.state}",
|
||||||
overflow: TextOverflow.fade,
|
overflow: TextOverflow.fade,
|
||||||
softWrap: false,
|
softWrap: false,
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
style: TextStyle(fontSize: 18.0),
|
style: TextStyle(fontSize: 17.0),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "device_tracker": {
|
case "device_tracker": {
|
||||||
badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, iconSize,Colors.black);
|
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(data, iconSize,Colors.black);
|
||||||
badgeTextValue = data.state;
|
badgeTextValue = data.state;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
badgeIcon = MaterialDesignIcons.createIconFromEntityData(data, iconSize,Colors.black);
|
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(data, iconSize,Colors.black);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Widget badgeText;
|
Widget badgeText;
|
||||||
if (badgeTextValue == null) {
|
if (badgeTextValue == null || badgeTextValue.length == 0) {
|
||||||
badgeText = Container(width: 0.0, height: 0.0);
|
badgeText = Container(width: 0.0, height: 0.0);
|
||||||
} else {
|
} else {
|
||||||
badgeText = Container(
|
badgeText = Container(
|
||||||
padding: EdgeInsets.fromLTRB(6.0, 2.0, 6.0, 2.0),
|
padding: EdgeInsets.fromLTRB(6.0, 2.0, 6.0, 2.0),
|
||||||
child: Text("$badgeTextValue",
|
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),
|
textAlign: TextAlign.center, softWrap: false, overflow: TextOverflow.fade),
|
||||||
decoration: new BoxDecoration(
|
decoration: new BoxDecoration(
|
||||||
// Circle shape
|
// Circle shape
|
||||||
@ -329,8 +329,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
Positioned(
|
Positioned(
|
||||||
//width: 50.0,
|
//width: 50.0,
|
||||||
bottom: -9.0,
|
bottom: -9.0,
|
||||||
left: -15.0,
|
left: -10.0,
|
||||||
right: -15.0,
|
right: -10.0,
|
||||||
child: Center(
|
child: Center(
|
||||||
child: badgeText,
|
child: badgeText,
|
||||||
)
|
)
|
||||||
@ -343,8 +343,9 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
child: Text(
|
child: Text(
|
||||||
"${data.displayName}",
|
"${data.displayName}",
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(fontSize: 12.0),
|
||||||
softWrap: true,
|
softWrap: true,
|
||||||
maxLines: 2,
|
maxLines: 3,
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -386,7 +387,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
var data = _entities.get(id);
|
var data = _entities.get(id);
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
entities.add(new ListTile(
|
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']}"),
|
//subtitle: Text("${data['entity_id']}"),
|
||||||
trailing: _buildEntityActionWidget(data),
|
trailing: _buildEntityActionWidget(data),
|
||||||
title: Text(
|
title: Text(
|
||||||
@ -468,7 +469,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
_homeAssistant.uiBuilder.views.forEach((viewId, view) {
|
_homeAssistant.uiBuilder.views.forEach((viewId, view) {
|
||||||
result.add(
|
result.add(
|
||||||
Tab(
|
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,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -2868,7 +2868,10 @@ class MaterialDesignIcons {
|
|||||||
"mdi:blank": 0xf68c
|
"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 (data.entityPicture != null) {
|
||||||
if (homeAssistantWebHost != null) {
|
if (homeAssistantWebHost != null) {
|
||||||
return CircleAvatar(
|
return CircleAvatar(
|
||||||
|
@ -19,9 +19,26 @@ class UIBuilder {
|
|||||||
void build(EntityCollection entitiesCollection) {
|
void build(EntityCollection entitiesCollection) {
|
||||||
_entities = entitiesCollection;
|
_entities = entitiesCollection;
|
||||||
_views.clear();
|
_views.clear();
|
||||||
|
if (!_entities.hasDefaultView) {
|
||||||
|
_createDefaultView();
|
||||||
|
}
|
||||||
_createViews(entitiesCollection.viewList);
|
_createViews(entitiesCollection.viewList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _createDefaultView() {
|
||||||
|
Map<String, List<String>> 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<String> viewsList) {
|
void _createViews(List<String> viewsList) {
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
viewsList.forEach((viewId) {
|
viewsList.forEach((viewId) {
|
||||||
|
Reference in New Issue
Block a user