This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/pages/entity.page.dart

72 lines
2.1 KiB
Dart
Raw Normal View History

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;
2019-09-14 18:32:44 +03:00
Entity entity;
Entity forwardToMainPage;
2019-09-14 18:54:31 +03:00
bool _popScheduled = false;
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-02-20 13:57:25 +02:00
Logger.d("State change event handled by entity page: ${event.entityId}");
2018-09-30 01:07:02 +03:00
setState(() {});
}
2018-09-29 17:59:38 +03:00
});
2018-11-18 13:19:00 +02:00
_refreshDataSubscription = eventBus.on<RefreshDataFinishedEvent>().listen((event) {
setState(() {});
});
2019-09-14 18:32:44 +03:00
entity = HomeAssistant().entities.get(widget.entityId);
2018-09-29 16:19:01 +03:00
}
@override
Widget build(BuildContext context) {
2019-09-14 18:32:44 +03:00
Widget body;
if (MediaQuery.of(context).size.width >= Sizes.tabletMinWidth) {
2019-09-14 18:54:31 +03:00
if (!_popScheduled) {
_popScheduled = true;
_popAfterBuild();
}
2019-09-14 18:32:44 +03:00
body = PageLoadingIndicator();
} else {
body = entity.buildEntityPageWidget(context);
}
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);
}),
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
2019-09-14 18:32:44 +03:00
title: new Text("${entity.displayName}"),
2018-09-29 16:19:01 +03:00
),
2019-09-14 18:32:44 +03:00
body: body,
2018-09-29 16:19:01 +03:00
);
}
2018-09-29 17:59:38 +03:00
2019-09-14 18:32:44 +03:00
_popAfterBuild() async {
forwardToMainPage = entity;
await Future.delayed(Duration(milliseconds: 300));
Navigator.of(context).pop();
}
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();
2019-09-14 18:32:44 +03:00
eventBus.fire(ShowEntityPageEvent(entity: forwardToMainPage));
2018-09-29 17:59:38 +03:00
super.dispose();
}
2018-09-29 16:19:01 +03:00
}