Handle entity taps and holds in one place

This commit is contained in:
Yegor Vialov 2018-11-23 15:06:42 +02:00
parent 67fbdb13c6
commit a0235ee385
7 changed files with 72 additions and 111 deletions

View File

@ -34,4 +34,5 @@ class EntityTapAction {
static const moreInfo = 'more-info'; static const moreInfo = 'more-info';
static const toggle = 'toggle'; static const toggle = 'toggle';
static const callService = 'call-service'; static const callService = 'call-service';
static const none = 'none';
} }

View File

@ -6,8 +6,10 @@ class EntityWrapper {
String icon; String icon;
String tapAction; String tapAction;
String holdAction; String holdAction;
String actionService; String tapActionService;
Map<String, dynamic> actionServiceData; Map<String, dynamic> tapActionServiceData;
String holdActionService;
Map<String, dynamic> holdActionServiceData;
Entity entity; Entity entity;
@ -16,12 +18,66 @@ class EntityWrapper {
String icon, String icon,
String displayName, String displayName,
this.tapAction: EntityTapAction.moreInfo, this.tapAction: EntityTapAction.moreInfo,
this.holdAction, this.holdAction: EntityTapAction.none,
this.actionService, this.tapActionService,
this.actionServiceData this.tapActionServiceData,
this.holdActionService,
this.holdActionServiceData
}) { }) {
this.icon = icon ?? entity.icon; this.icon = icon ?? entity.icon;
this.displayName = displayName ?? entity.displayName; this.displayName = displayName ?? entity.displayName;
} }
void handleTap() {
switch (tapAction) {
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entity.entityId, null));
break;
}
case EntityTapAction.callService: {
eventBus.fire(
ServiceCallEvent(tapActionService.split(".")[0], tapActionService.split(".")[1], null, tapActionServiceData));
break;
}
case EntityTapAction.none: {
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entity));
break;
}
}
}
void handleHold() {
switch (holdAction) {
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entity.entityId, null));
break;
}
case EntityTapAction.callService: {
eventBus.fire(
ServiceCallEvent(tapActionService.split(".")[0], tapActionService.split(".")[1], null, tapActionServiceData));
break;
}
case EntityTapAction.moreInfo: {
eventBus.fire(
new ShowEntityPageEvent(entity));
break;
}
default: {
break;
}
}
}
} }

View File

@ -22,44 +22,12 @@ class EntityIcon extends StatelessWidget {
), ),
onLongPress: () { onLongPress: () {
if (entityModel.handleTap) { if (entityModel.handleTap) {
switch (entityModel.entityWrapper.holdAction) { entityModel.entityWrapper.handleHold();
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null));
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entityModel.entityWrapper.entity));
break;
}
}
} }
}, },
onTap: () { onTap: () {
if (entityModel.handleTap) { if (entityModel.handleTap) {
switch (entityModel.entityWrapper.tapAction) { entityModel.entityWrapper.handleTap();
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null));
break;
}
case EntityTapAction.callService: {
eventBus.fire(
ServiceCallEvent(entityModel.entityWrapper.actionService.split(".")[0], entityModel.entityWrapper.actionService.split(".")[1], null, entityModel.entityWrapper.actionServiceData));
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entityModel.entityWrapper.entity));
break;
}
}
} }
} }

View File

@ -26,44 +26,12 @@ class EntityName extends StatelessWidget {
), ),
onLongPress: () { onLongPress: () {
if (entityModel.handleTap) { if (entityModel.handleTap) {
switch (entityModel.entityWrapper.holdAction) { entityModel.entityWrapper.handleHold();
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null));
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entityModel.entityWrapper.entity));
break;
}
}
} }
}, },
onTap: () { onTap: () {
if (entityModel.handleTap) { if (entityModel.handleTap) {
switch (entityModel.entityWrapper.tapAction) { entityModel.entityWrapper.handleTap();
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null));
break;
}
case EntityTapAction.callService: {
eventBus.fire(
ServiceCallEvent(entityModel.entityWrapper.actionService.split(".")[0], entityModel.entityWrapper.actionService.split(".")[1], null, entityModel.entityWrapper.actionServiceData));
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entityModel.entityWrapper.entity));
break;
}
}
} }
} }
); );

View File

@ -23,7 +23,7 @@ class GlanceEntityContainer extends StatelessWidget {
} }
result.add(EntityIcon( result.add(EntityIcon(
padding: EdgeInsets.all(0.0), padding: EdgeInsets.all(0.0),
iconSize: Sizes.largeIconSize, iconSize: Sizes.iconSize,
)); ));
if (showState) { if (showState) {
result.add(SimpleEntityState( result.add(SimpleEntityState(

View File

@ -25,44 +25,12 @@ class SimpleEntityState extends StatelessWidget {
)), )),
onLongPress: () { onLongPress: () {
if (entityModel.handleTap) { if (entityModel.handleTap) {
switch (entityModel.entityWrapper.holdAction) { entityModel.entityWrapper.handleHold();
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null));
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entityModel.entityWrapper.entity));
break;
}
}
} }
}, },
onTap: () { onTap: () {
if (entityModel.handleTap) { if (entityModel.handleTap) {
switch (entityModel.entityWrapper.tapAction) { entityModel.entityWrapper.handleHold();
case EntityTapAction.toggle: {
eventBus.fire(
ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null));
break;
}
case EntityTapAction.callService: {
eventBus.fire(
ServiceCallEvent(entityModel.entityWrapper.actionService.split(".")[0], entityModel.entityWrapper.actionService.split(".")[1], null, entityModel.entityWrapper.actionServiceData));
break;
}
default: {
eventBus.fire(
new ShowEntityPageEvent(entityModel.entityWrapper.entity));
break;
}
}
} }
} }
) )

View File

@ -453,9 +453,9 @@ class HomeAssistant {
displayName: rawEntity["name"], displayName: rawEntity["name"],
icon: rawEntity["icon"], icon: rawEntity["icon"],
tapAction: rawEntity["tap_action"] ?? EntityTapAction.moreInfo, tapAction: rawEntity["tap_action"] ?? EntityTapAction.moreInfo,
holdAction: rawEntity["hold_action"] ?? EntityTapAction.moreInfo, holdAction: rawEntity["hold_action"] ?? EntityTapAction.none,
actionService: rawEntity["service"], tapActionService: rawEntity["service"],
actionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId} tapActionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId}
) )
); );
} }