Skip to content

Commit

Permalink
Add some comments to the widget / group sorting logic to improve clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicStoxm committed Dec 25, 2024
1 parent 3f50205 commit 2cc7afa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ protected AutoRegisterModule<Widget> autoRegisterModule() {
.build();
}


/**
* Attempts to deserialize the given menu YAML string and construct a new animation menu object from it.
* @param menuYAML the YAML string to deserialize
Expand Down Expand Up @@ -97,10 +96,15 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
if (menuGroupSection == null)
throw new DeserializationException("Menu group '" + menuGroupKey + "' section is empty!", ErrorCode.GroupSectionEmptyOrMissing);

// Retrieve the groups index
// If non is present use -1
int index = YamlTools.getIntIfAvailable(Constants.Communication.YAML.Keys.Reply.MenuReply.INDEX, -1, menuGroupSection);

// Store the group in a temporary object for sorting
PreferencesGroup group = deserializeAnimationMenuGroup(animationMenu.getMenuID(), menuGroupKey, menuGroupSection);

// Add the group to the corresponding position in the groups tree map
// If no index is present store it in the unsorted groups list
if (index < 0) {
unsortedGroups.add(group);
} else {
Expand All @@ -112,11 +116,13 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
}
}

// Append all unsorted groups to the end of the sorted groups map
AtomicInteger highestIndex = new AtomicInteger(groups.lastKey());
unsortedGroups.forEach(entry -> {
groups.put(highestIndex.incrementAndGet(), entry);
});

// Loop through the sorted groups tree map and add them to the menu in the correct order
groups.forEach((index, group) -> {
try {
animationMenu.animationMenuContent.append(group);
Expand All @@ -139,7 +145,6 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
*/
private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animationName, @NotNull String menuGroupKey, @NotNull ConfigurationSection menuGroupSection) throws DeserializationException {


String topLevelWidgetType = menuGroupSection.getString(Constants.Communication.YAML.Keys.Reply.MenuReply.TYPE);
if (topLevelWidgetType == null)
throw new DeserializationException("Invalid widget type 'null' for top level group " + menuGroupKey + "'!", ErrorCode.WidgetMissingType);
Expand Down Expand Up @@ -195,6 +200,8 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
TreeMap<Integer, DeserializableWidget> widgets = new TreeMap<>();
List<DeserializableWidget> unsortedWidgets = new ArrayList<>();

// Loop through all widgets and check if their type is supported.
// Sort the widgets by index and store them in temporary objects.
for (String widgetKey : menuGroupContentSection.getKeys(false)) {
ConfigurationSection widgetSection = menuGroupContentSection.getConfigurationSection(widgetKey);
if (widgetSection == null)
Expand All @@ -210,6 +217,7 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation

int index = YamlTools.getIntIfAvailable(Constants.Communication.YAML.Keys.Reply.MenuReply.INDEX, -1, widgetSection);

// Store the widget temporarily for sorting
DeserializableWidget deserializableWidget =
DeserializableWidget.builder()
.animationName(animationName)
Expand All @@ -218,6 +226,8 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
.widgetType(widgetType)
.build();

// Add the widget to the corresponding position in the widgets tree map
// If no index is present store it in the unsorted widgets list
if (index < 0) {
unsortedWidgets.add(deserializableWidget);
} else {
Expand All @@ -229,11 +239,13 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
}
}

// Append all unsorted widgets to the end of the sorted widgets map
AtomicInteger highestIndex = new AtomicInteger(widgets.lastKey());
unsortedWidgets.forEach(entry -> {
widgets.put(highestIndex.incrementAndGet(), entry);
});

// Loop through the sorted widgets and append them to the parent group in the correct order.
widgets.forEach((index, deserializableWidget) -> {
try {
menuGroup.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public ExpanderRow deserialize(@NotNull DeserializableWidget deserializableWidge
TreeMap<Integer, DeserializableWidget> widgets = new TreeMap<>();
List<DeserializableWidget> unsortedWidgets = new ArrayList<>();

// Loop through all widgets and check if their type is supported.
// Sort the widgets by index and store them in temporary objects.
for (String widgetKey : contentSection.getKeys(false)) {
ConfigurationSection widgetSection = contentSection.getConfigurationSection(widgetKey);
if (widgetSection == null)
Expand All @@ -73,6 +75,7 @@ public ExpanderRow deserialize(@NotNull DeserializableWidget deserializableWidge

int index = YamlTools.getIntIfAvailable(Constants.Communication.YAML.Keys.Reply.MenuReply.INDEX, -1, widgetSection);

// Store the widget temporarily for sorting
DeserializableWidget expanderRowChild =
DeserializableWidget.builder()
.widgetType(type)
Expand All @@ -81,6 +84,8 @@ public ExpanderRow deserialize(@NotNull DeserializableWidget deserializableWidge
.animationName(animationName)
.build();

// Add the widget to the corresponding position in the widgets tree map
// If no index is present store it in the unsorted widgets list
if (index < 0) {
unsortedWidgets.add(expanderRowChild);
} else {
Expand All @@ -92,11 +97,13 @@ public ExpanderRow deserialize(@NotNull DeserializableWidget deserializableWidge
}
}

// Append all unsorted widgets to the end of the sorted widgets map
AtomicInteger highestIndex = new AtomicInteger(widgets.lastKey());
unsortedWidgets.forEach(entry -> {
widgets.put(highestIndex.incrementAndGet(), entry);
});

// Loop through the sorted widgets and append them to the parent group in the correct order.
widgets.forEach((index, expanderRowChild) -> {
try {
AnimationMenuManager animationMenuManager = getAnimationMenuManager(expanderRowChild.widgetKey(), expanderRowChild.widgetType());
Expand Down

0 comments on commit 2cc7afa

Please sign in to comment.