Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit e2db50c

Browse files
authored
feat: support remote version check (#47)
1 parent a89650b commit e2db50c

File tree

11 files changed

+182
-14
lines changed

11 files changed

+182
-14
lines changed

app/build.gradle

+31
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,35 @@ dependencies {
7474

7575
configJavafxRun {
7676
dependsOn classes
77+
}
78+
79+
task updateVersion {
80+
def versionFileDir = projectDir.getAbsolutePath() + '/src/main/java/cc/cc1234/version/Version.java'
81+
def oldVersionStr = findOldVersionStr(versionFileDir)
82+
def newVersionValue = version.toString().split("-")[0]
83+
def newVersionStr = " public static final String VERSION = \"" + newVersionValue + "\";"
84+
//旧版本和新版本不一样, 进行替换
85+
if (!oldVersionStr.toString().trim().equals(newVersionStr.trim())) {
86+
def updatedContent = new File(versionFileDir).getText('UTF-8').replaceAll(oldVersionStr, newVersionStr)
87+
new File(versionFileDir).write(updatedContent, 'UTF-8')
88+
}
89+
}
90+
91+
def findOldVersionStr(path) {
92+
def readerString = ""
93+
new File(path).withReader('UTF-8') { reader ->
94+
reader.eachLine {
95+
//读取一行,如果发现有"VERSION"字符,说明是我们要修改的那一行。
96+
if (it.contains("VERSION")) {
97+
//保存it到string,然后返回
98+
readerString <<= it
99+
return readerString
100+
}
101+
}
102+
return readerString
103+
}
104+
}
105+
106+
compileJava {
107+
dependsOn updateVersion
77108
}

app/src/main/java/cc/cc1234/PrettyZooApplication.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package cc.cc1234;
22

3+
import cc.cc1234.app.context.HostServiceContext;
34
import cc.cc1234.app.context.PrimaryStageContext;
5+
import cc.cc1234.app.controller.MainViewController;
6+
import cc.cc1234.app.dialog.Dialog;
47
import cc.cc1234.app.facade.PrettyZooFacade;
58
import cc.cc1234.app.util.FXMLs;
9+
import cc.cc1234.version.Version;
10+
import cc.cc1234.version.VersionChecker;
611
import javafx.application.Application;
7-
import javafx.fxml.FXMLLoader;
812
import javafx.scene.Scene;
913
import javafx.scene.image.Image;
1014
import javafx.scene.layout.StackPane;
@@ -21,7 +25,7 @@ public class PrettyZooApplication extends Application {
2125
private PrettyZooFacade facade = new PrettyZooFacade();
2226

2327
@Override
24-
public void start(Stage primaryStage) throws Exception {
28+
public void start(Stage primaryStage) {
2529
v2(primaryStage);
2630
}
2731

@@ -36,14 +40,23 @@ public static void main(String[] args) {
3640
Application.launch(args);
3741
}
3842

39-
private void v2(Stage primaryStage) throws IOException {
43+
private void v2(Stage primaryStage) {
4044
PrimaryStageContext.set(primaryStage);
41-
final FXMLLoader loader = new FXMLLoader();
42-
loader.setLocation(FXMLs.loadFXML("fxml/MainView.fxml"));
43-
final StackPane anchorPane = loader.load();
44-
primaryStage.setScene(new Scene(anchorPane));
45+
HostServiceContext.set(getHostServices());
46+
MainViewController controller = FXMLs.getController("fxml/MainView.fxml");
47+
final StackPane stackPane = controller.getRootStackPane();
48+
49+
primaryStage.setScene(new Scene(stackPane));
4550
primaryStage.setTitle("PrettyZoo");
4651
getIconStream().ifPresent(stream -> primaryStage.getIcons().add(new Image(stream)));
52+
primaryStage.setOnShown(e -> {
53+
VersionChecker.hasNewVersion(latestVersion -> {
54+
final String content = String.format("你当前使用的是 %s, 目前最新版本为 %s, 请前往 Github 下载",
55+
Version.VERSION, latestVersion);
56+
controller.showNewVersionLabel(latestVersion);
57+
Dialog.confirm("升级提醒", content, HostServiceContext::jumpToReleases);
58+
});
59+
});
4760
primaryStage.show();
4861
}
4962

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cc.cc1234.app.context;
2+
3+
import javafx.application.HostServices;
4+
import javafx.application.Platform;
5+
6+
public class HostServiceContext {
7+
8+
private static volatile HostServices hostServices = null;
9+
10+
public static synchronized void set(HostServices service) {
11+
if (hostServices == null) {
12+
hostServices = service;
13+
}
14+
}
15+
16+
public static HostServices get() {
17+
return hostServices;
18+
}
19+
20+
public static void jumpToReleases() {
21+
Platform.runLater(() -> {
22+
hostServices.showDocument("https://github.com/vran-dev/PrettyZoo/releases");
23+
});
24+
}
25+
}

app/src/main/java/cc/cc1234/app/controller/MainViewController.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.cc1234.app.controller;
22

33
import cc.cc1234.app.cell.ZkServerListCell;
4+
import cc.cc1234.app.context.HostServiceContext;
45
import cc.cc1234.app.context.PrimaryStageContext;
56
import cc.cc1234.app.context.RootPaneContext;
67
import cc.cc1234.app.util.FXMLs;
@@ -9,10 +10,7 @@
910
import cc.cc1234.app.vo.ServerConfigVO;
1011
import javafx.application.Platform;
1112
import javafx.fxml.FXML;
12-
import javafx.scene.control.Button;
13-
import javafx.scene.control.ListCell;
14-
import javafx.scene.control.ListView;
15-
import javafx.scene.control.SplitPane;
13+
import javafx.scene.control.*;
1614
import javafx.scene.layout.AnchorPane;
1715
import javafx.scene.layout.HBox;
1816
import javafx.scene.layout.StackPane;
@@ -53,6 +51,9 @@ public class MainViewController {
5351
@FXML
5452
private Button importButton;
5553

54+
@FXML
55+
private Label newVersionLabel;
56+
5657
private ServerViewController serverViewController = FXMLs.getController("fxml/ServerView.fxml");
5758

5859

@@ -68,6 +69,7 @@ private void initialize() {
6869
RootPaneContext.set(rootStackPane);
6970
exportButton.setOnMouseClicked(e -> onExportAction());
7071
importButton.setOnMouseClicked(e -> onImportAction());
72+
newVersionLabel.setOnMouseClicked(e -> HostServiceContext.jumpToReleases());
7173
}
7274

7375
private void onExportAction() {
@@ -97,7 +99,6 @@ private void onImportAction() {
9799
});
98100
}
99101

100-
101102
private void showServerInfoView() {
102103
serverListView.itemsProperty().set(prettyZooConfigVO.getServers());
103104
serverListView.setCellFactory(cellCallback -> {
@@ -120,4 +121,12 @@ private void showServerInfoView() {
120121
});
121122
}
122123

124+
public StackPane getRootStackPane() {
125+
return rootStackPane;
126+
}
127+
128+
public void showNewVersionLabel(String newVersion) {
129+
newVersionLabel.setVisible(true);
130+
newVersionLabel.setTooltip(new Tooltip("最新版 " + newVersion + "已发布"));
131+
}
123132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.cc1234.version;
2+
3+
public class Version {
4+
5+
public static final String VERSION = "1.1.0";
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cc.cc1234.version;
2+
3+
import com.fasterxml.jackson.databind.json.JsonMapper;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
5+
import javafx.application.Platform;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.net.URI;
10+
import java.net.http.HttpClient;
11+
import java.net.http.HttpRequest;
12+
import java.net.http.HttpResponse;
13+
import java.util.function.Consumer;
14+
15+
public class VersionChecker {
16+
17+
private static final Logger logger = LoggerFactory.getLogger(VersionChecker.class);
18+
19+
public static void hasNewVersion(Consumer<String> runnable) {
20+
var request = HttpRequest.newBuilder(URI.create("https://api.github.com/repos/vran-dev/PrettyZoo/releases/latest"))
21+
.build();
22+
var client = HttpClient.newHttpClient();
23+
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
24+
.thenAccept(response -> {
25+
try {
26+
final JsonMapper mapper = new JsonMapper();
27+
final ObjectNode node = mapper.readValue(response.body(), ObjectNode.class);
28+
final String latestVersion = node.get("tag_name").asText();
29+
compareAndRun(latestVersion, runnable);
30+
} catch (Exception exception) {
31+
logger.error("check update failed", exception);
32+
}
33+
});
34+
}
35+
36+
private static void compareAndRun(String latestVersion, Consumer<String> runnable) {
37+
if (isLargerThanCurrent(latestVersion)) {
38+
Platform.runLater(() -> runnable.accept(latestVersion));
39+
}
40+
}
41+
42+
private static Boolean isLargerThanCurrent(String remoteVersion) {
43+
final String[] arr = remoteVersion.split("v");
44+
String r = remoteVersion;
45+
if (arr.length == 2) {
46+
r = arr[1];
47+
}
48+
49+
final String[] localVersionArr = Version.VERSION.split("\\.");
50+
final String[] remoteVersionArr = r.split("\\.");
51+
for (int i = 0; i < localVersionArr.length; i++) {
52+
try {
53+
final int localVersionSymbol = Integer.parseInt(localVersionArr[i]);
54+
final int remoteVersionSymbol = Integer.parseInt(remoteVersionArr[i]);
55+
if (localVersionSymbol < remoteVersionSymbol) {
56+
return true;
57+
} else if (localVersionSymbol > remoteVersionSymbol) {
58+
return false;
59+
}
60+
} catch (Exception e) {
61+
return true;
62+
}
63+
}
64+
return false;
65+
}
66+
}

app/src/main/java/module-info.java

+3
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
requires domain.main;
1111
requires spi.main;
1212
requires java.desktop;
13+
requires java.net.http;
14+
requires com.fasterxml.jackson.core;
15+
requires com.fasterxml.jackson.databind;
1316
}
1.59 KB
Loading

app/src/main/resources/assets/style.css

+14-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@
246246
-fx-background-repeat: no-repeat;
247247
-fx-background-position: center;
248248
-fx-border-color: transparent;
249-
-fx-background-radius: transparent;
250249
-fx-background-color: transparent;
251250
-fx-border-radius: 25;
252251
-fx-background-size: 22;
@@ -829,4 +828,18 @@
829828
-fx-border-color: #a0a0a0;
830829
-fx-border-radius: 0;
831830
-fx-background-radius: 0;
831+
}
832+
833+
834+
.new-version {
835+
-fx-background-color: transparent;
836+
-fx-background-image: url("img/new.png");
837+
-fx-background-repeat: no-repeat;
838+
-fx-background-position: center;
839+
-fx-background-size: 30;
840+
-fx-pref-height: 28;
841+
-fx-pref-width: 40;
842+
-fx-text-alignment: center;
843+
-fx-alignment: center;
844+
-fx-content-display: center;
832845
}

app/src/main/resources/fxml/MainView.fxml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Insets left="10.0" top="1.0" />
3434
</padding>
3535
</HBox>
36+
<Label fx:id="newVersionLabel" layoutX="142.0" layoutY="14.0" styleClass="new-version" visible="false" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="10.0" />
3637
</children>
3738
</AnchorPane>
3839
<StackPane fx:id="mainRightPane" style="-fx-background-color: F4F8FB;">

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ subprojects {
2121

2222
sourceCompatibility = 11
2323
group 'cc.c1234'
24-
version '1.0.0'
24+
version '1.1.0'
2525

2626
idea {
2727
module {

0 commit comments

Comments
 (0)