Skip to content

Commit

Permalink
check new version every 24h and show popup if new version appear
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogefest committed Feb 20, 2021
1 parent 199c13b commit 2aa586d
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 3 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/notepack/MainViewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void windowRestore() {

app.getTheme().set(cssFile, mainScene);
app.addTask(new InitializeShortcuts());
app.addTask(new NewVersionCheck(hostServices));
app.startDispatcher();
}

Expand Down
114 changes: 114 additions & 0 deletions src/main/java/notepack/NewVersionController.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
15 changes: 14 additions & 1 deletion src/main/java/notepack/app/domain/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/notepack/app/task/NewVersionCheck.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
33 changes: 33 additions & 0 deletions src/main/resources/notepack/NewVersionPopup.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<?import java.net.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="212.0"
prefWidth="517.0" styleClass="card" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="notepack.NewVersionController">
<stylesheets>
<!--<URL value="@color-definition.css" />-->
<URL value="@scrollbar.css"/>
<URL value="@icons.css"/>
<URL value="@default.css"/>
<URL value="@layout.css"/>
<URL value="@popup.css"/>
</stylesheets>
<children>
<Label layoutX="14.0" layoutY="14.0" styleClass="popup-title" text="New version">
<font>
<Font size="62.0"/>
</font>
</Label>
<Button fx:id="btnCancel" cancelButton="true" layoutX="370.0" layoutY="163.0" mnemonicParsing="false"
onAction="#onCancel" text="Ignore" AnchorPane.bottomAnchor="14.0"/>
<Label fx:id="versionLabel" contentDisplay="CENTER" layoutX="14.0" layoutY="71.0" prefHeight="62.0"
prefWidth="574.0" text="New version was released, click open to open github page and download"
textAlignment="CENTER" wrapText="true" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0"/>
<Button fx:id="btnOpen" defaultButton="true" layoutX="444.0" layoutY="163.0" mnemonicParsing="false"
onAction="#onOpen" text="Open" AnchorPane.bottomAnchor="14.0"/>
</children>
</AnchorPane>

0 comments on commit 2aa586d

Please sign in to comment.