Skip to content

Commit

Permalink
* Actually check for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDead committed Oct 2, 2021
1 parent a1ee1a9 commit 3616eb4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/codedead/opal/OpalApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,13 @@ public void start(final Stage primaryStage) {
logger.info("Creating the MainWindowController");

final MainWindowController mainWindowController = loader.getController();
mainWindowController.setControllers(
settingsController,
new UpdateController(properties.getProperty("updateApi", "https://codedead.com/Software/Opal/version.json"))
mainWindowController.setControllers(settingsController, new UpdateController(properties.getProperty("updateApi", "https://codedead.com/Software/Opal/version.json"), properties.getProperty("currentVersion", "1.0.0.0"))
);

final Scene scene = new Scene(root);

primaryStage.setTitle(translationBundle.getString("MainWindowTitle"));
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(SharedVariables.ICON_URL)));
primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream(SharedVariables.ICON_URL))));
primaryStage.setScene(scene);

logger.info("Showing the MainWindow");
Expand Down
74 changes: 71 additions & 3 deletions src/main/java/com/codedead/opal/controller/UpdateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@
public final class UpdateController {

private String updateUrl;
private String currentVersion;

private final Logger logger;
private final ObjectMapper objectMapper;

/**
* Initialize a new UpdateController
*
* @param updateUrl The URL that can be used to check for updates
* @param updateUrl The URL that can be used to check for updates
* @param currentVersion The current application version
*/
public UpdateController(final String updateUrl) {
public UpdateController(final String updateUrl, final String currentVersion) {
logger = LogManager.getLogger(UpdateController.class);
logger.info("Initializing new UpdateController object");

objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

setUpdateUrl(updateUrl);
setCurrentVersion(currentVersion);
}

/**
Expand All @@ -63,10 +66,52 @@ public Optional<PlatformUpdate> checkForUpdates(final String currentPlatform, fi

final List<PlatformUpdate> updates = getUpdates();

return updates
final Optional<PlatformUpdate> update = updates
.stream()
.filter(e -> e.getPlatformName().equalsIgnoreCase(currentPlatform) && e.isPortable() == isPortable)
.findFirst();

if (update.isPresent()) {
final PlatformUpdate platformUpdate = update.get();
if (versionCompare(currentVersion, platformUpdate.getMajorVersion() + "." + platformUpdate.getMinorVersion() + "." + platformUpdate.getBuildVersion() + "." + platformUpdate.getRevisionVersion()) < 0) {
return update;
}
return Optional.empty();
}
return Optional.empty();
}

/**
* Compare to strings to check which version is larger
*
* @param v1 The first {@link String} object that contains a version
* @param v2 The second {@link String} object that contains a version
* @return 1 if v1 is larger than v2, -1 if v2 is larger than v1 and 0 if both are equal
*/
private int versionCompare(String v1, String v2) {
int vnum1 = 0;
int vnum2 = 0;

for (int i = 0, j = 0; (i < v1.length() || j < v2.length()); ) {
while (i < v1.length() && v1.charAt(i) != '.') {
vnum1 = vnum1 * 10 + (v1.charAt(i) - '0');
i++;
}
while (j < v2.length() && v2.charAt(j) != '.') {
vnum2 = vnum2 * 10 + (v2.charAt(j) - '0');
j++;
}

if (vnum1 > vnum2)
return 1;
if (vnum2 > vnum1)
return -1;

vnum1 = vnum2 = 0;
i++;
j++;
}
return 0;
}

/**
Expand Down Expand Up @@ -144,4 +189,27 @@ public void setUpdateUrl(final String updateUrl) {

this.updateUrl = updateUrl;
}

/**
* Get the current version
*
* @return The current version
*/
public String getCurrentVersion() {
return currentVersion;
}

/**
* Set the current version
*
* @param currentVersion The current version
*/
public void setCurrentVersion(final String currentVersion) {
if (currentVersion == null)
throw new NullPointerException("currentVersion cannot be null!");
if (currentVersion.isEmpty())
throw new IllegalArgumentException("currentVersion cannot be empty!");

this.currentVersion = currentVersion;
}
}
1 change: 1 addition & 0 deletions src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ portable=false
loglevel=INFO
timerDelay=3600000
timerDelayType=2
currentVersion=1.0.0.0

0 comments on commit 3616eb4

Please sign in to comment.