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/ui_class/view.class.dart

114 lines
3.0 KiB
Dart
Raw Normal View History

2018-10-27 17:28:47 +03:00
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 = [];
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,
linkedEntity: 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 )) {
2018-10-27 17:28:47 +03:00
HACard card = HACard(
2018-11-11 19:51:02 +02:00
id: groupIdToAdd,
name: entity.domain,
type: "entities"
);
card.entities.add(entity);
autoGeneratedCards.add(card);
} else {
autoGeneratedCards.firstWhere((card) => card.id == groupIdToAdd).entities.add(entity);
}
} else {
HACard card = HACard(
name: entity.displayName,
id: entity.entityId,
linkedEntity: entity,
type: "entities"
);
card.entities.addAll(entity.childEntities.where((entity) {return entity.domain != "media_player";}));
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,
linkedEntity: entity,
2018-11-11 19:51:02 +02:00
type: "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) {
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(),
);
}
}
}
2018-10-27 17:28:47 +03:00
Widget build(BuildContext context) {
return ViewWidget(
view: this,
);
}
}