From a0235ee3850c00ebc0e075444d94259d751e7c7f Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Fri, 23 Nov 2018 15:06:42 +0200 Subject: [PATCH] Handle entity taps and holds in one place --- lib/entity_class/const.dart | 1 + lib/entity_class/entity_wrapper.class.dart | 66 +++++++++++++++++-- lib/entity_widgets/entity_icon.dart | 36 +--------- lib/entity_widgets/entity_name.dart | 36 +--------- .../glance_entity_container.dart | 2 +- lib/entity_widgets/state/simple_state.dart | 36 +--------- lib/home_assistant.class.dart | 6 +- 7 files changed, 72 insertions(+), 111 deletions(-) diff --git a/lib/entity_class/const.dart b/lib/entity_class/const.dart index f8864d6..0253c45 100644 --- a/lib/entity_class/const.dart +++ b/lib/entity_class/const.dart @@ -34,4 +34,5 @@ class EntityTapAction { static const moreInfo = 'more-info'; static const toggle = 'toggle'; static const callService = 'call-service'; + static const none = 'none'; } \ No newline at end of file diff --git a/lib/entity_class/entity_wrapper.class.dart b/lib/entity_class/entity_wrapper.class.dart index 89a1f63..a36adb9 100644 --- a/lib/entity_class/entity_wrapper.class.dart +++ b/lib/entity_class/entity_wrapper.class.dart @@ -6,8 +6,10 @@ class EntityWrapper { String icon; String tapAction; String holdAction; - String actionService; - Map actionServiceData; + String tapActionService; + Map tapActionServiceData; + String holdActionService; + Map holdActionServiceData; Entity entity; @@ -16,12 +18,66 @@ class EntityWrapper { String icon, String displayName, this.tapAction: EntityTapAction.moreInfo, - this.holdAction, - this.actionService, - this.actionServiceData + this.holdAction: EntityTapAction.none, + this.tapActionService, + this.tapActionServiceData, + this.holdActionService, + this.holdActionServiceData }) { this.icon = icon ?? entity.icon; 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; + } + } + } + } \ No newline at end of file diff --git a/lib/entity_widgets/entity_icon.dart b/lib/entity_widgets/entity_icon.dart index 54b7969..f94b0df 100644 --- a/lib/entity_widgets/entity_icon.dart +++ b/lib/entity_widgets/entity_icon.dart @@ -22,44 +22,12 @@ class EntityIcon extends StatelessWidget { ), onLongPress: () { if (entityModel.handleTap) { - switch (entityModel.entityWrapper.holdAction) { - case EntityTapAction.toggle: { - eventBus.fire( - ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null)); - break; - } - - default: { - eventBus.fire( - new ShowEntityPageEvent(entityModel.entityWrapper.entity)); - break; - } - } - + entityModel.entityWrapper.handleHold(); } }, onTap: () { if (entityModel.handleTap) { - switch (entityModel.entityWrapper.tapAction) { - 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; - } - } - + entityModel.entityWrapper.handleTap(); } } diff --git a/lib/entity_widgets/entity_name.dart b/lib/entity_widgets/entity_name.dart index 4f41154..4a73ef2 100644 --- a/lib/entity_widgets/entity_name.dart +++ b/lib/entity_widgets/entity_name.dart @@ -26,44 +26,12 @@ class EntityName extends StatelessWidget { ), onLongPress: () { if (entityModel.handleTap) { - switch (entityModel.entityWrapper.holdAction) { - case EntityTapAction.toggle: { - eventBus.fire( - ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null)); - break; - } - - default: { - eventBus.fire( - new ShowEntityPageEvent(entityModel.entityWrapper.entity)); - break; - } - } - + entityModel.entityWrapper.handleHold(); } }, onTap: () { if (entityModel.handleTap) { - switch (entityModel.entityWrapper.tapAction) { - 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; - } - } - + entityModel.entityWrapper.handleTap(); } } ); diff --git a/lib/entity_widgets/glance_entity_container.dart b/lib/entity_widgets/glance_entity_container.dart index cdfc01b..89adbef 100644 --- a/lib/entity_widgets/glance_entity_container.dart +++ b/lib/entity_widgets/glance_entity_container.dart @@ -23,7 +23,7 @@ class GlanceEntityContainer extends StatelessWidget { } result.add(EntityIcon( padding: EdgeInsets.all(0.0), - iconSize: Sizes.largeIconSize, + iconSize: Sizes.iconSize, )); if (showState) { result.add(SimpleEntityState( diff --git a/lib/entity_widgets/state/simple_state.dart b/lib/entity_widgets/state/simple_state.dart index 6ea43ae..ba72235 100644 --- a/lib/entity_widgets/state/simple_state.dart +++ b/lib/entity_widgets/state/simple_state.dart @@ -25,44 +25,12 @@ class SimpleEntityState extends StatelessWidget { )), onLongPress: () { if (entityModel.handleTap) { - switch (entityModel.entityWrapper.holdAction) { - case EntityTapAction.toggle: { - eventBus.fire( - ServiceCallEvent("homeassistant", "toggle", entityModel.entityWrapper.entity.entityId, null)); - break; - } - - default: { - eventBus.fire( - new ShowEntityPageEvent(entityModel.entityWrapper.entity)); - break; - } - } - + entityModel.entityWrapper.handleHold(); } }, onTap: () { if (entityModel.handleTap) { - switch (entityModel.entityWrapper.tapAction) { - 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; - } - } - + entityModel.entityWrapper.handleHold(); } } ) diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 8506601..2b50953 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -453,9 +453,9 @@ class HomeAssistant { displayName: rawEntity["name"], icon: rawEntity["icon"], tapAction: rawEntity["tap_action"] ?? EntityTapAction.moreInfo, - holdAction: rawEntity["hold_action"] ?? EntityTapAction.moreInfo, - actionService: rawEntity["service"], - actionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId} + holdAction: rawEntity["hold_action"] ?? EntityTapAction.none, + tapActionService: rawEntity["service"], + tapActionServiceData: rawEntity["service_data"] ?? {"entity_id": e.entityId} ) ); }