From 4b0d857e9c7afbc21aa667b90d45dd2a6a38f9f9 Mon Sep 17 00:00:00 2001 From: estevez Date: Fri, 21 Sep 2018 23:05:12 +0300 Subject: [PATCH] [#63] Group standalone entities by domain. Separate groups for badges --- lib/data_model.dart | 37 +++++++--- lib/main.dart | 170 ++++++++++++++++++++++---------------------- 2 files changed, 115 insertions(+), 92 deletions(-) diff --git a/lib/data_model.dart b/lib/data_model.dart index 6e85742..46b0117 100644 --- a/lib/data_model.dart +++ b/lib/data_model.dart @@ -31,6 +31,7 @@ class HassioDataModel { Completer _servicesCompleter; Completer _configCompleter; Timer _fetchingTimer; + List _topBadgeDomains = ["alarm_control_panel", "binary_sensor", "device_tracker", "updater", "sun", "timer", "sensor"]; Map get entities => _entitiesData; Map get services => _servicesData; @@ -277,18 +278,38 @@ class HassioDataModel { //Gethering information for UI debugPrint("Gethering views"); - uiGroups.forEach((viewId) { + int viewCounter = 0; + uiGroups.forEach((viewId) { //Each view + viewCounter +=1; var viewGroup = _entitiesData[viewId]; Map viewGroupStructure = {}; if (viewGroup != null) { - viewGroupStructure["standalone"] = []; - viewGroupStructure["groups"] = []; + viewGroupStructure["standalone"] = {}; + viewGroupStructure["groups"] = {}; + viewGroupStructure["groups"]["haclientui.badges"] = {"children": [], "friendly_name": "Badges"}; 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 { - Map newGroup = {}; newGroup["entity_id"] = entityId; newGroup["friendly_name"] = (_entitiesData[entityId]['attributes'] != null) @@ -299,7 +320,7 @@ class HassioDataModel { groupedEntityId) { newGroup["children"].add(groupedEntityId); }); - viewGroupStructure["groups"].add(Map.from(newGroup)); + viewGroupStructure["groups"]["$entityId"] = Map.from(newGroup); } }); _uiStructure[viewId.split(".")[1]] = viewGroupStructure; diff --git a/lib/main.dart b/lib/main.dart index da4a884..93d47ad 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -156,6 +156,91 @@ class _MainPageState extends State with WidgetsBindingObserver { }).catchError((e) => _setErrorState(e)); } + List _buildViews() { + List 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 _buildSingleView(structure) { + List 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 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 _buildCardBody(List ids) { + List 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) { var entity = _entitiesData[entityId]; Widget result; @@ -201,89 +286,6 @@ class _MainPageState extends State with WidgetsBindingObserver { return result; } - Card _buildCard(List ids, String name) { - List 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 _buildCardBody(List ids) { - List 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 buildSingleView(structure) { - List 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 buildUIViews() { - List 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) { return IconData(code, fontFamily: 'Material Design Icons'); } @@ -475,7 +477,7 @@ class _MainPageState extends State with WidgetsBindingObserver { ), drawer: _buildAppDrawer(), body: TabBarView( - children: buildUIViews() + children: _buildViews() ), ) );