Skip to content

Commit 86ec49b

Browse files
8.2.1 (#49)
* [Feature] Dynamic state parent group (#48) * feat: add parentGroup field in createState message * bump SDK and TP Api version * Change parameter order * feat: use the name from the category of the categoryId if parentGroup isn't specified * fix: typo's in stateId fix: use correct categoryId * fix: add check for category id from constants * fix: break outer loop * feat: add CategoryHelper.getCategoryShortId method * core: Upgrade dependencies Co-authored-by: Pjiesco <55349095+Pjiesco@users.noreply.github.com>
1 parent e536310 commit 86ec49b

File tree

12 files changed

+116
-38
lines changed

12 files changed

+116
-38
lines changed

Annotations/src/main/java/com/christophecvb/touchportal/annotations/Category.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @see <a href="https://www.touch-portal.com/sdk/index.php?section=categories">TP Documentation: Categories</a>
3535
*/
36-
@Retention(RetentionPolicy.SOURCE)
36+
@Retention(RetentionPolicy.RUNTIME)
3737
@Target(ElementType.FIELD)
3838
public @interface Category {
3939
/**

AnnotationsProcessor/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ javadoc {
7777
}
7878

7979
dependencies {
80-
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
81-
implementation 'com.google.auto.service:auto-service:1.0-rc7'
82-
implementation group: 'com.squareup', name: 'javapoet', version: '1.12.1'
80+
implementation group: 'com.google.code.gson', name: 'gson', version: '2.9.0'
81+
implementation 'com.google.auto.service:auto-service:1.0.1'
82+
implementation group: 'com.squareup', name: 'javapoet', version: '1.13.0'
8383
implementation project(':Annotations')
8484
implementation project(':Helpers')
8585

86-
testImplementation group: 'junit', name: 'junit', version: '4.12'
86+
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
8787
}

Helpers/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ javadoc {
7777
}
7878

7979
dependencies {
80-
api group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
80+
api group: 'com.google.code.gson', name: 'gson', version: '2.9.0'
8181
api project(':Annotations')
8282
}

Helpers/src/main/java/com/christophecvb/touchportal/helpers/CategoryHelper.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.christophecvb.touchportal.annotations.Category;
2424

2525
import javax.lang.model.element.Element;
26+
import java.lang.reflect.Field;
2627

2728
/**
2829
* Touch Portal Plugin Category Helper
@@ -48,6 +49,17 @@ public static String getCategoryId(Element pluginElement, Element categoryElemen
4849
return CategoryHelper._getCategoryId(PluginHelper.getPluginId(pluginElement), category.id().isEmpty() ? categoryElement.getSimpleName().toString() : category.id());
4950
}
5051

52+
/**
53+
* Get Category Short ID
54+
*
55+
* @param categoryField {@link Field}
56+
* @param category {@link Category}
57+
* @return String categoryId
58+
*/
59+
public static String getCategoryShortId(Field categoryField, Category category) {
60+
return category.id().isEmpty() ? categoryField.getName() : category.id();
61+
}
62+
5163
/**
5264
* Get the generated Category Name
5365
*
@@ -59,6 +71,17 @@ public static String getCategoryName(Element categoryElement, Category category)
5971
return category.name().isEmpty() ? categoryElement.getSimpleName().toString() : category.name();
6072
}
6173

74+
/**
75+
* Get the generated Category Name
76+
*
77+
* @param categoryField {@link Field}
78+
* @param category {@link Category}
79+
* @return String categoryName
80+
*/
81+
public static String getCategoryName(Field categoryField, Category category) {
82+
return category.name().isEmpty() ? categoryField.getName() : category.name();
83+
}
84+
6285
/**
6386
* Get the generated Category ID
6487
*

Helpers/src/main/java/com/christophecvb/touchportal/helpers/PluginHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class PluginHelper {
4343
/**
4444
* Touch Portal Plugin System version
4545
*/
46-
public static final int TOUCH_PORTAL_PLUGIN_VERSION = 5;
46+
public static final int TOUCH_PORTAL_PLUGIN_VERSION = 6;
4747
/**
4848
* Argument passed to the jar to start the plugin
4949
*/

Helpers/src/main/java/com/christophecvb/touchportal/helpers/SentMessageHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ public class SentMessageHelper {
4747
public static final String OPTIONS = "options";
4848
public static final String CONNECTOR_ID = "connectorId";
4949
public static final String SHORT_ID = "shortId";
50+
public static final String PARENT_GROUP = "parentGroup";
5051

5152
}

Library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ dependencies {
8181
api project(':Annotations')
8282
api project(':Helpers')
8383

84-
api group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
84+
api group: 'com.google.code.gson', name: 'gson', version: '2.9.0'
8585

86-
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.7.2'
86+
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.3'
8787

88-
testImplementation group: 'junit', name: 'junit', version: '4.12'
88+
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
8989
testAnnotationProcessor project(':AnnotationsProcessor')
9090
}
9191

Library/src/main/java/com/christophecvb/touchportal/TouchPortalPlugin.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,21 @@ public boolean sendStateUpdate(String stateId, Object value, boolean allowEmptyV
712712
* @return boolean stateUpdateMessageSent
713713
*/
714714
public boolean sendCreateState(String categoryId, String stateId, String description, Object value) {
715-
return this.sendCreateState(categoryId, stateId, description, value, false, false);
715+
return this.sendCreateState(categoryId, stateId, null, description, value, false, false);
716+
}
717+
718+
/**
719+
* Send a Create a State Message to the Touch Portal Plugin System not allowing empty value
720+
*
721+
* @param categoryId String
722+
* @param stateId String
723+
* @param parentGroup String
724+
* @param description String
725+
* @param value Object
726+
* @return boolean stateUpdateMessageSent
727+
*/
728+
public boolean sendCreateState(String categoryId, String stateId, String parentGroup, String description, Object value) {
729+
return this.sendCreateState(categoryId, stateId, parentGroup, description, value, false, false);
716730
}
717731

718732
/**
@@ -727,6 +741,22 @@ public boolean sendCreateState(String categoryId, String stateId, String descrip
727741
* @return boolean stateCreateSent
728742
*/
729743
public boolean sendCreateState(String categoryId, String stateId, String description, Object value, boolean allowEmptyValue, boolean forceUpdate) {
744+
return this.sendCreateState(categoryId, stateId, null, description, value, allowEmptyValue, forceUpdate);
745+
}
746+
747+
/**
748+
* Send a Create a State Message to the Touch Portal Plugin System
749+
*
750+
* @param categoryId String
751+
* @param stateId String
752+
* @param parentGroup String
753+
* @param description String
754+
* @param value Object
755+
* @param allowEmptyValue boolean
756+
* @param forceUpdate boolean
757+
* @return boolean stateCreateSent
758+
*/
759+
public boolean sendCreateState(String categoryId, String stateId, String parentGroup, String description, Object value, boolean allowEmptyValue, boolean forceUpdate) {
730760
boolean sent = false;
731761
String valueStr = value != null ? String.valueOf(value) : null;
732762
if (categoryId != null && !categoryId.isEmpty() && stateId != null && !stateId.isEmpty() && description != null && !description.isEmpty() && valueStr != null && (allowEmptyValue || !valueStr.isEmpty())) {
@@ -737,6 +767,24 @@ public boolean sendCreateState(String categoryId, String stateId, String descrip
737767
createStateMessage.addProperty(SentMessageHelper.ID, stateId);
738768
createStateMessage.addProperty(SentMessageHelper.DESCRIPTION, description);
739769
createStateMessage.addProperty(SentMessageHelper.DEFAULT_VALUE, valueStr);
770+
if (parentGroup == null || parentGroup.isEmpty()) {
771+
classLoop:
772+
for (Class<?> subClass : this.pluginClass.getDeclaredClasses()) {
773+
for (Field subClassField : subClass.getDeclaredFields()) {
774+
if (subClassField.isAnnotationPresent(Category.class)) {
775+
Category category = subClassField.getAnnotation(Category.class);
776+
777+
if(categoryId.equals(CategoryHelper.getCategoryShortId(subClassField, category))) {
778+
createStateMessage.addProperty(SentMessageHelper.PARENT_GROUP, CategoryHelper.getCategoryName(subClassField, category));
779+
break classLoop;
780+
}
781+
}
782+
}
783+
}
784+
} else {
785+
createStateMessage.addProperty(SentMessageHelper.PARENT_GROUP, parentGroup);
786+
}
787+
740788
sent = this.send(createStateMessage);
741789
if (sent) {
742790
this.currentStates.put(stateId, valueStr);

Library/src/test/java/com/christophecvb/touchportal/test/LibraryTests.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -321,37 +321,43 @@ public void testDynamicStates() {
321321
assertFalse(this.touchPortalPluginTest.sendCreateState(null, null, null, ""));
322322
assertFalse(this.touchPortalPluginTest.sendCreateState("", "", "", ""));
323323

324-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", null, null, null));
325-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "", null, null));
326-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", null, "", null));
327-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", null, null, ""));
328-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "", "", ""));
324+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", null, null, null));
325+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "", null, null));
326+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", null, "", null));
327+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", null, null, ""));
328+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "", "", ""));
329329

330-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", null, null));
331-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "", null));
332-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", null, ""));
333-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "", ""));
330+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", null, null));
331+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "", null));
332+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", null, ""));
333+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "", ""));
334334

335-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", null));
336-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", ""));
335+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", null));
336+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", ""));
337337

338-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", null, true, false));
339-
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", "", true, false));
340-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", "", true, false));
341-
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "StateId", "Dynamically Created State", "", true, true));
338+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", null, true, false));
339+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "", true, false));
340+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "", true, false));
341+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "", true, true));
342+
343+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 01"));
344+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 01"));
345+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 02"));
346+
347+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Parent", "Dynamically Created State", "Default Value 01"));
348+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "", "Dynamically Created State", "Default Value 02"));
349+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", null, "Dynamically Created State", "Default Value 03"));
350+
assertTrue(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Parent", "Dynamically Created State", "Default Value 04", true, false));
342351

343-
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 01"));
344-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 01"));
345-
assertTrue(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 02"));
346352

347353
assertFalse(this.touchPortalPluginTest.sendRemoveState(null, null));
348354
assertFalse(this.touchPortalPluginTest.sendRemoveState("", null));
349355
assertFalse(this.touchPortalPluginTest.sendRemoveState(null, ""));
350356
assertFalse(this.touchPortalPluginTest.sendRemoveState("", ""));
351-
assertFalse(this.touchPortalPluginTest.sendRemoveState("CategoryId", null));
352-
assertFalse(this.touchPortalPluginTest.sendRemoveState("CategoryId", ""));
357+
assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", null));
358+
assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", ""));
353359

354-
assertTrue(this.touchPortalPluginTest.sendRemoveState("CategoryId", "SateId"));
360+
assertTrue(this.touchPortalPluginTest.sendRemoveState("BaseCategory", "StateId"));
355361
}
356362

357363
@Test
@@ -360,8 +366,8 @@ public void testSendFail() {
360366
assertFalse(this.touchPortalPluginTest.sendStateUpdate(TouchPortalPluginTestConstants.BaseCategory.States.CustomState.ID, "New Value"));
361367
assertFalse(this.touchPortalPluginTest.sendChoiceUpdate("listId", null, true));
362368
assertFalse(this.touchPortalPluginTest.sendSpecificChoiceUpdate("listId", "instanceId", null, true));
363-
assertFalse(this.touchPortalPluginTest.sendCreateState("CategoryId", "SateId", "Dynamically Created State", "Default Value 01"));
364-
assertFalse(this.touchPortalPluginTest.sendRemoveState("CategoryId", "SateId"));
369+
assertFalse(this.touchPortalPluginTest.sendCreateState("BaseCategory", "StateId", "Dynamically Created State", "Default Value 01"));
370+
assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", "StateId"));
365371
}
366372

367373
@Test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#TouchPortalPluginTest
2-
#Fri Jan 21 19:02:31 CET 2022
2+
#Mon May 23 10:54:55 CEST 2022
33
samplekey=Sample Value
44
plugin.version=1

0 commit comments

Comments
 (0)