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

158 lines
3.6 KiB
Dart
Raw Normal View History

part of 'main.dart';
2018-10-21 02:39:51 +03:00
class View {
List<Entity> childEntitiesAsBadges;
Map<String, CardSkeleton> childEntitiesAsCards;
int count;
List<Entity> entities;
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 02:39:51 +03:00
this.count,
this.entities
}) {
childEntitiesAsBadges = [];
childEntitiesAsCards = {};
2018-10-21 15:26:14 +03:00
_filterEntities();
2018-10-21 02:39:51 +03:00
}
Widget buildWidget(BuildContext context) {
return ViewWidget(
badges: childEntitiesAsBadges,
cards: childEntitiesAsCards,
);
}
2018-10-21 15:26:14 +03:00
void _filterEntities() {
2018-10-21 02:39:51 +03:00
entities.forEach((Entity entity){
if (!entity.isGroup) {
if (entity.isBadge) {
childEntitiesAsBadges.add(entity);
} else {
String groupIdToAdd = "${entity.domain}.${entity.domain}$count";
if (childEntitiesAsCards[groupIdToAdd] == null) {
childEntitiesAsCards[groupIdToAdd] = CardSkeleton(
displayName: entity.domain,
);
}
childEntitiesAsCards[groupIdToAdd].childEntities.add(entity);
}
} else {
childEntitiesAsCards[entity.entityId] = CardSkeleton(
displayName: entity.displayName,
);
childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
}
});
}
}
class ViewWidget extends StatefulWidget {
final List<Entity> badges;
final Map<String, CardSkeleton> cards;
final String displayName;
const ViewWidget({
Key key,
this.badges,
this.cards,
2018-10-07 02:17:14 +03:00
this.displayName
}) : super(key: key);
@override
State<StatefulWidget> createState() {
2018-10-21 02:39:51 +03:00
return ViewWidgetState();
}
}
2018-10-21 02:39:51 +03:00
class ViewWidgetState extends State<ViewWidget> {
StreamSubscription _refreshDataSubscription;
Completer _refreshCompleter;
@override
void initState() {
super.initState();
_refreshDataSubscription = eventBus.on<RefreshDataFinishedEvent>().listen((event) {
if ((_refreshCompleter != null) && (!_refreshCompleter.isCompleted)) {
_refreshCompleter.complete();
}
});
}
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 02:39:51 +03:00
if (widget.badges.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 02:39:51 +03:00
children: _buildBadges(context, widget.badges),
2018-10-07 02:17:14 +03:00
)
);
}
2018-10-07 02:17:14 +03:00
2018-10-21 02:39:51 +03:00
widget.cards.forEach((String id, CardSkeleton skeleton){
2018-10-07 02:17:14 +03:00
result.add(
HACard(
2018-10-21 02:39:51 +03:00
entities: skeleton.childEntities,
friendlyName: skeleton.displayName,
2018-10-07 02:17:14 +03:00
)
);
});
return result;
}
2018-10-21 02:39:51 +03:00
List<Widget> _buildBadges(BuildContext context, List<Entity> badges) {
2018-10-12 18:03:27 +03:00
List<Widget> result = [];
2018-10-21 02:39:51 +03:00
badges.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();
}
2018-10-21 02:39:51 +03:00
}
class CardSkeleton {
String displayName;
List<Entity> childEntities;
CardSkeleton({Key key, this.displayName, this.childEntities}) {
childEntities = [];
}
}