From 813770329c8fecc4c06734cde5067c89d0148e7e Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Wed, 31 Oct 2018 01:37:36 +0200 Subject: [PATCH] WIP #120 render only needed states --- lib/entity_class/climate_entity.class.dart | 3 +- .../history_chart/combined_history_chart.dart | 55 +++++++++++++------ .../history_chart/entity_history.dart | 3 +- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/entity_class/climate_entity.class.dart b/lib/entity_class/climate_entity.class.dart index 76af32a..5107e4b 100644 --- a/lib/entity_class/climate_entity.class.dart +++ b/lib/entity_class/climate_entity.class.dart @@ -8,7 +8,8 @@ class ClimateEntity extends Entity { EntityHistoryConfig historyConfig = EntityHistoryConfig( chartType: EntityHistoryWidgetType.numericAttributes, numericState: false, - numericAttributesToShow: ["temperature", "current_temperature"] + numericAttributesToShow: ["temperature", "current_temperature"], + statesToShow: ["heat", "cool", "auto"] ); static const SUPPORT_TARGET_TEMPERATURE = 1; diff --git a/lib/entity_widgets/history_chart/combined_history_chart.dart b/lib/entity_widgets/history_chart/combined_history_chart.dart index 7827b46..a94528b 100644 --- a/lib/entity_widgets/history_chart/combined_history_chart.dart +++ b/lib/entity_widgets/history_chart/combined_history_chart.dart @@ -28,8 +28,13 @@ class _CombinedHistoryChartWidgetState extends State if ((_selectedId > -1) && (_parsedHistory != null) && (_parsedHistory.first.data.length >= (_selectedId + 1))) { selectedTime = _parsedHistory.first.data[_selectedId].startTime; _parsedHistory.where((item) { return item.id == "state"; }).forEach((item) { - selectedStates.add("${item.data[_selectedId].state}"); - colorIndexes.add(item.data[_selectedId].colorId); + if (_selectedId < item.data.length) { + selectedStates.add(item.data[_selectedId].state); + colorIndexes.add(item.data[_selectedId].colorId); + } else { + selectedStates.add(null); + colorIndexes.add(-1); + } }); _parsedHistory.where((item) { return item.id == "value"; }).forEach((item) { selectedStates.add("${item.data[_selectedId].value}"); @@ -104,10 +109,19 @@ class _CombinedHistoryChartWidgetState extends State } else { endTime = now; } + String state; + if (widget.config.statesToShow != null) { + if (widget.config.statesToShow.isNotEmpty) { + state = widget.config.statesToShow.contains(stateData["state"]) ? stateData["state"] : null; + } else { + state = stateData["state"]; + } + } + if (stateData["attributes"] != null) { - data.add(CombinedEntityStateHistoryMoment(_parseToDouble(stateData["attributes"]["$attrName"]), stateData["state"], startTime, endTime, i, colorIdCounter)); + data.add(CombinedEntityStateHistoryMoment(_parseToDouble(stateData["attributes"]["$attrName"]), state, startTime, endTime, i, colorIdCounter)); } else { - data.add(CombinedEntityStateHistoryMoment(null, stateData["state"], startTime, endTime, i, colorIdCounter)); + data.add(CombinedEntityStateHistoryMoment(null, state, startTime, endTime, i, colorIdCounter)); } } data.add(CombinedEntityStateHistoryMoment(data.last.value, data.last.state, now, null, widget.rawHistory.length, colorIdCounter)); @@ -144,7 +158,7 @@ class _CombinedHistoryChartWidgetState extends State domainUpperBoundFn: (CombinedEntityStateHistoryMoment historyMoment, _) => historyMoment.endTime ?? DateTime.now(), // No measure values are needed for symbol annotations. measureFn: (_, __) => null, - data: numericDataLists[numericDataLists.keys.first], + data: numericDataLists[numericDataLists.keys.first].where((hm) => hm.state != null).toList(), ) // Configure our custom symbol annotation renderer for this series. ..setAttribute(charts.rendererIdKey, 'stateBars') @@ -237,17 +251,26 @@ class CombinedHistoryControlWidget extends StatelessWidget { Widget _buildStates() { List children = []; for (int i = 0; i < selectedStates.length; i++) { - children.add( - Text( - "${selectedStates[i]}", - textAlign: TextAlign.right, - style: TextStyle( - fontWeight: FontWeight.bold, - color: EntityColors.historyStateColor(selectedStates[i], colorIndexes[i]), - fontSize: 22.0 - ), - ) - ); + if (selectedStates[i] != null) { + children.add( + Text( + "${selectedStates[i]}", + textAlign: TextAlign.right, + style: TextStyle( + fontWeight: FontWeight.bold, + color: EntityColors.historyStateColor( + selectedStates[i], colorIndexes[i]), + fontSize: 20.0 + ), + ) + ); + } else { + children.add( + Container( + height: 23.0, + ) + ); + } } return Column( crossAxisAlignment: CrossAxisAlignment.end, diff --git a/lib/entity_widgets/history_chart/entity_history.dart b/lib/entity_widgets/history_chart/entity_history.dart index cba6e96..51ba48e 100644 --- a/lib/entity_widgets/history_chart/entity_history.dart +++ b/lib/entity_widgets/history_chart/entity_history.dart @@ -9,9 +9,10 @@ class EntityHistoryWidgetType { class EntityHistoryConfig { final int chartType; final List numericAttributesToShow; + final List statesToShow; final bool numericState; - EntityHistoryConfig({this.chartType, this.numericAttributesToShow, this.numericState: true}); + EntityHistoryConfig({this.chartType, this.numericAttributesToShow: const [], this.statesToShow: const [], this.numericState: true}); }