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

116 lines
3.3 KiB
Dart
Raw Normal View History

2019-09-07 18:23:04 +03:00
part of 'main.dart';
2018-10-27 17:28:47 +03:00
class HAView {
List<HACard> cards = [];
List<Entity> badges = [];
Entity linkedEntity;
final String name;
final String id;
final String iconName;
final int count;
final bool panel;
2018-10-27 17:28:47 +03:00
HAView({
this.name,
this.id,
this.count,
this.iconName,
this.panel: false,
2018-10-27 17:28:47 +03:00
List<Entity> childEntities
}) {
if (childEntities != null) {
_fillView(childEntities);
}
}
void _fillView(List<Entity> childEntities) {
List<HACard> autoGeneratedCards = [];
2018-11-11 19:51:02 +02:00
badges.addAll(childEntities.where((entity){ return entity.isBadge;}));
childEntities.where((entity){ return entity.domain == "media_player";}).forEach((e){
HACard card = HACard(
name: e.displayName,
id: e.entityId,
linkedEntityWrapper: EntityWrapper(entity: e),
2019-09-07 15:47:09 +03:00
type: CardType.MEDIA_CONTROL
2018-11-11 19:51:02 +02:00
);
cards.add(card);
});
childEntities.where((e){return (!e.isBadge && e.domain != "media_player");}).forEach((entity) {
if (!entity.isGroup) {
String groupIdToAdd = "${entity.domain}.${entity.domain}$count";
if (autoGeneratedCards.every((HACard card) => card.id != groupIdToAdd )) {
2018-10-27 17:28:47 +03:00
HACard card = HACard(
2018-11-11 19:51:02 +02:00
id: groupIdToAdd,
name: entity.domain,
2019-09-07 15:47:09 +03:00
type: CardType.ENTITIES
2018-11-11 19:51:02 +02:00
);
2018-11-15 19:08:47 +02:00
card.entities.add(EntityWrapper(entity: entity));
2018-11-11 19:51:02 +02:00
autoGeneratedCards.add(card);
} else {
2018-11-15 19:08:47 +02:00
autoGeneratedCards.firstWhere((card) => card.id == groupIdToAdd).entities.add(EntityWrapper(entity: entity));
2018-11-11 19:51:02 +02:00
}
} else {
HACard card = HACard(
name: entity.displayName,
id: entity.entityId,
linkedEntityWrapper: EntityWrapper(entity: entity),
2019-09-07 15:47:09 +03:00
type: CardType.ENTITIES
2018-11-11 19:51:02 +02:00
);
card.entities.addAll(entity.childEntities.where((entity) {return entity.domain != "media_player";}).map((e) {return EntityWrapper(entity: e);}));
2018-11-11 19:51:02 +02:00
entity.childEntities.where((entity) {return entity.domain == "media_player";}).forEach((entity){
HACard mediaCard = HACard(
2018-10-27 17:28:47 +03:00
name: entity.displayName,
id: entity.entityId,
linkedEntityWrapper: EntityWrapper(entity: entity),
2019-09-07 15:47:09 +03:00
type: CardType.MEDIA_CONTROL
2018-10-27 17:28:47 +03:00
);
2018-11-11 19:51:02 +02:00
cards.add(mediaCard);
});
cards.add(card);
2018-10-27 17:28:47 +03:00
}
});
cards.addAll(autoGeneratedCards);
}
2018-11-04 22:19:45 +02:00
Widget buildTab() {
if (linkedEntity == null) {
if (iconName != null && iconName.isNotEmpty) {
2018-11-04 22:19:45 +02:00
return
Tab(
icon:
Icon(
MaterialDesignIcons.getIconDataFromIconName(
2018-11-04 22:19:45 +02:00
iconName ?? "mdi:home-assistant"),
size: 24.0,
)
);
} else {
return
Tab(
2019-09-18 21:14:21 +03:00
text: "${name?.toUpperCase() ?? "UNNAMED VIEW"}",
2018-11-04 22:19:45 +02:00
);
}
} else {
if (linkedEntity.icon != null && linkedEntity.icon.length > 0) {
return Tab(
icon: Icon(
MaterialDesignIcons.getIconDataFromIconName(
2018-11-04 22:19:45 +02:00
linkedEntity.icon),
size: 24.0,
)
);
} else {
return Tab(
2019-09-18 21:14:21 +03:00
text: "${linkedEntity.displayName?.toUpperCase()}",
2018-11-04 22:19:45 +02:00
);
}
}
}
2018-10-27 17:28:47 +03:00
Widget build(BuildContext context) {
return ViewWidget(
view: this,
);
}
}