Refactoring: badges and cards

This commit is contained in:
estevez 2018-09-28 11:18:37 +03:00
parent 098a556279
commit 49444ab3df
3 changed files with 36 additions and 29 deletions

View File

@ -214,7 +214,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
result.add( result.add(
Wrap( Wrap(
alignment: WrapAlignment.center, alignment: WrapAlignment.center,
spacing: 10.0, spacing: 12.0,
runSpacing: 4.0, runSpacing: 4.0,
children: _buildBadges(view.badges), children: _buildBadges(view.badges),
) )

View File

@ -3,7 +3,7 @@ part of 'main.dart';
class UIBuilder { class UIBuilder {
EntityCollection _entities; EntityCollection _entities;
Map<String, View> _views; Map<String, View> _views;
List _topBadgeDomains = ["alarm_control_panel", "binary_sensor", "device_tracker", "updater", "sun", "timer", "sensor"]; static List badgeDomains = ["alarm_control_panel", "binary_sensor", "device_tracker", "updater", "sun", "timer", "sensor"];
bool get isEmpty => _views.length == 0; bool get isEmpty => _views.length == 0;
Map<String, View> get views => _views ?? {}; Map<String, View> get views => _views ?? {};
@ -12,6 +12,10 @@ class UIBuilder {
_views = {}; _views = {};
} }
static bool isBadge(String domain) {
return badgeDomains.contains(domain);
}
void build(EntityCollection entitiesCollection) { void build(EntityCollection entitiesCollection) {
_entities = entitiesCollection; _entities = entitiesCollection;
_views.clear(); _views.clear();
@ -36,18 +40,7 @@ class UIBuilder {
viewGroupEntity.childEntities.forEach(( viewGroupEntity.childEntities.forEach((
entityId) { //Each entity or group in view entityId) { //Each entity or group in view
if (_entities.isExist(entityId)) { if (_entities.isExist(entityId)) {
Entity entityToAdd = _entities.get(entityId); view.add(_entities.get(entityId));
if (!entityToAdd.isGroup) {
if (_topBadgeDomains.contains(entityToAdd.domain)) {
//This is badge
view.addBadge(entityId);
} else {
//This is a standalone entity
view.addEntityWithoutGroup(entityToAdd);
}
} else {
view.addCardWithEntities(entityId, entityToAdd.displayName, entityToAdd.childEntities);
}
} else { } else {
TheLogger.log("Warning", "Unknown entity inside view: $entityId"); TheLogger.log("Warning", "Unknown entity inside view: $entityId");
} }

View File

@ -15,25 +15,39 @@ class View {
badges = {}; badges = {};
} }
void addBadge(String entityId) { void add(Entity entity) {
if (!entity.isGroup) {
_addEntityWithoutGroup(entity);
} else {
_addCardWithEntities(entity);
}
}
void _addBadge(String entityId) {
badges.addAll({entityId: Badge(entityId)}); badges.addAll({entityId: Badge(entityId)});
} }
void addEntityWithoutGroup(Entity entity) { void _addEntityWithoutGroup(Entity entity) {
if (UIBuilder.isBadge(entity.domain)) {
//This is badge
_addBadge(entity.entityId);
} else {
//This is a standalone entity
String groupIdToAdd = "${entity.domain}.${entity.domain}$_count"; String groupIdToAdd = "${entity.domain}.${entity.domain}$_count";
if (cards[groupIdToAdd] == null) { if (cards[groupIdToAdd] == null) {
addCard(groupIdToAdd, entity.domain); _addCard(groupIdToAdd, entity.domain);
} }
cards[groupIdToAdd].addEntity(entity.entityId); cards[groupIdToAdd].addEntity(entity.entityId);
} }
}
void addCard(String entityId, String friendlyName) { void _addCard(String entityId, String friendlyName) {
cards.addAll({"$entityId": HACard(entityId, friendlyName)}); cards.addAll({"$entityId": HACard(entityId, friendlyName)});
} }
void addCardWithEntities(String entityId, String friendlyName, List entities) { void _addCardWithEntities(Entity entity) {
cards.addAll({"$entityId": HACard(entityId, friendlyName)}); cards.addAll({"${entity.entityId}": HACard(entity.entityId, entity.displayName)});
cards[entityId].addEntities(entities); cards[entity.entityId].addEntities(entity.childEntities);
} }
} }