diff --git a/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java b/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java index 53a02130e..8be37b528 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java +++ b/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java @@ -1,8 +1,10 @@ package org.cryptomator.ui.fxapp; +import javafx.beans.binding.BooleanBinding; import javafx.beans.property.ReadOnlyStringProperty; import javafx.beans.property.StringProperty; import javafx.concurrent.ScheduledService; +import javafx.concurrent.Worker; import javafx.concurrent.WorkerStateEvent; import javafx.util.Duration; import org.cryptomator.common.settings.Settings; @@ -36,13 +38,14 @@ public class UpdateChecker { public void startCheckingForUpdates(Duration initialDelay) { updateCheckerService.setDelay(initialDelay); + updateCheckerService.setOnRunning(this::checkStarted); updateCheckerService.setOnSucceeded(this::checkSucceeded); updateCheckerService.setOnFailed(this::checkFailed); updateCheckerService.restart(); } - public ReadOnlyStringProperty latestVersionProperty() { - return latestVersionProperty; + private void checkStarted(WorkerStateEvent event) { + LOG.debug("Checking for updates..."); } private void checkSucceeded(WorkerStateEvent event) { @@ -51,8 +54,8 @@ public class UpdateChecker { LOG.info("Current version: {}, lastest version: {}", currentVersion, latestVersion); // TODO settings.lastVersionCheck = Instant.now() - if (currentVersion != null && semVerComparator.compare(currentVersion, latestVersion) < 0) { - // update is available! + if (currentVersion == null || semVerComparator.compare(currentVersion, latestVersion) < 0) { + // update is available latestVersionProperty.set(latestVersion); } else { latestVersionProperty.set(null); @@ -62,5 +65,15 @@ public class UpdateChecker { private void checkFailed(WorkerStateEvent event) { LOG.warn("Error checking for updates", event.getSource().getException()); } + + /* Observable Properties */ + + public BooleanBinding checkingForUpdatesProperty() { + return updateCheckerService.stateProperty().isEqualTo(Worker.State.RUNNING); + } + + public ReadOnlyStringProperty latestVersionProperty() { + return latestVersionProperty; + } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateCheckerModule.java b/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateCheckerModule.java index 0a735865f..8b99dacd1 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateCheckerModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/fxapp/UpdateCheckerModule.java @@ -10,6 +10,7 @@ import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.concurrent.ScheduledService; import javafx.concurrent.Task; +import javafx.concurrent.Worker; import javafx.util.Duration; import org.apache.commons.lang3.SystemUtils; @@ -26,13 +27,6 @@ public abstract class UpdateCheckerModule { private static final URI LATEST_VERSION_URI = URI.create("https://api.cryptomator.org/updates/latestVersion.json"); private static final Duration UPDATE_CHECK_INTERVAL = Duration.hours(3); - @Provides - @Named("checkingForUpdates") - @FxApplicationScoped - static ReadOnlyBooleanProperty provideCheckingForUpdates(ScheduledService updateCheckerService) { - return updateCheckerService.runningProperty(); - } - @Provides @Named("latestVersion") @FxApplicationScoped diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java index 4b1b2295b..8c1b2601d 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/UpdatesPreferencesController.java @@ -1,28 +1,36 @@ package org.cryptomator.ui.preferences; import javafx.beans.binding.Bindings; +import javafx.beans.binding.BooleanBinding; import javafx.beans.binding.ObjectBinding; -import javafx.beans.property.ReadOnlyBooleanProperty; +import javafx.beans.property.ReadOnlyStringProperty; import javafx.fxml.FXML; import javafx.scene.control.CheckBox; import javafx.scene.control.ContentDisplay; +import javafx.util.Duration; import org.cryptomator.common.settings.Settings; import org.cryptomator.ui.common.FxController; +import org.cryptomator.ui.fxapp.UpdateChecker; import javax.inject.Inject; -import javax.inject.Named; @PreferencesScoped public class UpdatesPreferencesController implements FxController { private final Settings settings; + private final UpdateChecker updateChecker; private final ObjectBinding checkForUpdatesButtonState; + private final ReadOnlyStringProperty latestVersion; + private final BooleanBinding updateAvailable; public CheckBox checkForUpdatesCheckbox; @Inject - UpdatesPreferencesController(Settings settings, @Named("checkingForUpdates") ReadOnlyBooleanProperty checkingForUpdates) { + UpdatesPreferencesController(Settings settings, UpdateChecker updateChecker) { this.settings = settings; - this.checkForUpdatesButtonState = Bindings.when(checkingForUpdates).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY); + this.updateChecker = updateChecker; + this.checkForUpdatesButtonState = Bindings.when(updateChecker.checkingForUpdatesProperty()).then(ContentDisplay.LEFT).otherwise(ContentDisplay.TEXT_ONLY); + this.latestVersion = updateChecker.latestVersionProperty(); + this.updateAvailable = latestVersion.isNotNull(); } public void initialize() { @@ -31,9 +39,10 @@ public class UpdatesPreferencesController implements FxController { @FXML public void checkNow() { + updateChecker.startCheckingForUpdates(Duration.ZERO); } - /* Getter/Setter */ + /* Observable Properties */ public ObjectBinding checkForUpdatesButtonStateProperty() { return checkForUpdatesButtonState; @@ -42,4 +51,20 @@ public class UpdatesPreferencesController implements FxController { public ContentDisplay getCheckForUpdatesButtonState() { return checkForUpdatesButtonState.get(); } + + public ReadOnlyStringProperty latestVersionProperty() { + return latestVersion; + } + + public String getLatestVersion() { + return latestVersion.get(); + } + + public BooleanBinding updateAvailableProperty() { + return updateAvailable; + } + + public boolean isUpdateAvailable() { + return updateAvailable.get(); + } } diff --git a/main/ui/src/main/resources/fxml/preferences_updates.fxml b/main/ui/src/main/resources/fxml/preferences_updates.fxml index a95565d2c..e1050de66 100644 --- a/main/ui/src/main/resources/fxml/preferences_updates.fxml +++ b/main/ui/src/main/resources/fxml/preferences_updates.fxml @@ -1,10 +1,12 @@ - - - + + + + + + + + + + +