Resolves #70 Build default_view automatically
This commit is contained in:
parent
b935a0e372
commit
af3a5bc611
@ -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<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(
|
||||
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<MainPage> 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<MainPage> 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<MainPage> 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<MainPage> 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<MainPage> 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,
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
|
@ -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(
|
||||
|
@ -19,9 +19,26 @@ class UIBuilder {
|
||||
void build(EntityCollection entitiesCollection) {
|
||||
_entities = entitiesCollection;
|
||||
_views.clear();
|
||||
if (!_entities.hasDefaultView) {
|
||||
_createDefaultView();
|
||||
}
|
||||
_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) {
|
||||
int counter = 0;
|
||||
viewsList.forEach((viewId) {
|
||||
|
Reference in New Issue
Block a user