Jelajahi Sumber

Implemented manual update check button (references #272)

Sebastian Stenzel 6 tahun lalu
induk
melakukan
09aca188fe

+ 17 - 4
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;
+	}
 
 }

+ 1 - 7
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<String> updateCheckerService) {
-		return updateCheckerService.runningProperty();
-	}
-
 	@Provides
 	@Named("latestVersion")
 	@FxApplicationScoped

+ 30 - 5
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<ContentDisplay> 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<ContentDisplay> 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();
+	}
 }

+ 10 - 2
main/ui/src/main/resources/fxml/preferences_updates.fxml

@@ -1,10 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
 <?import javafx.geometry.Insets?>
+<?import javafx.scene.control.Button?>
 <?import javafx.scene.control.CheckBox?>
-<?import javafx.scene.layout.VBox?>
 <?import javafx.scene.control.ProgressIndicator?>
-<?import javafx.scene.control.Button?>
+<?import javafx.scene.layout.VBox?>
+<?import javafx.scene.text.Text?>
+<?import javafx.scene.text.TextFlow?>
 <VBox xmlns="http://javafx.com/javafx"
 	  xmlns:fx="http://javafx.com/fxml"
 	  fx:controller="org.cryptomator.ui.preferences.UpdatesPreferencesController"
@@ -20,5 +22,11 @@
 				<ProgressIndicator progress="-1" prefWidth="12" prefHeight="12"/>
 			</graphic>
 		</Button>
+		
+		<TextFlow styleClass="text-flow" visible="${controller.updateAvailable}" textAlignment="CENTER">
+			<Text text="TODO Update to version "/>
+			<Text text="${controller.latestVersion}"/>
+			<Text text=" available."/>
+		</TextFlow>
 	</children>
 </VBox>