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
2019-09-07 18:23:04 +03:00

116 lines
3.3 KiB
Dart

part of 'main.dart';
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;
HAView({
this.name,
this.id,
this.count,
this.iconName,
this.panel: false,
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,
linkedEntityWrapper: EntityWrapper(entity: e),
type: CardType.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: CardType.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,
linkedEntityWrapper: EntityWrapper(entity: entity),
type: CardType.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,
linkedEntityWrapper: EntityWrapper(entity: entity),
type: CardType.MEDIA_CONTROL
);
cards.add(mediaCard);
});
cards.add(card);
}
});
cards.addAll(autoGeneratedCards);
}
Widget buildTab() {
if (linkedEntity == null) {
if (iconName != null) {
return
Tab(
icon:
Icon(
MaterialDesignIcons.getIconDataFromIconName(
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.getIconDataFromIconName(
linkedEntity.icon),
size: 24.0,
)
);
} else {
return Tab(
text: linkedEntity.displayName.toUpperCase(),
);
}
}
}
Widget build(BuildContext context) {
return ViewWidget(
view: this,
);
}
}