Skip to content

Commit

Permalink
Refactor DownloadPage
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Aug 11, 2023
1 parent 5543cd1 commit 953558d
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,50 @@ protected ModDownloadPageSkin(DownloadPage control) {
for (String gameVersion : control.versions.keys().stream()
.sorted(VersionNumber.VERSION_COMPARATOR.reversed())
.collect(Collectors.toList())) {
ComponentList sublist = new ComponentList(() ->
control.versions.get(gameVersion).stream()
.map(version -> new ModItem(version, control))
.collect(Collectors.toList()));
ComponentList sublist = new ComponentList(() -> {
Collection<RemoteMod.Version> versions = control.versions.get(gameVersion);
List<Node> res = new ArrayList<>(versions.size() + 1);

VBox dependenciesBox = new VBox(8);
versions.stream().findFirst().ifPresent(dataItem -> {
if (control.repository.getType() == RemoteModRepository.Type.MOD) {
EnumMap<RemoteMod.DependencyType, List<DependencyModItem>> dependencies = new EnumMap<>(RemoteMod.DependencyType.class);
try {
for (RemoteMod.Dependency dependency : dataItem.getDependencies()) {
if (dependency.getType() == INCOMPATIBLE || dependency.getType() == BROKEN) {
continue;
}

if (!dependencies.containsKey(dependency.getType())) {
dependencies.put(dependency.getType(), new ArrayList<>());
}
dependencies.get(dependency.getType()).add(new DependencyModItem(control.page, dependency.load(), control.version, control.callback));
}
} catch (IOException exception) {
dependencies.clear();
Label msg = new Label(i18n("download.failed.refresh"));
msg.setPadding(new Insets(8));
dependenciesBox.getChildren().setAll(msg);
LOG.log(Level.WARNING, String.format("Fail to load dependencies of mod %s.", dataItem.getModid()), exception);
}

for (Map.Entry<RemoteMod.DependencyType, List<DependencyModItem>> entry : dependencies.entrySet()) {
Label title = new Label(i18n(DependencyModItem.I18N_KEY.get(entry.getKey())));
title.setPadding(new Insets(0, 8, 0, 8));
dependenciesBox.getChildren().setAll(title);
dependenciesBox.getChildren().addAll(entry.getValue());
}
}
});
dependenciesBox.setPadding(new Insets(0, 0, 8, 0));
res.add(dependenciesBox);

for (RemoteMod.Version version : versions) {
res.add(new ModItem(version, control));
}

return res;
});
sublist.getStyleClass().add("no-padding");
sublist.setTitle(gameVersion);

Expand Down Expand Up @@ -380,37 +420,6 @@ private static final class ModItem extends StackPane {
pane.setPadding(new Insets(8, 0, 8, 0));

{
if (selfPage.repository.getType() == RemoteModRepository.Type.MOD) {
List<Node> elements = new ArrayList<>();
EnumMap<RemoteMod.DependencyType, List<DependencyModItem>> dependencies = new EnumMap<>(RemoteMod.DependencyType.class);
try {
for (RemoteMod.Dependency dependency : dataItem.getDependencies()) {
if (dependency.getType() == INCOMPATIBLE || dependency.getType() == BROKEN) {
continue;
}

if (!dependencies.containsKey(dependency.getType())) {
dependencies.put(dependency.getType(), new ArrayList<>());
}
dependencies.get(dependency.getType()).add(new DependencyModItem(selfPage.page, dependency.load(), selfPage.version, selfPage.callback));
}
} catch (IOException exception) {
dependencies.clear();
Label msg = new Label(i18n("download.failed.refresh"));
msg.setPadding(new Insets(8));
elements.add(msg);
LOG.log(Level.WARNING, String.format("Fail to load dependencies of mod %s.", dataItem.getModid()), exception);
}

for (Map.Entry<RemoteMod.DependencyType, List<DependencyModItem>> entry : dependencies.entrySet()) {
Label title = new Label(i18n(DependencyModItem.I18N_KEY.get(entry.getKey())));
title.setPadding(new Insets(0, 8, 0, 8));
elements.add(title);
elements.addAll(entry.getValue());
}
pane.getChildren().addAll(elements);
}

HBox descPane = new HBox(8);
descPane.setPadding(new Insets(0, 8, 0, 8));
descPane.setAlignment(Pos.CENTER_LEFT);
Expand Down

0 comments on commit 953558d

Please sign in to comment.