Skip to content

Commit

Permalink
[API 7] Event valueType text (#67)
Browse files Browse the repository at this point in the history
* Feat: Event valueType

* Update sample

* Get valueType from context

---------

Co-authored-by: Christophe Carvalho Vilas-Boas <[email protected]>
  • Loading branch information
Pjiesco and ChristopheCVB authored Nov 18, 2024
1 parent bd3073a commit f3de736
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.christophecvb.touchportal.annotations;

public enum ValueType {
TEXT("text"),
CHOICE("choice")
;

private final String key;

ValueType(String key) {
this.key = key;
}

public String getKey() {
return this.key;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.christophecvb.touchportal.annotations.processor;

import com.christophecvb.touchportal.annotations.Category;
import com.christophecvb.touchportal.annotations.Event;
import com.christophecvb.touchportal.annotations.Plugin;
import com.christophecvb.touchportal.annotations.State;
import com.christophecvb.touchportal.annotations.*;
import com.christophecvb.touchportal.annotations.processor.utils.Pair;
import com.christophecvb.touchportal.annotations.processor.utils.SpecUtils;
import com.christophecvb.touchportal.helpers.EventHelper;
Expand Down Expand Up @@ -38,12 +35,12 @@ public static Pair<JsonObject, TypeSpec.Builder> process(TouchPortalPluginAnnota
State state = eventElement.getAnnotation(State.class);
Event event = eventElement.getAnnotation(Event.class);

String reference = eventElement.getEnclosingElement().getSimpleName() + "." + eventElement.getSimpleName();

if (state == null) {
throw new TPAnnotationException.Builder(State.class).isMissing(true).forElement(eventElement).build();
}

String reference = eventElement.getEnclosingElement().getSimpleName() + "." + eventElement.getSimpleName();

TypeSpec.Builder eventTypeSpecBuilder = SpecUtils.createEventTypeSpecBuilder(pluginElement, categoryElement, category, eventElement, event);

JsonObject jsonEvent = new JsonObject();
Expand All @@ -56,19 +53,21 @@ public static Pair<JsonObject, TypeSpec.Builder> process(TouchPortalPluginAnnota
jsonEvent.addProperty(EventHelper.TYPE, EventHelper.TYPE_COMMUNICATE);
jsonEvent.addProperty(EventHelper.NAME, EventHelper.getEventName(eventElement, event));
jsonEvent.addProperty(EventHelper.FORMAT, event.format());
jsonEvent.addProperty(EventHelper.VALUE_STATE_ID, StateHelper.getStateId(pluginElement, categoryElement, category, eventElement, state));

String desiredTPType = GenericHelper.getTouchPortalType(reference, eventElement);
if (desiredTPType.equals(StateHelper.TYPE_TEXT)) {
jsonEvent.addProperty(EventHelper.VALUE_TYPE, EventHelper.VALUE_TYPE_CHOICE);
JsonArray eventValueChoices = new JsonArray();
for (String valueChoice : event.valueChoices()) {
eventValueChoices.add(valueChoice);

if (desiredTPType.equals(EventHelper.VALUE_TYPE)) {
if (event.valueChoices().length > 0) {
jsonEvent.addProperty(EventHelper.VALUE_TYPE, StateHelper.TYPE_CHOICE);
JsonArray eventValueChoices = new JsonArray();
for (String valueChoice : event.valueChoices()) {
eventValueChoices.add(valueChoice);
}
jsonEvent.add(EventHelper.VALUE_CHOICES, eventValueChoices);
} else {
jsonEvent.addProperty(EventHelper.VALUE_TYPE, StateHelper.TYPE_TEXT);
}
jsonEvent.add(EventHelper.VALUE_CHOICES, eventValueChoices);
jsonEvent.addProperty(EventHelper.VALUE_STATE_ID, StateHelper.getStateId(pluginElement, categoryElement, category, eventElement, state));
}
else {
throw new GenericHelper.TPTypeException.Builder(reference).typeUnsupported(desiredTPType).forAnnotation(GenericHelper.TPTypeException.ForAnnotation.EVENT).build();
}

return Pair.create(jsonEvent, eventTypeSpecBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public static TypeSpec.Builder createEventTypeSpecBuilder(Element pluginElement,
eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("name", EventHelper.getEventName(eventElement, event)));
eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("format", event.format()));
eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringArrayFieldSpec("value_choices", event.valueChoices()));
eventTypeSpecBuilder.addField(SpecUtils.getStaticFinalStringFieldSpec("value_type", event.valueType().getKey()));

return eventTypeSpecBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ private enum Categories {
* State and Event definition example
*/
@State(defaultValue = "1", categoryId = "BaseCategory")
@Event(valueChoices = {"1", "2"}, format = "When customStateWithEvent becomes $val")
@Event(valueChoices = {"1", "2"}, valueType = ValueType.CHOICE, format = "When customStateWithEvent becomes $val")
private String customStateWithEvent;

/**
* State and Event definition example
*/
@State(defaultValue = "1", categoryId = "BaseCategory")
@Event(valueType = ValueType.TEXT, format = "When customStateWithEvent becomes $val")
private String stateWithEventTypeText;
* State and Event in Subcategory definition example
*/
@State(defaultValue = "1", categoryId = "CategoryWithSubs")
Expand Down

0 comments on commit f3de736

Please sign in to comment.