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/view_class.dart

123 lines
3.1 KiB
Dart
Raw Normal View History

part of 'main.dart';
2018-10-21 00:30:58 +03:00
class View extends StatefulWidget {
2018-10-07 02:17:14 +03:00
final String displayName;
2018-10-21 00:30:58 +03:00
final List<Entity> childEntities;
final int count;
2018-10-07 02:17:14 +03:00
2018-10-21 00:30:58 +03:00
View({
2018-10-07 02:17:14 +03:00
Key key,
2018-10-21 00:30:58 +03:00
@required this.childEntities,
@required this.count,
2018-10-07 02:17:14 +03:00
this.displayName
}) : super(key: key);
@override
State<StatefulWidget> createState() {
2018-10-21 00:30:58 +03:00
return ViewState();
}
}
2018-10-21 00:30:58 +03:00
class ViewState extends State<View> {
StreamSubscription _refreshDataSubscription;
Completer _refreshCompleter;
2018-10-21 00:30:58 +03:00
List<Entity> _childEntitiesAsBadges;
Map<String, Entity> _childEntitiesAsCards;
@override
void initState() {
super.initState();
_refreshDataSubscription = eventBus.on<RefreshDataFinishedEvent>().listen((event) {
if ((_refreshCompleter != null) && (!_refreshCompleter.isCompleted)) {
_refreshCompleter.complete();
}
});
2018-10-21 00:30:58 +03:00
_childEntitiesAsCards = {};
_childEntitiesAsBadges = [];
_composeEntities();
}
void _composeEntities() {
widget.childEntities.forEach((Entity entity){
if (!entity.isGroup) {
if (entity.isBadge) {
_childEntitiesAsBadges.add(entity);
} else {
String groupIdToAdd = "${entity.domain}.${entity.domain}${widget.count}";
if (_childEntitiesAsCards[groupIdToAdd] == null) {
_childEntitiesAsCards[groupIdToAdd] = entity;
}
_childEntitiesAsCards[groupIdToAdd].childEntities.add(entity);
}
} else {
_childEntitiesAsCards[entity.entityId] = entity;
_childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
}
});
}
2018-10-07 02:17:14 +03:00
@override
Widget build(BuildContext context) {
return RefreshIndicator(
color: Colors.amber,
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: _buildChildren(context),
),
onRefresh: () => _refreshData(),
);
}
List<Widget> _buildChildren(BuildContext context) {
List<Widget> result = [];
2018-10-21 00:30:58 +03:00
if (_childEntitiesAsBadges.isNotEmpty) {
2018-10-07 02:17:14 +03:00
result.insert(0,
Wrap(
alignment: WrapAlignment.center,
spacing: 10.0,
runSpacing: 1.0,
2018-10-21 00:30:58 +03:00
children: _buildBadges(context),
2018-10-07 02:17:14 +03:00
)
);
}
2018-10-07 02:17:14 +03:00
2018-10-21 00:30:58 +03:00
_childEntitiesAsCards.forEach((String id, Entity groupEntity){
2018-10-07 02:17:14 +03:00
result.add(
HACard(
2018-10-21 00:30:58 +03:00
entities: groupEntity.childEntities,
friendlyName: groupEntity.displayName,
hidden: groupEntity.isHidden
2018-10-07 02:17:14 +03:00
)
);
});
return result;
}
2018-10-21 00:30:58 +03:00
List<Widget> _buildBadges(BuildContext context) {
2018-10-12 18:03:27 +03:00
List<Widget> result = [];
2018-10-21 00:30:58 +03:00
_childEntitiesAsBadges.forEach((Entity entity) {
result.add(entity.buildBadgeWidget(context));
2018-10-07 02:17:14 +03:00
});
return result;
}
2018-10-07 02:17:14 +03:00
Future _refreshData() {
if ((_refreshCompleter != null) && (!_refreshCompleter.isCompleted)) {
TheLogger.log("Debug","Previous data refresh is still in progress");
} else {
_refreshCompleter = Completer();
eventBus.fire(RefreshDataEvent());
}
return _refreshCompleter.future;
}
2018-10-07 02:17:14 +03:00
@override
void dispose() {
_refreshDataSubscription.cancel();
super.dispose();
}
}