[#63] Group standalone entities by domain. Separate groups for badges
This commit is contained in:
@ -31,6 +31,7 @@ class HassioDataModel {
|
|||||||
Completer _servicesCompleter;
|
Completer _servicesCompleter;
|
||||||
Completer _configCompleter;
|
Completer _configCompleter;
|
||||||
Timer _fetchingTimer;
|
Timer _fetchingTimer;
|
||||||
|
List _topBadgeDomains = ["alarm_control_panel", "binary_sensor", "device_tracker", "updater", "sun", "timer", "sensor"];
|
||||||
|
|
||||||
Map get entities => _entitiesData;
|
Map get entities => _entitiesData;
|
||||||
Map get services => _servicesData;
|
Map get services => _servicesData;
|
||||||
@ -277,18 +278,38 @@ class HassioDataModel {
|
|||||||
|
|
||||||
//Gethering information for UI
|
//Gethering information for UI
|
||||||
debugPrint("Gethering views");
|
debugPrint("Gethering views");
|
||||||
uiGroups.forEach((viewId) {
|
int viewCounter = 0;
|
||||||
|
uiGroups.forEach((viewId) { //Each view
|
||||||
|
viewCounter +=1;
|
||||||
var viewGroup = _entitiesData[viewId];
|
var viewGroup = _entitiesData[viewId];
|
||||||
Map viewGroupStructure = {};
|
Map viewGroupStructure = {};
|
||||||
if (viewGroup != null) {
|
if (viewGroup != null) {
|
||||||
viewGroupStructure["standalone"] = [];
|
viewGroupStructure["standalone"] = {};
|
||||||
viewGroupStructure["groups"] = [];
|
viewGroupStructure["groups"] = {};
|
||||||
|
viewGroupStructure["groups"]["haclientui.badges"] = {"children": [], "friendly_name": "Badges"};
|
||||||
viewGroupStructure["iconCode"] = viewGroup["iconCode"];
|
viewGroupStructure["iconCode"] = viewGroup["iconCode"];
|
||||||
viewGroup["attributes"]["entity_id"].forEach((entityId) {
|
|
||||||
if (_entitiesData[entityId]["domain"] != "group") {
|
|
||||||
viewGroupStructure["standalone"].add(entityId);
|
viewGroup["attributes"]["entity_id"].forEach((entityId) { //Each entity or group in view
|
||||||
|
Map newGroup = {};
|
||||||
|
String domain = _entitiesData[entityId]["domain"];
|
||||||
|
if (domain != "group") {
|
||||||
|
String autoGroupID;
|
||||||
|
if (_topBadgeDomains.contains(domain)) {
|
||||||
|
autoGroupID = "haclientui.badges";
|
||||||
|
} else {
|
||||||
|
autoGroupID = "$domain.$domain$viewCounter";
|
||||||
|
}
|
||||||
|
if (viewGroupStructure["groups"]["$autoGroupID"] == null) {
|
||||||
|
newGroup["entity_id"] = "$domain.$domain$viewCounter";
|
||||||
|
newGroup["friendly_name"] = "$domain";
|
||||||
|
newGroup["children"] = [];
|
||||||
|
newGroup["children"].add(entityId);
|
||||||
|
viewGroupStructure["groups"]["$autoGroupID"] = Map.from(newGroup);
|
||||||
|
} else {
|
||||||
|
viewGroupStructure["groups"]["$autoGroupID"]["children"].add(entityId);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Map newGroup = {};
|
|
||||||
newGroup["entity_id"] = entityId;
|
newGroup["entity_id"] = entityId;
|
||||||
newGroup["friendly_name"] =
|
newGroup["friendly_name"] =
|
||||||
(_entitiesData[entityId]['attributes'] != null)
|
(_entitiesData[entityId]['attributes'] != null)
|
||||||
@ -299,7 +320,7 @@ class HassioDataModel {
|
|||||||
groupedEntityId) {
|
groupedEntityId) {
|
||||||
newGroup["children"].add(groupedEntityId);
|
newGroup["children"].add(groupedEntityId);
|
||||||
});
|
});
|
||||||
viewGroupStructure["groups"].add(Map.from(newGroup));
|
viewGroupStructure["groups"]["$entityId"] = Map.from(newGroup);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
_uiStructure[viewId.split(".")[1]] = viewGroupStructure;
|
_uiStructure[viewId.split(".")[1]] = viewGroupStructure;
|
||||||
|
170
lib/main.dart
170
lib/main.dart
@ -156,6 +156,91 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
}).catchError((e) => _setErrorState(e));
|
}).catchError((e) => _setErrorState(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Widget> _buildViews() {
|
||||||
|
List<Widget> result = [];
|
||||||
|
if ((_entitiesData != null) && (_uiStructure != null)) {
|
||||||
|
_uiStructure.forEach((viewId, structure) {
|
||||||
|
result.add(
|
||||||
|
RefreshIndicator(
|
||||||
|
color: Colors.amber,
|
||||||
|
child: ListView(
|
||||||
|
physics: const AlwaysScrollableScrollPhysics(),
|
||||||
|
children: _buildSingleView(structure),
|
||||||
|
),
|
||||||
|
onRefresh: () => _refreshData(),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _buildSingleView(structure) {
|
||||||
|
List<Widget> result = [];
|
||||||
|
/*structure["standalone"].forEach((entityId) {
|
||||||
|
result.add(_buildCard([entityId], ""));
|
||||||
|
});*/
|
||||||
|
structure["groups"].forEach((id, group) {
|
||||||
|
if (group["children"].length > 0) {
|
||||||
|
result.add(_buildCard(
|
||||||
|
group["children"], group["friendly_name"].toString()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Card _buildCard(List ids, String name) {
|
||||||
|
List<Widget> body = [];
|
||||||
|
body.add(_buildCardHeader(name));
|
||||||
|
body.addAll(_buildCardBody(ids));
|
||||||
|
Card result =
|
||||||
|
Card(child: new Column(mainAxisSize: MainAxisSize.min, children: body));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildCardHeader(String name) {
|
||||||
|
var result;
|
||||||
|
if (name.length > 0) {
|
||||||
|
result = new ListTile(
|
||||||
|
//leading: const Icon(Icons.device_hub),
|
||||||
|
//subtitle: Text(".."),
|
||||||
|
//trailing: Text("${data["state"]}"),
|
||||||
|
title: Text("$name",
|
||||||
|
textAlign: TextAlign.left,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
result = new Container(width: 0.0, height: 0.0);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _buildCardBody(List ids) {
|
||||||
|
List<Widget> entities = [];
|
||||||
|
ids.forEach((id) {
|
||||||
|
var data = _entitiesData[id];
|
||||||
|
if (data == null) {
|
||||||
|
debugPrint("Hiding unknown entity from card: $id");
|
||||||
|
} else {
|
||||||
|
entities.add(new ListTile(
|
||||||
|
leading: Icon(
|
||||||
|
_createMDIfromCode(data["iconCode"]),
|
||||||
|
color: _stateIconColors[data["state"]] ?? Colors.blueGrey,
|
||||||
|
),
|
||||||
|
//subtitle: Text("${data['entity_id']}"),
|
||||||
|
trailing: _buildEntityAction(id),
|
||||||
|
title: Text(
|
||||||
|
"${data["display_name"]}",
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return entities;
|
||||||
|
}
|
||||||
|
|
||||||
Widget _buildEntityAction(String entityId) {
|
Widget _buildEntityAction(String entityId) {
|
||||||
var entity = _entitiesData[entityId];
|
var entity = _entitiesData[entityId];
|
||||||
Widget result;
|
Widget result;
|
||||||
@ -201,89 +286,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Card _buildCard(List<String> ids, String name) {
|
|
||||||
List<Widget> body = [];
|
|
||||||
body.add(_buildCardHeader(name));
|
|
||||||
body.addAll(_buildCardBody(ids));
|
|
||||||
Card result =
|
|
||||||
Card(child: new Column(mainAxisSize: MainAxisSize.min, children: body));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildCardHeader(String name) {
|
|
||||||
var result;
|
|
||||||
if (name.length > 0) {
|
|
||||||
result = new ListTile(
|
|
||||||
//leading: const Icon(Icons.device_hub),
|
|
||||||
//subtitle: Text(".."),
|
|
||||||
//trailing: Text("${data["state"]}"),
|
|
||||||
title: Text("$name",
|
|
||||||
textAlign: TextAlign.left,
|
|
||||||
overflow: TextOverflow.ellipsis,
|
|
||||||
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
result = new Container(width: 0.0, height: 0.0);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Widget> _buildCardBody(List<String> ids) {
|
|
||||||
List<Widget> entities = [];
|
|
||||||
ids.forEach((id) {
|
|
||||||
var data = _entitiesData[id];
|
|
||||||
if (data == null) {
|
|
||||||
debugPrint("Hiding unknown entity from card: $id");
|
|
||||||
} else {
|
|
||||||
entities.add(new ListTile(
|
|
||||||
leading: Icon(
|
|
||||||
_createMDIfromCode(data["iconCode"]),
|
|
||||||
color: _stateIconColors[data["state"]] ?? Colors.blueGrey,
|
|
||||||
),
|
|
||||||
//subtitle: Text("${data['entity_id']}"),
|
|
||||||
trailing: _buildEntityAction(id),
|
|
||||||
title: Text(
|
|
||||||
"${data["display_name"]}",
|
|
||||||
overflow: TextOverflow.ellipsis,
|
|
||||||
),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return entities;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Widget> buildSingleView(structure) {
|
|
||||||
List<Widget> result = [];
|
|
||||||
structure["standalone"].forEach((entityId) {
|
|
||||||
result.add(_buildCard([entityId], ""));
|
|
||||||
});
|
|
||||||
structure["groups"].forEach((group) {
|
|
||||||
result.add(_buildCard(
|
|
||||||
group["children"], group["friendly_name"].toString()));
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Widget> buildUIViews() {
|
|
||||||
List<Widget> result = [];
|
|
||||||
if ((_entitiesData != null) && (_uiStructure != null)) {
|
|
||||||
_uiStructure.forEach((viewId, structure) {
|
|
||||||
result.add(
|
|
||||||
RefreshIndicator(
|
|
||||||
color: Colors.amber,
|
|
||||||
child: ListView(
|
|
||||||
physics: const AlwaysScrollableScrollPhysics(),
|
|
||||||
children: buildSingleView(structure),
|
|
||||||
),
|
|
||||||
onRefresh: () => _refreshData(),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
IconData _createMDIfromCode(int code) {
|
IconData _createMDIfromCode(int code) {
|
||||||
return IconData(code, fontFamily: 'Material Design Icons');
|
return IconData(code, fontFamily: 'Material Design Icons');
|
||||||
}
|
}
|
||||||
@ -475,7 +477,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
),
|
),
|
||||||
drawer: _buildAppDrawer(),
|
drawer: _buildAppDrawer(),
|
||||||
body: TabBarView(
|
body: TabBarView(
|
||||||
children: buildUIViews()
|
children: _buildViews()
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user