114 lines
3.2 KiB
Dart
114 lines
3.2 KiB
Dart
part of '../main.dart';
|
|
|
|
class HAView {
|
|
List<HACard> cards = [];
|
|
List<Entity> badges = [];
|
|
Entity linkedEntity;
|
|
String name;
|
|
String id;
|
|
String iconName;
|
|
int count;
|
|
|
|
HAView({
|
|
this.name,
|
|
this.id,
|
|
this.count,
|
|
this.iconName,
|
|
List<Entity> childEntities
|
|
}) {
|
|
if (childEntities != null) {
|
|
_fillView(childEntities);
|
|
}
|
|
}
|
|
|
|
void _fillView(List<Entity> childEntities) {
|
|
List<HACard> autoGeneratedCards = [];
|
|
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,
|
|
linkedEntity: EntityWrapper(entity: e),
|
|
type: "media-control"
|
|
);
|
|
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 )) {
|
|
HACard card = HACard(
|
|
id: groupIdToAdd,
|
|
name: entity.domain,
|
|
type: "entities"
|
|
);
|
|
card.entities.add(EntityWrapper(entity: entity));
|
|
autoGeneratedCards.add(card);
|
|
} else {
|
|
autoGeneratedCards.firstWhere((card) => card.id == groupIdToAdd).entities.add(EntityWrapper(entity: entity));
|
|
}
|
|
} else {
|
|
HACard card = HACard(
|
|
name: entity.displayName,
|
|
id: entity.entityId,
|
|
linkedEntity: EntityWrapper(entity: entity),
|
|
type: "entities"
|
|
);
|
|
card.entities.addAll(entity.childEntities.where((entity) {return entity.domain != "media_player";}).map((e) {return EntityWrapper(entity: e);}));
|
|
entity.childEntities.where((entity) {return entity.domain == "media_player";}).forEach((entity){
|
|
HACard mediaCard = HACard(
|
|
name: entity.displayName,
|
|
id: entity.entityId,
|
|
linkedEntity: EntityWrapper(entity: entity),
|
|
type: "media-control"
|
|
);
|
|
cards.add(mediaCard);
|
|
});
|
|
cards.add(card);
|
|
}
|
|
});
|
|
cards.addAll(autoGeneratedCards);
|
|
}
|
|
|
|
Widget buildTab() {
|
|
if (linkedEntity == null) {
|
|
if (iconName != null) {
|
|
return
|
|
Tab(
|
|
icon:
|
|
Icon(
|
|
MaterialDesignIcons.createIconDataFromIconName(
|
|
iconName ?? "mdi:home-assistant"),
|
|
size: 24.0,
|
|
)
|
|
);
|
|
} else {
|
|
return
|
|
Tab(
|
|
text: name.toUpperCase(),
|
|
);
|
|
}
|
|
} else {
|
|
if (linkedEntity.icon != null && linkedEntity.icon.length > 0) {
|
|
return Tab(
|
|
icon: Icon(
|
|
MaterialDesignIcons.createIconDataFromIconName(
|
|
linkedEntity.icon),
|
|
size: 24.0,
|
|
)
|
|
);
|
|
} else {
|
|
return Tab(
|
|
text: linkedEntity.displayName.toUpperCase(),
|
|
);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
Widget build(BuildContext context) {
|
|
return ViewWidget(
|
|
view: this,
|
|
);
|
|
}
|
|
} |