2019-08-24 21:22:32 +03:00
|
|
|
part of '../main.dart';
|
2018-09-29 16:19:01 +03:00
|
|
|
|
|
|
|
class EntityViewPage extends StatefulWidget {
|
2019-09-01 00:12:16 +03:00
|
|
|
EntityViewPage({Key key, @required this.entityId}) : super(key: key);
|
2018-09-29 16:19:01 +03:00
|
|
|
|
2018-11-18 13:19:00 +02:00
|
|
|
final String entityId;
|
2018-09-29 16:19:01 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
_EntityViewPageState createState() => new _EntityViewPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EntityViewPageState extends State<EntityViewPage> {
|
2018-11-18 13:19:00 +02:00
|
|
|
StreamSubscription _refreshDataSubscription;
|
2018-09-29 17:59:38 +03:00
|
|
|
StreamSubscription _stateSubscription;
|
2020-04-14 21:15:28 +03:00
|
|
|
Entity _entity;
|
2018-09-29 16:19:01 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-09-29 17:59:38 +03:00
|
|
|
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
|
2018-11-18 13:19:00 +02:00
|
|
|
if (event.entityId == widget.entityId) {
|
2019-09-15 14:34:00 +03:00
|
|
|
Logger.d("[Entity page] State change event handled: ${event.entityId}");
|
2020-04-14 21:15:28 +03:00
|
|
|
setState(() {
|
|
|
|
_getEntity();
|
|
|
|
});
|
2018-09-30 01:07:02 +03:00
|
|
|
}
|
2018-09-29 17:59:38 +03:00
|
|
|
});
|
2018-11-18 13:19:00 +02:00
|
|
|
_refreshDataSubscription = eventBus.on<RefreshDataFinishedEvent>().listen((event) {
|
2020-04-14 21:15:28 +03:00
|
|
|
Logger.d("[Entity page] Refresh data event handled");
|
|
|
|
setState(() {
|
|
|
|
_getEntity();
|
|
|
|
});
|
2018-11-18 13:19:00 +02:00
|
|
|
});
|
2020-04-14 21:15:28 +03:00
|
|
|
_getEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getEntity() {
|
2020-05-02 11:31:10 +03:00
|
|
|
_entity = HomeAssistant().entities?.get(widget.entityId);
|
2018-09-29 16:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-04-14 21:15:28 +03:00
|
|
|
String entityNameToDisplay = '${(_entity?.displayName ?? widget.entityId) ?? ''}';
|
2018-09-29 16:19:01 +03:00
|
|
|
return new Scaffold(
|
|
|
|
appBar: new AppBar(
|
|
|
|
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
|
|
|
|
Navigator.pop(context);
|
|
|
|
}),
|
2020-04-14 21:15:28 +03:00
|
|
|
title: new Text(entityNameToDisplay),
|
2018-09-29 16:19:01 +03:00
|
|
|
),
|
2020-04-14 21:15:28 +03:00
|
|
|
body: _entity == null ? PageLoadingError(
|
|
|
|
errorText: 'Entity is not available $entityNameToDisplay',
|
|
|
|
) : EntityPageLayout(entity: _entity),
|
2018-09-29 16:19:01 +03:00
|
|
|
);
|
|
|
|
}
|
2018-09-29 17:59:38 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose(){
|
|
|
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
2018-11-18 13:19:00 +02:00
|
|
|
if (_refreshDataSubscription != null) _refreshDataSubscription.cancel();
|
2018-09-29 17:59:38 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2018-09-29 16:19:01 +03:00
|
|
|
}
|