Resolves #206 Entity button with tap and hold events

This commit is contained in:
Yegor Vialov 2018-11-25 18:09:06 +02:00
parent 5633e30448
commit 20b1b90e39
9 changed files with 63 additions and 46 deletions

View File

@ -65,18 +65,22 @@ class GlanceEntityContainer extends StatelessWidget {
fontSize: nameFontSize, fontSize: nameFontSize,
)); ));
} }
if (expanded) { if (expanded) {
return Container( return InkWell(
child: InkWell( onTap: () => entityWrapper.handleTap(),
child: Column( onLongPress: () => entityWrapper.handleHold(),
mainAxisSize: MainAxisSize.min, child: ConstrainedBox(
//mainAxisAlignment: MainAxisAlignment.start, constraints: BoxConstraints(maxHeight: 100.0),
//crossAxisAlignment: CrossAxisAlignment.center, child: FittedBox(
children: result, fit: BoxFit.fitHeight,
child: Column(
mainAxisSize: MainAxisSize.min,
//mainAxisAlignment: MainAxisAlignment.start,
//crossAxisAlignment: CrossAxisAlignment.center,
children: result,
),
), ),
onTap: () => entityWrapper.handleTap(),
onLongPress: () => entityWrapper.handleHold(),
), ),
); );
} else { } else {

View File

@ -447,38 +447,49 @@ class HomeAssistant {
} else { } else {
if (entities.isExist(rawEntity["entity"])) { if (entities.isExist(rawEntity["entity"])) {
Entity e = entities.get(rawEntity["entity"]); Entity e = entities.get(rawEntity["entity"]);
String tapAction = EntityTapAction.moreInfo; String tapAction;
String holdAction = EntityTapAction.none; String holdAction;
if (card.type == CardType.glance) { if (card.type == CardType.glance && card.type == CardType.entityButton) {
tapAction = rawEntity["tap_action"] ?? EntityTapAction.moreInfo; tapAction = rawEntity["tap_action"] ?? EntityTapAction.moreInfo;
holdAction = rawEntity["hold_action"] ?? EntityTapAction.none; holdAction = rawEntity["hold_action"] ?? EntityTapAction.none;
} else {
tapAction = EntityTapAction.moreInfo;
holdAction = EntityTapAction.none;
} }
card.entities.add( card.entities.add(
EntityWrapper( EntityWrapper(
entity: e, entity: e,
displayName: rawEntity["name"], displayName: rawEntity["name"],
icon: rawEntity["icon"], icon: rawEntity["icon"],
tapAction: tapAction, tapAction: tapAction,
holdAction: holdAction, holdAction: holdAction,
tapActionService: rawEntity["service"], tapActionService: rawEntity["service"],
tapActionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId} tapActionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId}
) )
); );
} }
} }
}); });
if (rawCard["entity"] != null) { if (rawCard["entity"] != null) {
var en = rawCard["entity"]; var en = rawCard["entity"];
String tapAction = rawCard["tap_action"] ?? EntityTapAction.moreInfo;
String holdAction = rawCard["hold_action"] ?? EntityTapAction.none;
if (en is String) { if (en is String) {
if (entities.isExist(en)) { if (entities.isExist(en)) {
card.linkedEntity = EntityWrapper(entity: entities.get(en)); card.linkedEntityWrapper = EntityWrapper(
entity: entities.get(en),
tapAction: tapAction,
holdAction: holdAction
);
} }
} else { } else {
if (entities.isExist(en["entity"])) { if (entities.isExist(en["entity"])) {
card.linkedEntity = EntityWrapper( card.linkedEntityWrapper = EntityWrapper(
entity: entities.get(en["entity"]), entity: entities.get(en["entity"]),
icon: en["icon"], icon: en["icon"],
displayName: en["name"] displayName: en["name"],
tapAction: tapAction,
holdAction: holdAction
); );
} }
} }

View File

@ -2,7 +2,7 @@ part of '../main.dart';
class HACard { class HACard {
List<EntityWrapper> entities = []; List<EntityWrapper> entities = [];
EntityWrapper linkedEntity; EntityWrapper linkedEntityWrapper;
String name; String name;
String id; String id;
String type; String type;
@ -13,7 +13,7 @@ class HACard {
HACard({ HACard({
this.name, this.name,
this.id, this.id,
this.linkedEntity, this.linkedEntityWrapper,
this.columnsCount: 4, this.columnsCount: 4,
this.showName: true, this.showName: true,
this.showState: true, this.showState: true,
@ -65,7 +65,7 @@ class HACard {
} }
default: { default: {
if ((linkedEntity == null) && (entities.isNotEmpty)) { if ((linkedEntityWrapper == null) && (entities.isNotEmpty)) {
return EntitiesCardWidget( return EntitiesCardWidget(
card: this, card: this,
); );

View File

@ -28,8 +28,8 @@ class HAView {
HACard card = HACard( HACard card = HACard(
name: e.displayName, name: e.displayName,
id: e.entityId, id: e.entityId,
linkedEntity: EntityWrapper(entity: e), linkedEntityWrapper: EntityWrapper(entity: e),
type: "media-control" type: CardType.mediaControl
); );
cards.add(card); cards.add(card);
}); });
@ -40,7 +40,7 @@ class HAView {
HACard card = HACard( HACard card = HACard(
id: groupIdToAdd, id: groupIdToAdd,
name: entity.domain, name: entity.domain,
type: "entities" type: CardType.entities
); );
card.entities.add(EntityWrapper(entity: entity)); card.entities.add(EntityWrapper(entity: entity));
autoGeneratedCards.add(card); autoGeneratedCards.add(card);
@ -51,16 +51,16 @@ class HAView {
HACard card = HACard( HACard card = HACard(
name: entity.displayName, name: entity.displayName,
id: entity.entityId, id: entity.entityId,
linkedEntity: EntityWrapper(entity: entity), linkedEntityWrapper: EntityWrapper(entity: entity),
type: "entities" type: CardType.entities
); );
card.entities.addAll(entity.childEntities.where((entity) {return entity.domain != "media_player";}).map((e) {return EntityWrapper(entity: e);})); 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){ entity.childEntities.where((entity) {return entity.domain == "media_player";}).forEach((entity){
HACard mediaCard = HACard( HACard mediaCard = HACard(
name: entity.displayName, name: entity.displayName,
id: entity.entityId, id: entity.entityId,
linkedEntity: EntityWrapper(entity: entity), linkedEntityWrapper: EntityWrapper(entity: entity),
type: "media-control" type: CardType.mediaControl
); );
cards.add(mediaCard); cards.add(mediaCard);
}); });

View File

@ -11,7 +11,7 @@ class EntitiesCardWidget extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if ((card.linkedEntity!= null) && (card.linkedEntity.entity.isHidden)) { if ((card.linkedEntityWrapper!= null) && (card.linkedEntityWrapper.entity.isHidden)) {
return Container(width: 0.0, height: 0.0,); return Container(width: 0.0, height: 0.0,);
} }
List<Widget> body = []; List<Widget> body = [];

View File

@ -11,15 +11,17 @@ class EntityButtonCardWidget extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (card.linkedEntity!= null && card.linkedEntity.entity.isHidden) { if (card.linkedEntityWrapper!= null && card.linkedEntityWrapper.entity.isHidden) {
return Container(width: 0.0, height: 0.0,); return Container(width: 0.0, height: 0.0,);
} }
card.linkedEntity.displayName = card.name; if (card.name != null) {
card.linkedEntityWrapper.displayName = card.name;
}
return Card( return Card(
child: Padding( child: Padding(
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, Sizes.rowPadding), padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, Sizes.rowPadding),
child: EntityModel( child: EntityModel(
entityWrapper: card.linkedEntity, entityWrapper: card.linkedEntityWrapper,
child: GlanceEntityContainer( child: GlanceEntityContainer(
showName: true, showName: true,
showState: false, showState: false,

View File

@ -11,7 +11,7 @@ class GlanceCardWidget extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if ((card.linkedEntity!= null) && (card.linkedEntity.entity.isHidden)) { if ((card.linkedEntityWrapper!= null) && (card.linkedEntityWrapper.entity.isHidden)) {
return Container(width: 0.0, height: 0.0,); return Container(width: 0.0, height: 0.0,);
} }
List<Widget> rows = []; List<Widget> rows = [];

View File

@ -11,13 +11,13 @@ class MediaControlCardWidget extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if ((card.linkedEntity == null) || (card.linkedEntity.entity.isHidden)) { if ((card.linkedEntityWrapper == null) || (card.linkedEntityWrapper.entity.isHidden)) {
return Container(width: 0.0, height: 0.0,); return Container(width: 0.0, height: 0.0,);
} }
return Card( return Card(
child: EntityModel( child: EntityModel(
entityWrapper: card.linkedEntity, entityWrapper: card.linkedEntityWrapper,
handleTap: null, handleTap: null,
child: MediaPlayerWidget() child: MediaPlayerWidget()
) )

View File

@ -11,7 +11,7 @@ class UnsupportedCardWidget extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if ((card.linkedEntity!= null) && (card.linkedEntity.entity.isHidden)) { if ((card.linkedEntityWrapper!= null) && (card.linkedEntityWrapper.entity.isHidden)) {
return Container(width: 0.0, height: 0.0,); return Container(width: 0.0, height: 0.0,);
} }
List<Widget> body = []; List<Widget> body = [];
@ -28,14 +28,14 @@ class UnsupportedCardWidget extends StatelessWidget {
List<Widget> _buildCardBody(BuildContext context) { List<Widget> _buildCardBody(BuildContext context) {
List<Widget> result = []; List<Widget> result = [];
if (card.linkedEntity != null) { if (card.linkedEntityWrapper != null) {
result.addAll(<Widget>[ result.addAll(<Widget>[
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, Sizes.rowPadding, 0.0, Sizes.rowPadding), padding: EdgeInsets.fromLTRB(0.0, Sizes.rowPadding, 0.0, Sizes.rowPadding),
child: EntityModel( child: EntityModel(
entityWrapper: card.linkedEntity, entityWrapper: card.linkedEntityWrapper,
handleTap: true, handleTap: true,
child: card.linkedEntity.entity.buildDefaultWidget(context) child: card.linkedEntityWrapper.entity.buildDefaultWidget(context)
), ),
) )
]); ]);