From 2aa586db5e74e47a7748a75bfc12e9db18b23c38 Mon Sep 17 00:00:00 2001 From: Ogefest Date: Sat, 20 Feb 2021 19:15:33 +0100 Subject: [PATCH] check new version every 24h and show popup if new version appear --- build.gradle | 5 +- .../java/notepack/MainViewController.java | 1 + .../java/notepack/NewVersionController.java | 114 ++++++++++++++++++ .../java/notepack/app/domain/Version.java | 15 ++- .../notepack/app/task/NewVersionCheck.java | 54 +++++++++ .../resources/notepack/NewVersionPopup.fxml | 33 +++++ 6 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 src/main/java/notepack/NewVersionController.java create mode 100644 src/main/java/notepack/app/task/NewVersionCheck.java create mode 100644 src/main/resources/notepack/NewVersionPopup.fxml diff --git a/build.gradle b/build.gradle index 13acf07..4fb6aff 100644 --- a/build.gradle +++ b/build.gradle @@ -115,13 +115,14 @@ jpackage { } processResources { - doFirst { + outputs.upToDateWhen { false } + doLast { def date = new Date() def formattedDate = date.format('yyyyMMddHHmmss') - def resourcesDir = sourceSets.main.output.resourcesDir resourcesDir.mkdirs() def contents = "name=$project.name\nversion=" + getCurrentGitTag() + "\nbuild=" + formattedDate + "\n" + println contents new File(resourcesDir, "build-info.properties").text = contents } } diff --git a/src/main/java/notepack/MainViewController.java b/src/main/java/notepack/MainViewController.java index 133d037..cc6233e 100644 --- a/src/main/java/notepack/MainViewController.java +++ b/src/main/java/notepack/MainViewController.java @@ -128,6 +128,7 @@ public void windowRestore() { app.getTheme().set(cssFile, mainScene); app.addTask(new InitializeShortcuts()); + app.addTask(new NewVersionCheck(hostServices)); app.startDispatcher(); } diff --git a/src/main/java/notepack/NewVersionController.java b/src/main/java/notepack/NewVersionController.java new file mode 100644 index 0000000..323a776 --- /dev/null +++ b/src/main/java/notepack/NewVersionController.java @@ -0,0 +1,114 @@ +package notepack; + +import javafx.application.HostServices; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import notepack.app.domain.PopupController; +import notepack.app.domain.Version; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ResourceBundle; + +/** + * FXML Controller class + */ +public class NewVersionController extends PopupController implements Initializable { + + @FXML + private Button btnCancel; + + @FXML + private Label versionLabel; + + public HostServices hostServices; + + private String urlWithVersion; + private boolean isNewVersionAvailable = false; + + /** + * Initializes the controller class. + */ + @Override + public void initialize(URL url, ResourceBundle rb) { + + } + + public void setHostServices(HostServices hostServices) { + this.hostServices = hostServices; + } + + public boolean isNewVersionAvailable() { + + String currentVersion = Version.app(); + String currentBuild = Version.build(); + String[] parts = currentVersion.split("\\."); + + int v1 = 0; + int v2 = 0; + int v3 = 0; + int g1 = 0; + int g2 = 0; + int g3 = 0; + + if (parts.length == 3) { + v1 = Integer.parseInt(parts[0]); + v2 = Integer.parseInt(parts[1]); + v3 = Integer.parseInt(parts[2]); + } + + HttpURLConnection con = null; + try { + URL appVersionUrl = new URL("https://notepackapp.com/version.json?build=" + currentBuild + "&version=" + currentVersion); + con = (HttpURLConnection) appVersionUrl.openConnection(); + con.setRequestMethod("GET"); + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer content = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + + JSONObject jsonObject = new JSONObject(content.toString()); + String githubVersion = jsonObject.getString("version"); + String[] ghParts = githubVersion.split("\\."); + + if (ghParts.length == 3) { + g1 = Integer.parseInt(ghParts[0]); + g2 = Integer.parseInt(ghParts[1]); + g3 = Integer.parseInt(ghParts[2]); + } + + urlWithVersion = jsonObject.getString("url"); + + } catch (IOException e) { + isNewVersionAvailable = false; + } + + if (g1 > v1 || g2 > v2 || g3 > v3) { + isNewVersionAvailable = true; + } + + return this.isNewVersionAvailable; + } + + @FXML + private void onCancel(ActionEvent event) { + getTaskUtil().closePopup(); + } + + @FXML + private void onOpen(ActionEvent event) { + hostServices.showDocument(urlWithVersion); + } + +} diff --git a/src/main/java/notepack/app/domain/Version.java b/src/main/java/notepack/app/domain/Version.java index 265e5de..d124c32 100644 --- a/src/main/java/notepack/app/domain/Version.java +++ b/src/main/java/notepack/app/domain/Version.java @@ -13,7 +13,20 @@ public static String app() { props.load(in); version = (String) props.get("version"); } catch (IOException e) { - e.printStackTrace(); + // run locally + } + return version; + } + + public static String build() { + InputStream in = Version.class.getClassLoader().getResourceAsStream("build-info.properties"); + Properties props = new Properties(); + String version = "unknown"; + try { + props.load(in); + version = (String) props.get("build"); + } catch (IOException e) { + // run locally } return version; } diff --git a/src/main/java/notepack/app/task/NewVersionCheck.java b/src/main/java/notepack/app/task/NewVersionCheck.java new file mode 100644 index 0000000..1538c69 --- /dev/null +++ b/src/main/java/notepack/app/task/NewVersionCheck.java @@ -0,0 +1,54 @@ +package notepack.app.task; + +import javafx.application.HostServices; +import javafx.fxml.FXMLLoader; +import javafx.scene.layout.AnchorPane; +import notepack.MainViewController; +import notepack.NewVersionController; +import notepack.app.domain.App; +import notepack.app.domain.Task; +import notepack.app.domain.exception.GuiNotReadyError; +import notepack.gui.TaskUtil; + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class NewVersionCheck extends BaseTask implements Task, TypeRecurring, TypeGui { + + private HostServices hostServices; + + public NewVersionCheck(HostServices hostServices) { + this.hostServices = hostServices; + } + + @Override + public void backgroundWork() { + } + + @Override + public int getInterval() { + return 86400; + } + + @Override + public void guiWork(TaskUtil taskUtil, App app) throws GuiNotReadyError { + AnchorPane pane; + try { + + FXMLLoader loader = new FXMLLoader(getClass().getResource("/notepack/NewVersionPopup.fxml")); + pane = loader.load(); + + NewVersionController ctrl = loader.getController(); + ctrl.setTaskUtil(taskUtil); + ctrl.setHostServices(hostServices); + + if (ctrl.isNewVersionAvailable()) { + taskUtil.openPopup(pane); + } + + } catch (IOException ex) { + Logger.getLogger(MainViewController.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/src/main/resources/notepack/NewVersionPopup.fxml b/src/main/resources/notepack/NewVersionPopup.fxml new file mode 100644 index 0000000..746dc66 --- /dev/null +++ b/src/main/resources/notepack/NewVersionPopup.fxml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + +