get rid of registerException

use separate views, no need to pass state between views
This commit is contained in:
Sebastian Stenzel
2024-01-11 11:23:54 +01:00
parent a6d6474294
commit a902400522
6 changed files with 73 additions and 35 deletions

View File

@@ -22,6 +22,7 @@ public enum FxmlFile {
HUB_RECEIVE_KEY("/fxml/hub_receive_key.fxml"), //
HUB_LEGACY_REGISTER_DEVICE("/fxml/hub_legacy_register_device.fxml"), //
HUB_REGISTER_SUCCESS("/fxml/hub_register_success.fxml"), //
HUB_REGISTER_DEVICE_ALREADY_EXISTS("/fxml/hub_register_device_already_exists.fxml"), //
HUB_REGISTER_FAILED("/fxml/hub_register_failed.fxml"), //
HUB_REGISTER_DEVICE("/fxml/hub_register_device.fxml"), //
HUB_UNAUTHORIZED_DEVICE("/fxml/hub_unauthorized_device.fxml"), //

View File

@@ -72,14 +72,6 @@ public abstract class HubKeyLoadingModule {
return new CompletableFuture<>();
}
@Provides
@KeyLoadingScoped
@Named("registerException")
static AtomicReference<Throwable> provideRegisterException() {
return new AtomicReference<>();
}
@Binds
@IntoMap
@KeyLoadingScoped
@@ -149,6 +141,13 @@ public abstract class HubKeyLoadingModule {
return fxmlLoaders.createScene(FxmlFile.HUB_REGISTER_DEVICE);
}
@Provides
@FxmlScene(FxmlFile.HUB_REGISTER_DEVICE_ALREADY_EXISTS)
@KeyLoadingScoped
static Scene provideHubRegisterDeviceAlreadyExistsScene(@KeyLoading FxmlLoaderFactory fxmlLoaders) {
return fxmlLoaders.createScene(FxmlFile.HUB_REGISTER_DEVICE_ALREADY_EXISTS);
}
@Provides
@FxmlScene(FxmlFile.HUB_UNAUTHORIZED_DEVICE)
@KeyLoadingScoped

View File

@@ -55,9 +55,9 @@ public class RegisterDeviceController implements FxController {
private final Stage window;
private final HubConfig hubConfig;
private final String bearerToken;
private final AtomicReference<Throwable> registerException;
private final Lazy<Scene> registerSuccessScene;
private final Lazy<Scene> registerFailedScene;
private final Lazy<Scene> registerDeviceAlreadyExistsScene;
private final String deviceId;
private final P384KeyPair deviceKeyPair;
private final CompletableFuture<ReceivedKey> result;
@@ -70,16 +70,16 @@ public class RegisterDeviceController implements FxController {
public Button registerBtn;
@Inject
public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture<ReceivedKey> result, @Named("bearerToken") AtomicReference<String> bearerToken, @Named("registerException") AtomicReference<Throwable> registerException, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy<Scene> registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy<Scene> registerFailedScene) {
public RegisterDeviceController(@KeyLoading Stage window, ExecutorService executor, HubConfig hubConfig, @Named("deviceId") String deviceId, DeviceKey deviceKey, CompletableFuture<ReceivedKey> result, @Named("bearerToken") AtomicReference<String> bearerToken, @FxmlScene(FxmlFile.HUB_REGISTER_SUCCESS) Lazy<Scene> registerSuccessScene, @FxmlScene(FxmlFile.HUB_REGISTER_FAILED) Lazy<Scene> registerFailedScene, @FxmlScene(FxmlFile.HUB_REGISTER_DEVICE_ALREADY_EXISTS) Lazy<Scene> registerDeviceAlreadyExistsScene) {
this.window = window;
this.hubConfig = hubConfig;
this.deviceId = deviceId;
this.deviceKeyPair = Objects.requireNonNull(deviceKey.get());
this.result = result;
this.bearerToken = Objects.requireNonNull(bearerToken.get());
this.registerException = registerException;
this.registerSuccessScene = registerSuccessScene;
this.registerFailedScene = registerFailedScene;
this.registerDeviceAlreadyExistsScene = registerDeviceAlreadyExistsScene;
this.window.addEventHandler(WindowEvent.WINDOW_HIDING, this::windowClosed);
this.httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).executor(executor).build();
}
@@ -184,13 +184,12 @@ public class RegisterDeviceController implements FxController {
private void setupFailed(Throwable cause) {
switch (cause) {
case CompletionException e when e.getCause() instanceof JWEHelper.InvalidJweKeyException -> invalidSetupCode.set(true);
case DeviceAlreadyExistsException e -> {
LOG.debug("Device already registered in hub instance {} for different user", hubConfig.authSuccessUrl);
window.setScene(registerDeviceAlreadyExistsScene.get());
}
default -> {
if (cause instanceof DeviceAlreadyExistsException) {
LOG.debug("Device already registered in hub instance {} for different user", hubConfig.authSuccessUrl);
} else {
LOG.warn("Device setup failed.", cause);
}
registerException.set(cause);
LOG.warn("Device setup failed.", cause);
window.setScene(registerFailedScene.get());
}
}

View File

@@ -4,36 +4,25 @@ import org.cryptomator.ui.common.FxController;
import org.cryptomator.ui.keyloading.KeyLoading;
import javax.inject.Inject;
import javax.inject.Named;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.stage.Stage;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.CompletableFuture;
public class RegisterFailedController implements FxController {
private final Stage window;
private final Throwable registerException;
private final SimpleBooleanProperty deviceAlreadyExisting;
private final CompletableFuture<ReceivedKey> result;
@Inject
public RegisterFailedController(@KeyLoading Stage window, @Named("registerException") AtomicReference<Throwable> registerExceptionRef) {
public RegisterFailedController(@KeyLoading Stage window, CompletableFuture<ReceivedKey> result) {
this.window = window;
this.registerException = registerExceptionRef.get();
this.deviceAlreadyExisting = new SimpleBooleanProperty(registerException instanceof DeviceAlreadyExistsException);
this.result = result;
}
@FXML
public void close() {
result.cancel(true);
window.close();
}
public boolean isDeviceAlreadyExisting() {
return deviceAlreadyExisting.get();
}
public boolean isGenericError() {
return !deviceAlreadyExisting.get();
}
}