-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check new version every 24h and show popup if new version appear
- Loading branch information
Ogefest
committed
Feb 20, 2021
1 parent
199c13b
commit 2aa586d
Showing
6 changed files
with
219 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |