Card separation by type

This commit is contained in:
Yegor Vialov 2018-10-27 17:28:47 +03:00
parent 9edfec7dff
commit 809c7d6355
9 changed files with 215 additions and 91 deletions

View File

@ -401,7 +401,8 @@ class HomeAssistant {
TheLogger.debug("------card: ${rawCard['type']}"); TheLogger.debug("------card: ${rawCard['type']}");
HACard card = HACard( HACard card = HACard(
id: "card", id: "card",
name: rawCard["title"] name: rawCard["title"],
type: rawCard['type']
); );
rawCard["entities"]?.forEach((rawEntity) { rawCard["entities"]?.forEach((rawEntity) {
if (rawEntity is String) { if (rawEntity is String) {
@ -414,6 +415,9 @@ class HomeAssistant {
} }
} }
}); });
if (rawCard["entity"] != null) {
card.linkedEntity = entities.get(rawCard["entity"]);
}
result.add(card); result.add(card);
} }
}); });

View File

@ -53,9 +53,13 @@ part 'entity.page.dart';
part 'utils.class.dart'; part 'utils.class.dart';
part 'mdi.class.dart'; part 'mdi.class.dart';
part 'entity_collection.class.dart'; part 'entity_collection.class.dart';
part 'ui/ui.dart'; part 'ui_class/ui.dart';
part 'ui/view.class.dart'; part 'ui_class/view.class.dart';
part 'ui/card.class.dart'; part 'ui_class/card.class.dart';
part 'ui_widgets/view.dart';
part 'ui_widgets/entities_card.dart';
part 'ui_widgets/unsupported_card.dart';
part 'ui_widgets/media_control_card.dart';
EventBus eventBus = new EventBus(); EventBus eventBus = new EventBus();
const String appName = "HA Client"; const String appName = "HA Client";

View 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,
);
}
}
}
}
}

View 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,
);
}
}

View File

@ -1,30 +1,10 @@
part of '../main.dart'; part of '../main.dart';
class HACard { class EntitiesCardWidget extends StatelessWidget {
List<Entity> entities = [];
Entity linkedEntity;
String name;
String id;
HACard({
this.name,
this.id,
this.linkedEntity
});
Widget build(BuildContext context) {
return HACardWidget(
card: this,
);
}
}
class HACardWidget extends StatelessWidget {
final HACard card; final HACard card;
const HACardWidget({ const EntitiesCardWidget({
Key key, Key key,
this.card this.card
}) : super(key: key); }) : super(key: key);

View File

@ -0,0 +1,27 @@
part of '../main.dart';
class MediaControlCardWidget extends StatelessWidget {
final HACard card;
const MediaControlCardWidget({
Key key,
this.card
}) : super(key: key);
@override
Widget build(BuildContext context) {
if ((card.linkedEntity!= null) && (card.linkedEntity.isHidden)) {
return Container(width: 0.0, height: 0.0,);
}
List<Widget> body = [];
return Card(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: body
)
);
}
}

View File

@ -0,0 +1,57 @@
part of '../main.dart';
class UnsupportedCardWidget extends StatelessWidget {
final HACard card;
const UnsupportedCardWidget({
Key key,
this.card
}) : super(key: key);
@override
Widget build(BuildContext context) {
if ((card.linkedEntity!= null) && (card.linkedEntity.isHidden)) {
return Container(width: 0.0, height: 0.0,);
}
List<Widget> body = [];
body.add(_buildCardHeader());
body.addAll(_buildCardBody(context));
return Card(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: body
)
);
}
Widget _buildCardHeader() {
return ListTile(
title: Text("${card.name ?? card.type}",
textAlign: TextAlign.left,
overflow: TextOverflow.ellipsis,
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
);
}
List<Widget> _buildCardBody(BuildContext context) {
List<Widget> result = [];
result.addAll(<Widget>[
Padding(
padding: EdgeInsets.fromLTRB(Entity.leftWidgetPadding, 0.0, Entity.rightWidgetPadding, 0.0),
child: Text("Card type '${card.type}' is not supported yet"),
),
Padding(
padding: EdgeInsets.fromLTRB(Entity.leftWidgetPadding, Entity.rowPadding, Entity.rightWidgetPadding, 0.0),
child: Text("Linked entity: ${card.linkedEntity?.entityId}"),
),
Padding(
padding: EdgeInsets.fromLTRB(Entity.leftWidgetPadding, Entity.rowPadding, Entity.rightWidgetPadding, Entity.rowPadding),
child: Text("Child entities: ${card.entities}"),
),
]);
return result;
}
}

View File

@ -1,82 +1,21 @@
part of '../main.dart'; part of '../main.dart';
class HAView { class ViewWidget extends StatefulWidget {
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
}) {
_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
);
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
);
card.entities.addAll(entity.childEntities);
cards.add(card);
}
}
});
cards.addAll(autoGeneratedCards);
}
Widget build(BuildContext context) {
return HAViewWidget(
view: this,
);
}
}
class HAViewWidget extends StatefulWidget {
final HAView view; final HAView view;
const HAViewWidget({ const ViewWidget({
Key key, Key key,
this.view this.view
}) : super(key: key); }) : super(key: key);
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
return HAViewWidgetState(); return ViewWidgetState();
} }
} }
class HAViewWidgetState extends State<HAViewWidget> { class ViewWidgetState extends State<ViewWidget> {
StreamSubscription _refreshDataSubscription; StreamSubscription _refreshDataSubscription;
Completer _refreshCompleter; Completer _refreshCompleter;