Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API 7] Event valueType text #67

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading