From 2fcd27d24000ce24b0116b6888b9f6eaed8b1740 Mon Sep 17 00:00:00 2001 From: estevez Date: Sat, 29 Sep 2018 17:59:38 +0300 Subject: [PATCH] Fix state handling on entity view page --- lib/entity.class.dart | 2 +- lib/entity.page.dart | 15 +++++++++++++++ lib/main.dart | 2 +- lib/utils.class.dart | 4 ++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/entity.class.dart b/lib/entity.class.dart index b993eb8..246006a 100644 --- a/lib/entity.class.dart +++ b/lib/entity.class.dart @@ -256,7 +256,7 @@ class InputEntity extends Entity { Padding( padding: EdgeInsets.only(right: 16.0), child: Text( - "${_state}${this.unitOfMeasurement}", + "$_state${this.unitOfMeasurement}", textAlign: TextAlign.right, style: new TextStyle( fontSize: 16.0, diff --git a/lib/entity.page.dart b/lib/entity.page.dart index a41cc0c..ad693a9 100644 --- a/lib/entity.page.dart +++ b/lib/entity.page.dart @@ -13,12 +13,21 @@ class _EntityViewPageState extends State { String _title; Entity _entity; String _lastState; + StreamSubscription _stateSubscription; @override void initState() { super.initState(); _entity = widget.entity; _lastState = _entity.state; + if (_stateSubscription != null) _stateSubscription.cancel(); + _stateSubscription = eventBus.on().listen((event) { + setState(() { + if (event.entityId == _entity.entityId) { + _lastState = event.newState ?? _entity.state; + } + }); + }); _prepareData(); } @@ -47,4 +56,10 @@ class _EntityViewPageState extends State { ), ); } + + @override + void dispose(){ + if (_stateSubscription != null) _stateSubscription.cancel(); + super.dispose(); + } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 9bebc03..ad34b1d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -144,7 +144,7 @@ class _MainPageState extends State with WidgetsBindingObserver { if (_stateSubscription != null) _stateSubscription.cancel(); _stateSubscription = eventBus.on().listen((event) { setState(() { - if (event.setToCollection) { + if (event.localChange) { _entities .get(event.entityId) .state = event.newState; diff --git a/lib/utils.class.dart b/lib/utils.class.dart index 8a6486c..a05b660 100644 --- a/lib/utils.class.dart +++ b/lib/utils.class.dart @@ -45,9 +45,9 @@ class haUtils { class StateChangedEvent { String entityId; String newState; - bool setToCollection; + bool localChange; - StateChangedEvent(this.entityId, this.newState, this.setToCollection); + StateChangedEvent(this.entityId, this.newState, this.localChange); } class SettingsChangedEvent {