Skip to content

Commit

Permalink
Application: Replace post-update changelog with "check for updates"
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Jun 21, 2024
1 parent a7e975a commit 9100c02
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.shade.decima.model.app.ProjectManager;
import com.shade.decima.ui.editor.NodeEditorInputLazy;
import com.shade.decima.ui.editor.ProjectEditorInput;
import com.shade.decima.ui.menu.menus.HelpMenu;
import com.shade.decima.ui.navigator.NavigatorTree;
import com.shade.decima.ui.navigator.NavigatorTreeModel;
import com.shade.decima.ui.navigator.NavigatorView;
Expand Down Expand Up @@ -295,13 +294,6 @@ private static boolean isSameProject(@NotNull EditorInput input, @NotNull Projec
});

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent event) {
if (!BuildConfig.APP_VERSION.equals(preferences.get("version", BuildConfig.APP_VERSION))) {
HelpMenu.ChangelogItem.open();
}
}

@Override
public void windowClosing(WindowEvent e) {
final NavigatorTreeModel model = Application.getNavigator().getModel();
Expand Down Expand Up @@ -419,8 +411,6 @@ private void saveState() {
} catch (Exception e) {
log.warn("Unable to save window visuals", e);
}

preferences.put("version", BuildConfig.APP_VERSION);
}

private void saveWindow(@NotNull Preferences pref) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
package com.shade.decima.ui.menu.menus;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shade.decima.BuildConfig;
import com.shade.decima.ui.editor.html.HtmlEditorInput;
import com.shade.platform.model.runtime.ProgressMonitor;
import com.shade.decima.ui.updater.UpdateAvailableDialog;
import com.shade.platform.model.util.IOUtils;
import com.shade.platform.ui.editors.EditorInput;
import com.shade.platform.ui.editors.EditorManager;
import com.shade.platform.ui.editors.lazy.LazyEditorInput;
import com.shade.platform.ui.menus.Menu;
import com.shade.platform.ui.menus.MenuItem;
import com.shade.platform.ui.menus.*;
import com.shade.platform.ui.util.UIUtils;
import com.shade.util.NotNull;
import com.shade.util.Nullable;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodySubscribers;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

import static com.shade.decima.ui.menu.MenuConstants.*;

Expand All @@ -48,96 +34,11 @@ public void perform(@NotNull MenuItemContext ctx) {
}
}

@MenuItemRegistration(parent = APP_MENU_HELP_ID, name = "&Changelog", group = APP_MENU_HELP_GROUP_ABOUT, order = 1000)
public static class ChangelogItem extends MenuItem {
private static final Pattern COMMIT_PATTERN = Pattern.compile("([a-fA-F0-9]{40})");
private static final Pattern MENTION_PATTERN = Pattern.compile("@(\\w+)");
private static final Gson GSON = new Gson();

public static void open() {
EditorManager.getInstance().openEditor(new ChangelogEditorInputLazy(true), null, null, true, true, 0);
}

@MenuItemRegistration(parent = APP_MENU_HELP_ID, name = "&Check for Updates\u2026", group = APP_MENU_HELP_GROUP_ABOUT, order = 1000)
public static class CheckForUpdatesItem extends MenuItem {
@Override
public void perform(@NotNull MenuItemContext ctx) {
open();
}

private static class ChangelogEditorInput extends HtmlEditorInput {
public ChangelogEditorInput(@NotNull String body) {
super("Changelog", body);
}
}

private record ChangelogEditorInputLazy(boolean canLoadImmediately) implements LazyEditorInput {
@NotNull
@Override
public EditorInput loadRealInput(@NotNull ProgressMonitor monitor) throws Exception {
final var client = HttpClient.newHttpClient();
final var release = client.send(
HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/repos/ShadelessFox/decima/releases/tags/v%s".formatted(
BuildConfig.APP_VERSION
)))
.build(),
info -> BodySubscribers.<String, Map<String, Object>>mapping(
BodySubscribers.ofString(StandardCharsets.UTF_8),
body -> GSON.fromJson(body, new TypeToken<Map<String, Object>>() {}.getType())
)
);
final var markdown = client.send(
HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/markdown"))
.POST(BodyPublishers.ofString(GSON.toJson(Map.of(
"text", release.body().get("body"),
"mode", "gfm"
))))
.build(),
info -> BodySubscribers.mapping(
BodySubscribers.ofString(StandardCharsets.UTF_8),
body -> {
body = COMMIT_PATTERN
.matcher(body)
.replaceAll(result -> "<code><a href=\"https://github.com/ShadelessFox/decima/commit/%s\">%s</a></code>".formatted(
result.group(1),
result.group(1).substring(0, 7)
));
body = MENTION_PATTERN
.matcher(body)
.replaceAll("<a href=\"https://github.com/$1\">$0</a>");
return "<h1>What's new in %s %s</h1>%s".formatted(
BuildConfig.APP_TITLE,
BuildConfig.APP_VERSION,
body
);
}
)
);
return new ChangelogEditorInput(markdown.body());
}

@NotNull
@Override
public String getName() {
return "Changelog";
}

@Nullable
@Override
public String getDescription() {
return null;
}

@Override
public boolean representsSameResource(@NotNull EditorInput other) {
return other instanceof ChangelogEditorInput || other instanceof ChangelogEditorInputLazy;
}

@NotNull
@Override
public LazyEditorInput canLoadImmediately(boolean value) {
return new ChangelogEditorInputLazy(value);
}
UpdateAvailableDialog.checkForUpdates();
}
}

Expand Down
Loading

0 comments on commit 9100c02

Please sign in to comment.