Card separation by type
This commit is contained in:
47
lib/ui_class/card.class.dart
Normal file
47
lib/ui_class/card.class.dart
Normal file
@ -0,0 +1,47 @@
|
||||
part of '../main.dart';
|
||||
|
||||
class HACard {
|
||||
List<Entity> entities = [];
|
||||
Entity linkedEntity;
|
||||
String name;
|
||||
String id;
|
||||
String type;
|
||||
|
||||
HACard({
|
||||
this.name,
|
||||
this.id,
|
||||
this.linkedEntity,
|
||||
@required this.type
|
||||
});
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
switch (type) {
|
||||
|
||||
case "entities": {
|
||||
return EntitiesCardWidget(
|
||||
card: this,
|
||||
);
|
||||
}
|
||||
|
||||
case "media-control": {
|
||||
return UnsupportedCardWidget(
|
||||
card: this,
|
||||
);
|
||||
}
|
||||
|
||||
default: {
|
||||
if ((linkedEntity == null) && (entities.isNotEmpty)) {
|
||||
return EntitiesCardWidget(
|
||||
card: this,
|
||||
);
|
||||
} else {
|
||||
return UnsupportedCardWidget(
|
||||
card: this,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
26
lib/ui_class/ui.dart
Normal file
26
lib/ui_class/ui.dart
Normal file
@ -0,0 +1,26 @@
|
||||
part of '../main.dart';
|
||||
|
||||
class HomeAssistantUI {
|
||||
List<HAView> views;
|
||||
|
||||
HomeAssistantUI() {
|
||||
views = [];
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return TabBarView(
|
||||
children: _buildViews(context)
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _buildViews(BuildContext context) {
|
||||
List<Widget> result = [];
|
||||
views.forEach((view) {
|
||||
result.add(
|
||||
view.build(context)
|
||||
);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
66
lib/ui_class/view.class.dart
Normal file
66
lib/ui_class/view.class.dart
Normal file
@ -0,0 +1,66 @@
|
||||
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 = [];
|
||||
childEntities.forEach((entity) {
|
||||
if (entity.isBadge) {
|
||||
badges.add(entity);
|
||||
TheLogger.debug("----Badge: ${entity.entityId}");
|
||||
} else {
|
||||
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"
|
||||
);
|
||||
TheLogger.debug("----Creating card: $groupIdToAdd");
|
||||
card.entities.add(entity);
|
||||
autoGeneratedCards.add(card);
|
||||
} else {
|
||||
autoGeneratedCards.firstWhere((card) => card.id == groupIdToAdd).entities.add(entity);
|
||||
}
|
||||
} else {
|
||||
TheLogger.debug("----Card: ${entity.entityId}");
|
||||
HACard card = HACard(
|
||||
name: entity.displayName,
|
||||
id: entity.entityId,
|
||||
linkedEntity: entity,
|
||||
type: "entities"
|
||||
);
|
||||
card.entities.addAll(entity.childEntities);
|
||||
cards.add(card);
|
||||
}
|
||||
}
|
||||
});
|
||||
cards.addAll(autoGeneratedCards);
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return ViewWidget(
|
||||
view: this,
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user