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';
class View {
2018-10-07 02:17:14 +03:00
List<Entity> childEntitiesAsBadges;
Map<String, CardSkeleton> childEntitiesAsCards;
2018-10-07 02:17:14 +03:00
int count;
List<Entity> entities;
2018-10-07 02:17:14 +03:00
View({
Key key,
this.count,
this.entities
}) {
childEntitiesAsBadges = [];
childEntitiesAsCards = {};
_composeEntities();
}
2018-10-07 02:17:14 +03:00
Widget buildWidget(BuildContext context) {
return ViewWidget(
badges: childEntitiesAsBadges,
cards: childEntitiesAsCards,
);
2018-09-28 11:18:37 +03:00
}
2018-10-07 02:17:14 +03:00
void _composeEntities() {
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;
}
});
}
2018-10-07 02:17:14 +03:00
}
class ViewWidget extends StatefulWidget {
2018-10-07 02:17:14 +03:00
final List<Entity> badges;
final Map<String, CardSkeleton> cards;
final String displayName;
const ViewWidget({
Key key,
this.badges,
this.cards,
this.displayName
}) : super(key: key);
@override
State<StatefulWidget> createState() {
return ViewWidgetState();
}
}
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 = [];
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,
children: _buildBadges(context, widget.badges),
2018-10-07 02:17:14 +03:00
)
);
}
2018-10-07 02:17:14 +03:00
widget.cards.forEach((String id, CardSkeleton skeleton){
2018-10-07 02:17:14 +03:00
result.add(
HACard(
entities: skeleton.childEntities,
friendlyName: skeleton.displayName,
)
);
});
return result;
}
2018-10-07 02:17:14 +03:00
List<EntityWidget> _buildBadges(BuildContext context, List<Entity> badges) {
List<EntityWidget> result = [];
badges.forEach((Entity entity) {
result.add(entity.buildWidget(context, EntityWidgetType.badge));
});
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-07 02:17:14 +03:00
}
class CardSkeleton {
String displayName;
List<Entity> childEntities;
2018-10-07 02:17:14 +03:00
CardSkeleton({Key key, this.displayName, this.childEntities}) {
childEntities = [];
}
}