Skip to content

Commit

Permalink
Added optional index support for widgets and groups. This ensures tha…
Browse files Browse the repository at this point in the history
…t the order in which the widgets / groups were constructed is kept.
  • Loading branch information
ToxicStoxm committed Dec 23, 2024
1 parent 7183f95 commit 698a59e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/toxicstoxm/LEDSuite/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public static final class MenuReply {
public static final String FILENAME = "filename";
public static final String TYPE = "type";
public static final String VALUE = "value";
public static final String INDEX = "index";

public static final class Groups {
public static final String SUFFIX = "suffix";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.toxicstoxm.LEDSuite.communication.packet_management.packets.errors.ErrorCode;
import com.toxicstoxm.LEDSuite.logger.LEDSuiteLogAreas;
import com.toxicstoxm.LEDSuite.tools.ExceptionTools;
import com.toxicstoxm.LEDSuite.tools.YamlTools;
import com.toxicstoxm.LEDSuite.ui.LEDSuiteApplication;
import com.toxicstoxm.LEDSuite.ui.animation_menu.AnimationMenu;
import com.toxicstoxm.YAJSI.api.file.YamlConfiguration;
Expand All @@ -16,6 +17,9 @@
import org.gnome.adw.PreferencesGroup;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static com.toxicstoxm.LEDSuite.tools.YamlTools.checkIfKeyExists;
import static com.toxicstoxm.LEDSuite.tools.YamlTools.ensureKeyExists;

Expand Down Expand Up @@ -80,6 +84,9 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
ConfigurationSection menuContentSection = yaml.getConfigurationSection(Constants.Communication.YAML.Keys.Reply.MenuReply.CONTENT);
if (menuContentSection == null) throw new DeserializationException("Menu content section is empty!", ErrorCode.MenuContentKeyMissing);

List<PreferencesGroup> groups = new ArrayList<>();
List<PreferencesGroup> unsortedGroups = new ArrayList<>();

// Loop through the content widgets and ensure they are groups, because all top level widgets need to be groups
// If a type key exists check if it is a group
for (String menuGroupKey : menuContentSection.getKeys(false)) {
Expand All @@ -88,10 +95,29 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
if (menuGroupSection == null)
throw new DeserializationException("Menu group '" + menuGroupKey + "' section is empty!", ErrorCode.GroupSectionEmptyOrMissing);

try {
animationMenu.animationMenuContent.append(deserializeAnimationMenuGroup(animationMenu.getMenuID(), menuGroupKey, menuGroupSection));
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize animation menu group '" + menuGroupKey + "'!", e, e.getErrorCode());
int index = YamlTools.getIntIfAvailable(Constants.Communication.YAML.Keys.Reply.MenuReply.INDEX, -1, menuGroupSection);

PreferencesGroup group = deserializeAnimationMenuGroup(animationMenu.getMenuID(), menuGroupKey, menuGroupSection);

if (index < 0) {
unsortedGroups.add(group);
} else {
groups.set(index, group);
}
}

groups.addAll(unsortedGroups);

for (int i = 0; i < groups.size(); i++) {
PreferencesGroup group = groups.get(i);
if (group != null) {
try {
animationMenu.animationMenuContent.append(group);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize animation menu group '" + group.getName() + "'!", e, e.getErrorCode());
}
} else {
LEDSuiteApplication.getLogger().warn("Skipping group index '" + i + "', because the group at that position is 'null'!", new LEDSuiteLogAreas.COMMUNICATION());
}
}

Expand Down Expand Up @@ -127,7 +153,7 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
if (checkIfKeyExists(Constants.Communication.YAML.Keys.Reply.MenuReply.TOOLTIP, menuGroupSection)) {
menuGroup.setTooltipText(menuGroupSection.getString(Constants.Communication.YAML.Keys.Reply.MenuReply.TOOLTIP));
}

if (checkIfKeyExists(Constants.Communication.YAML.Keys.Reply.MenuReply.Groups.SUFFIX, menuGroupSection)) {
ConfigurationSection groupHeaderSuffixSection = menuGroupSection.getConfigurationSection(Constants.Communication.YAML.Keys.Reply.MenuReply.Groups.SUFFIX);
if (groupHeaderSuffixSection == null)
Expand Down Expand Up @@ -159,7 +185,11 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
throw new DeserializationException("Group content section is missing!", ErrorCode.GroupContentKeyMissing);

ConfigurationSection menuGroupContentSection = menuGroupSection.getConfigurationSection(Constants.Communication.YAML.Keys.Reply.MenuReply.CONTENT);
if (menuGroupContentSection == null) throw new DeserializationException("Group content section is empty!", ErrorCode.GroupContentSectionEmptyOrMissing);
if (menuGroupContentSection == null)
throw new DeserializationException("Group content section is empty!", ErrorCode.GroupContentSectionEmptyOrMissing);

List<DeserializableWidget> widgets = new ArrayList<>();
List<DeserializableWidget> unsortedWidgets = new ArrayList<>();

for (String widgetKey : menuGroupContentSection.getKeys(false)) {
ConfigurationSection widgetSection = menuGroupContentSection.getConfigurationSection(widgetKey);
Expand All @@ -174,20 +204,42 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
if (!isRegistered(widgetType))
throw new DeserializationException("Invalid / Unknown widget type '" + widgetType + "' for widget '" + widgetKey + "'!", ErrorCode.WidgetInvalidOrUnknownType);

try {
menuGroup.add(
get(widgetType).deserialize(
DeserializableWidget.builder()
.animationName(animationName)
.widgetSection(widgetSection)
.widgetKey(widgetKey)
.build()
)
);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize widget '" + widgetKey + "' with type '" + widgetType + "'!", e, e.getErrorCode());
int index = YamlTools.getIntIfAvailable(Constants.Communication.YAML.Keys.Reply.MenuReply.INDEX, -1, widgetSection);

DeserializableWidget deserializableWidget =
DeserializableWidget.builder()
.animationName(animationName)
.widgetSection(widgetSection)
.widgetKey(widgetKey)
.widgetType(widgetType)
.build();

if (index < 0) {
unsortedWidgets.add(deserializableWidget);
} else {
widgets.set(index, deserializableWidget);
}
}

widgets.addAll(unsortedWidgets);

for (int i = 0; i < widgets.size(); i++) {
DeserializableWidget widget = widgets.get(i);
if (widget != null) {
try {
menuGroup.add(
get(widget.widgetType()).deserialize(
widget
)
);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize widget '" + widget.widgetKey() + "' with type '" + widget.widgetType() + "'!", e, e.getErrorCode());
}
} else {
LEDSuiteApplication.getLogger().warn("Skipping widget index '" + i + "', because the widget at that position is 'null'!", new LEDSuiteLogAreas.COMMUNICATION());
}
}

return menuGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
* @since 1.0.0
*/
@Builder
public record DeserializableWidget(ConfigurationSection widgetSection, String widgetKey, String animationName) {}
public record DeserializableWidget(ConfigurationSection widgetSection, String widgetKey, String animationName, String widgetType) {}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ protected DeserializableWidget save() {
.build()
);
yaml.set(Constants.Communication.YAML.Keys.Reply.MenuReply.TYPE, getWidgetID());
return new DeserializableWidget(
yaml,
getWidgetID(),
getAnimationName()
);
return DeserializableWidget.builder()
.widgetSection(yaml)
.widgetKey(getWidgetID())
.animationName(getAnimationName())
.build();
}

protected void sendMenuChangeRequest(Object objectValue) {
Expand Down

0 comments on commit 698a59e

Please sign in to comment.