From 3a50c32e500975a8cbbdcbc9a4cfae4f0300550e Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Tue, 4 Apr 2023 12:42:23 +0200 Subject: [PATCH] rework convert button logic --- .../HubToLocalConvertController.java | 31 +++++++++++++------ .../fxml/convertvault_hubtolocal_convert.fxml | 11 +++++-- src/main/resources/i18n/strings.properties | 3 +- .../HubToLocalConvertControllerTest.java | 7 +++-- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/convertvault/HubToLocalConvertController.java b/src/main/java/org/cryptomator/ui/convertvault/HubToLocalConvertController.java index 119e88e89..4e761c119 100644 --- a/src/main/java/org/cryptomator/ui/convertvault/HubToLocalConvertController.java +++ b/src/main/java/org/cryptomator/ui/convertvault/HubToLocalConvertController.java @@ -21,16 +21,20 @@ import org.slf4j.LoggerFactory; import javax.inject.Inject; import javafx.application.Platform; +import javafx.beans.binding.Bindings; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.ContentDisplay; import javafx.stage.Stage; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.ResourceBundle; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutorService; @@ -52,13 +56,15 @@ public class HubToLocalConvertController implements FxController { private final RecoveryKeyFactory recoveryKeyFactory; private final MasterkeyFileAccess masterkeyFileAccess; private final ExecutorService backgroundExecutorService; - private final BooleanProperty isConverting; + private final ResourceBundle resourceBundle; + private final BooleanProperty conversionStarted; @FXML NewPasswordController newPasswordController; + public Button convertBtn; @Inject - public HubToLocalConvertController(@ConvertVaultWindow Stage window, @FxmlScene(FxmlFile.CONVERTVAULT_HUBTOLOCAL_SUCCESS) Lazy successScene, FxApplicationWindows applicationWindows, @ConvertVaultWindow Vault vault, @ConvertVaultWindow StringProperty recoveryKey, RecoveryKeyFactory recoveryKeyFactory, MasterkeyFileAccess masterkeyFileAccess, ExecutorService backgroundExecutorService) { + public HubToLocalConvertController(@ConvertVaultWindow Stage window, @FxmlScene(FxmlFile.CONVERTVAULT_HUBTOLOCAL_SUCCESS) Lazy successScene, FxApplicationWindows applicationWindows, @ConvertVaultWindow Vault vault, @ConvertVaultWindow StringProperty recoveryKey, RecoveryKeyFactory recoveryKeyFactory, MasterkeyFileAccess masterkeyFileAccess, ExecutorService backgroundExecutorService, ResourceBundle resourceBundle) { this.window = window; this.successScene = successScene; this.applicationWindows = applicationWindows; @@ -67,11 +73,23 @@ public class HubToLocalConvertController implements FxController { this.recoveryKeyFactory = recoveryKeyFactory; this.masterkeyFileAccess = masterkeyFileAccess; this.backgroundExecutorService = backgroundExecutorService; - this.isConverting = new SimpleBooleanProperty(false); + this.resourceBundle = resourceBundle; + this.conversionStarted = new SimpleBooleanProperty(false); + } @FXML public void initialize() { + convertBtn.disableProperty().bind(Bindings.createBooleanBinding( // + () -> !newPasswordController.isGoodPassword() || conversionStarted.get(), // + newPasswordController.goodPasswordProperty(), // + conversionStarted)); + convertBtn.contentDisplayProperty().bind(Bindings.createObjectBinding( // + () -> conversionStarted.getValue() ? ContentDisplay.LEFT : ContentDisplay.TEXT_ONLY, // + conversionStarted)); + convertBtn.textProperty().bind(Bindings.createStringBinding( // + () -> resourceBundle.getString("convertVault.convert.convertBtn." + (conversionStarted.get() ? "processing" : "before")), // + conversionStarted)); } @FXML @@ -83,10 +101,9 @@ public class HubToLocalConvertController implements FxController { public void convert() { Preconditions.checkState(newPasswordController.isGoodPassword()); LOG.info("Converting hub vault {} to local", vault.getPath()); - CompletableFuture.runAsync(() -> isConverting.setValue(true), Platform::runLater) // + CompletableFuture.runAsync(() -> conversionStarted.setValue(true), Platform::runLater) // .thenRunAsync(this::convertInternal, backgroundExecutorService) //TODO: which executor is used? .whenCompleteAsync((result, exception) -> { - isConverting.setValue(false); if (exception == null) { //TODO: check, how the exceptions are wrapped LOG.info("Conversion of vault {} succeeded.", vault.getPath()); window.setScene(successScene.get()); @@ -138,8 +155,4 @@ public class HubToLocalConvertController implements FxController { /* Getter/Setter */ - public NewPasswordController getNewPasswordController() { - return newPasswordController; - } - } diff --git a/src/main/resources/fxml/convertvault_hubtolocal_convert.fxml b/src/main/resources/fxml/convertvault_hubtolocal_convert.fxml index 9cc5f0f80..eff7c2035 100644 --- a/src/main/resources/fxml/convertvault_hubtolocal_convert.fxml +++ b/src/main/resources/fxml/convertvault_hubtolocal_convert.fxml @@ -1,10 +1,11 @@ + + + - - diff --git a/src/main/resources/i18n/strings.properties b/src/main/resources/i18n/strings.properties index 05c2d6387..f7689855a 100644 --- a/src/main/resources/i18n/strings.properties +++ b/src/main/resources/i18n/strings.properties @@ -462,7 +462,8 @@ recoveryKey.recover.resetSuccess.description=You can unlock your vault with the # Convert Vault convertVault.title=Convert vault -convertVault.convert.convertBtn=Convert vault +convertVault.convert.convertBtn.before=Convert vault +convertVault.convert.convertBtn.processing=Converting… convertVault.success.message=Conversion successful convertVault.success.description=You can now unlock the vault with the chosen password without requiring authentication or internet access. diff --git a/src/test/java/org/cryptomator/ui/convertvault/HubToLocalConvertControllerTest.java b/src/test/java/org/cryptomator/ui/convertvault/HubToLocalConvertControllerTest.java index 69d6f30e7..3942ce401 100644 --- a/src/test/java/org/cryptomator/ui/convertvault/HubToLocalConvertControllerTest.java +++ b/src/test/java/org/cryptomator/ui/convertvault/HubToLocalConvertControllerTest.java @@ -26,6 +26,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.Optional; +import java.util.ResourceBundle; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutorService; @@ -41,6 +42,7 @@ public class HubToLocalConvertControllerTest { RecoveryKeyFactory recoveryKeyFactory; MasterkeyFileAccess masterkeyFileAccess; ExecutorService backgroundExecutorService; + ResourceBundle resourceBundle; BooleanProperty isConverting; FxApplicationWindows appWindows; Lazy successScene; @@ -57,11 +59,12 @@ public class HubToLocalConvertControllerTest { recoveryKeyFactory = Mockito.mock(RecoveryKeyFactory.class); masterkeyFileAccess = Mockito.mock(MasterkeyFileAccess.class); backgroundExecutorService = Mockito.mock(ExecutorService.class); + resourceBundle = Mockito.mock(ResourceBundle.class); isConverting = Mockito.mock(BooleanProperty.class); appWindows = Mockito.mock(FxApplicationWindows.class); successScene = Mockito.mock(Lazy.class); newPasswordController = Mockito.mock(NewPasswordController.class); - inTest = new HubToLocalConvertController(window, successScene, appWindows, vault, recoveryKey, recoveryKeyFactory, masterkeyFileAccess, backgroundExecutorService); + inTest = new HubToLocalConvertController(window, successScene, appWindows, vault, recoveryKey, recoveryKeyFactory, masterkeyFileAccess, backgroundExecutorService, resourceBundle); inTest.newPasswordController = newPasswordController; } @@ -136,7 +139,7 @@ public class HubToLocalConvertControllerTest { @Test public void testConvertInternalNotWrapsIAE() throws IOException { - Mockito.doThrow(new IllegalArgumentException("yudu")).when(recoveryKeyFactory).newMasterkeyFileWithPassphrase(any(),anyString(),any()); + Mockito.doThrow(new IllegalArgumentException("yudu")).when(recoveryKeyFactory).newMasterkeyFileWithPassphrase(any(), anyString(), any()); Assertions.assertThrows(IllegalArgumentException.class, inSpy::convertInternal);