From 263eb811f40b90abbe6aa039ec883aabbd61e86f Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 24 Oct 2024 13:46:34 +0000 Subject: [PATCH] More rework --- .../impl/ConnectionRefreshExchangeImpl.java | 2 +- .../app/comp/store/StoreCreationComp.java | 26 +++++------- .../app/comp/store/StoreCreationMenu.java | 2 +- .../xpipe/app/comp/store/StoreIntroComp.java | 2 +- .../java/io/xpipe/app/ext/ShellStore.java | 2 +- .../io/xpipe/app/issue/ErrorHandlerComp.java | 3 ++ .../io/xpipe/app/storage/DataStoreEntry.java | 35 ++-------------- .../java/io/xpipe/app/util/ScanAlert.java | 14 +++---- .../java/io/xpipe/app/util/ScanDialog.java | 41 +++---------------- .../io/xpipe/core/store/ValidatableStore.java | 2 +- .../ext/base/action/ScanStoreAction.java | 2 +- 11 files changed, 34 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/beacon/impl/ConnectionRefreshExchangeImpl.java b/app/src/main/java/io/xpipe/app/beacon/impl/ConnectionRefreshExchangeImpl.java index 1b33eee84..5fa336528 100644 --- a/app/src/main/java/io/xpipe/app/beacon/impl/ConnectionRefreshExchangeImpl.java +++ b/app/src/main/java/io/xpipe/app/beacon/impl/ConnectionRefreshExchangeImpl.java @@ -17,7 +17,7 @@ public class ConnectionRefreshExchangeImpl extends ConnectionRefreshExchange { if (e.getStore() instanceof FixedHierarchyStore) { DataStorage.get().refreshChildren(e, true); } else { - e.validateOrThrowAndClose(null); + e.validateOrThrow(); } return Response.builder().build(); } diff --git a/app/src/main/java/io/xpipe/app/comp/store/StoreCreationComp.java b/app/src/main/java/io/xpipe/app/comp/store/StoreCreationComp.java index ea116f73f..fb2262177 100644 --- a/app/src/main/java/io/xpipe/app/comp/store/StoreCreationComp.java +++ b/app/src/main/java/io/xpipe/app/comp/store/StoreCreationComp.java @@ -174,11 +174,8 @@ public class StoreCreationComp extends DialogComp { e.getProvider(), e.getStore(), v -> true, - (newE, context, validated) -> { + (newE, validated) -> { ThreadHelper.runAsync(() -> { - if (context != null) { - context.close(); - } if (!DataStorage.get().getStoreEntries().contains(e)) { DataStorage.get().addStoreEntryIfNotPresent(newE); } else { @@ -205,16 +202,15 @@ public class StoreCreationComp extends DialogComp { base != null ? DataStoreProviders.byStore(base) : null, base, dataStoreProvider -> category.equals(dataStoreProvider.getCreationCategory()), - (e, context, validated) -> { + (e, validated) -> { try { DataStorage.get().addStoreEntryIfNotPresent(e); - if (context != null - && validated + if (validated && e.getProvider().shouldShowScan() && AppPrefs.get() .openConnectionSearchWindowOnConnectionCreation() .get()) { - ScanAlert.showAsync(e, context); + ScanAlert.showAsync(e); } } catch (Exception ex) { ErrorEvent.fromThrowable(ex).handle(); @@ -226,7 +222,7 @@ public class StoreCreationComp extends DialogComp { public interface CreationConsumer { - void consume(DataStoreEntry entry, ValidationContext validationContext, boolean validated); + void consume(DataStoreEntry entry, boolean validated); } private static void show( @@ -265,7 +261,7 @@ public class StoreCreationComp extends DialogComp { return List.of( new ButtonComp(AppI18n.observable("skipValidation"), null, () -> { if (showInvalidConfirmAlert()) { - commit(null, false); + commit(false); } else { finish(); } @@ -308,7 +304,7 @@ public class StoreCreationComp extends DialogComp { // We didn't change anything if (existingEntry != null && existingEntry.getStore().equals(store.getValue())) { - commit(null, false); + commit(false); return; } @@ -338,8 +334,8 @@ public class StoreCreationComp extends DialogComp { try (var ignored = new BooleanScope(busy).start()) { DataStorage.get().addStoreEntryInProgress(entry.getValue()); - var context = entry.getValue().validateAndKeepOpenOrThrowAndClose(null); - commit(context, true); + entry.getValue().validateOrThrow(); + commit(true); } catch (Throwable ex) { if (ex instanceof ValidationException) { ErrorEvent.expected(ex); @@ -420,14 +416,14 @@ public class StoreCreationComp extends DialogComp { .createRegion(); } - private void commit(ValidationContext validationContext, boolean validated) { + private void commit(boolean validated) { if (finished.get()) { return; } finished.setValue(true); if (entry.getValue() != null) { - consumer.consume(entry.getValue(), validationContext, validated); + consumer.consume(entry.getValue(), validated); } PlatformThread.runLaterIfNeeded(() -> { diff --git a/app/src/main/java/io/xpipe/app/comp/store/StoreCreationMenu.java b/app/src/main/java/io/xpipe/app/comp/store/StoreCreationMenu.java index df546265f..bf546f036 100644 --- a/app/src/main/java/io/xpipe/app/comp/store/StoreCreationMenu.java +++ b/app/src/main/java/io/xpipe/app/comp/store/StoreCreationMenu.java @@ -22,7 +22,7 @@ public class StoreCreationMenu { automatically.setGraphic(new FontIcon("mdi2e-eye-plus-outline")); automatically.textProperty().bind(AppI18n.observable("addAutomatically")); automatically.setOnAction(event -> { - ScanAlert.showAsync(null, null); + ScanAlert.showAsync(null); event.consume(); }); menu.getItems().add(automatically); diff --git a/app/src/main/java/io/xpipe/app/comp/store/StoreIntroComp.java b/app/src/main/java/io/xpipe/app/comp/store/StoreIntroComp.java index c427fca82..6ae9626f3 100644 --- a/app/src/main/java/io/xpipe/app/comp/store/StoreIntroComp.java +++ b/app/src/main/java/io/xpipe/app/comp/store/StoreIntroComp.java @@ -39,7 +39,7 @@ public class StoreIntroComp extends SimpleComp { var scanButton = new Button(null, new FontIcon("mdi2m-magnify")); scanButton.textProperty().bind(AppI18n.observable("detectConnections")); - scanButton.setOnAction(event -> ScanAlert.showAsync(DataStorage.get().local(), null)); + scanButton.setOnAction(event -> ScanAlert.showAsync(DataStorage.get().local())); scanButton.setDefaultButton(true); var scanPane = new StackPane(scanButton); scanPane.setAlignment(Pos.CENTER); diff --git a/app/src/main/java/io/xpipe/app/ext/ShellStore.java b/app/src/main/java/io/xpipe/app/ext/ShellStore.java index 0412e8c08..d6c6a0412 100644 --- a/app/src/main/java/io/xpipe/app/ext/ShellStore.java +++ b/app/src/main/java/io/xpipe/app/ext/ShellStore.java @@ -46,7 +46,7 @@ public interface ShellStore extends DataStore, FileSystemStore, ValidatableStore ShellControlFunction shellFunction(); @Override - default ShellValidationContext validate(ShellValidationContext context) throws Exception { + default ShellValidationContext validate() throws Exception { var func = shellFunction(); var c = func instanceof ShellControlParentStoreFunction s ? s.control(s.getParentStore().getOrStartSession()) : func instanceof ShellControlParentFunction p ? p.control(p.parentControl()) : func.control(); diff --git a/app/src/main/java/io/xpipe/app/issue/ErrorHandlerComp.java b/app/src/main/java/io/xpipe/app/issue/ErrorHandlerComp.java index 944319ade..544302e71 100644 --- a/app/src/main/java/io/xpipe/app/issue/ErrorHandlerComp.java +++ b/app/src/main/java/io/xpipe/app/issue/ErrorHandlerComp.java @@ -138,6 +138,9 @@ public class ErrorHandlerComp extends SimpleComp { } catch (Throwable t) { t.printStackTrace(); } + } else { + showLatch.countDown(); + finishLatch.countDown(); } }); diff --git a/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java b/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java index b20ad891a..0de900046 100644 --- a/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java +++ b/app/src/main/java/io/xpipe/app/storage/DataStoreEntry.java @@ -516,47 +516,18 @@ public class DataStoreEntry extends StorageElement { } public void validateOrThrow() throws Throwable { - validateOrThrowAndClose(null); - } - - public boolean validateOrThrowAndClose(ValidationContext existingContext) throws Throwable { - var subContext = validateAndKeepOpenOrThrowAndClose(existingContext); - if (subContext != null) { - subContext.close(); - return true; - } else { - return false; - } - } - - @SuppressWarnings("unchecked") - public ValidationContext validateAndKeepOpenOrThrowAndClose(ValidationContext existingContext) - throws Throwable { if (store == null) { - return null; + return; } if (!(store instanceof ValidatableStore l)) { - return null; + return; } try { store.checkComplete(); incrementBusyCounter(); - ValidationContext context = existingContext != null - ? (ValidationContext) existingContext - : (ValidationContext) l.createContext(); - if (context == null) { - return null; - } - - try { - var r = ((ValidatableStore>) l).validate(context); - return r; - } catch (Throwable t) { - context.close(); - throw t; - } + l.validate(); } finally { decrementBusyCounter(); } diff --git a/app/src/main/java/io/xpipe/app/util/ScanAlert.java b/app/src/main/java/io/xpipe/app/util/ScanAlert.java index f74ef1ee7..b6850403e 100644 --- a/app/src/main/java/io/xpipe/app/util/ScanAlert.java +++ b/app/src/main/java/io/xpipe/app/util/ScanAlert.java @@ -17,7 +17,7 @@ import java.util.function.BiFunction; public class ScanAlert { - public static void showAsync(DataStoreEntry entry, ValidationContext context) { + public static void showAsync(DataStoreEntry entry) { ThreadHelper.runAsync(() -> { var showForCon = entry == null || (entry.getStore() instanceof ShellStore @@ -25,12 +25,12 @@ public class ScanAlert { || shellStoreState.getTtyState() == null || shellStoreState.getTtyState() == ShellTtyState.NONE)); if (showForCon) { - showForShellStore(entry, (ShellValidationContext) context); + showForShellStore(entry); } }); } - public static void showForShellStore(DataStoreEntry initial, ShellValidationContext context) { + public static void showForShellStore(DataStoreEntry initial) { show( initial, (DataStoreEntry entry, ShellControl sc) -> { @@ -61,17 +61,15 @@ public class ScanAlert { } } return applicable; - }, - context); + }); } private static void show( DataStoreEntry initialStore, - BiFunction> applicable, - ShellValidationContext shellValidationContext) { + BiFunction> applicable) { DialogComp.showWindow( "scanAlertTitle", stage -> new ScanDialog( - stage, initialStore != null ? initialStore.ref() : null, applicable, shellValidationContext)); + stage, initialStore != null ? initialStore.ref() : null, applicable)); } } diff --git a/app/src/main/java/io/xpipe/app/util/ScanDialog.java b/app/src/main/java/io/xpipe/app/util/ScanDialog.java index baee6834b..f4dae7ada 100644 --- a/app/src/main/java/io/xpipe/app/util/ScanDialog.java +++ b/app/src/main/java/io/xpipe/app/util/ScanDialog.java @@ -40,18 +40,15 @@ class ScanDialog extends DialogComp { private final ListProperty selected = new SimpleListProperty<>(FXCollections.observableArrayList()); private final BooleanProperty busy = new SimpleBooleanProperty(); - private ShellValidationContext shellValidationContext; ScanDialog( Stage window, DataStoreEntryRef entry, - BiFunction> applicable, - ShellValidationContext shellValidationContext) { + BiFunction> applicable) { this.window = window; this.initialStore = entry; this.entry = new SimpleObjectProperty<>(entry); this.applicable = applicable; - this.shellValidationContext = shellValidationContext; } @Override @@ -62,7 +59,6 @@ class ScanDialog extends DialogComp { @Override protected void finish() { ThreadHelper.runFailableAsync(() -> { - try { if (entry.get() == null) { return; } @@ -84,7 +80,7 @@ class ScanDialog extends DialogComp { } // Previous scan operation could have exited the shell - shellValidationContext.get().start(); + initialStore.getStore().getOrStartSession(); try { a.getScanner().run(); @@ -93,24 +89,11 @@ class ScanDialog extends DialogComp { } } }); - } finally { - if (shellValidationContext != null) { - shellValidationContext.close(); - shellValidationContext = null; - } - } }); } @Override - protected void discard() { - ThreadHelper.runAsync(() -> { - if (shellValidationContext != null) { - shellValidationContext.close(); - shellValidationContext = null; - } - }); - } + protected void discard() {} @Override protected Comp pane(Comp content) { @@ -161,22 +144,8 @@ class ScanDialog extends DialogComp { ThreadHelper.runFailableAsync(() -> { BooleanScope.executeExclusive(busy, () -> { - if (shellValidationContext != null) { - shellValidationContext.close(); - shellValidationContext = null; - } - - shellValidationContext = new ShellValidationContext( - newValue.getStore().getOrStartSession().withoutLicenseCheck().start()); - - // Handle window close while connection is established - if (!window.isShowing()) { - discard(); - return; - } - - var a = applicable.apply(entry.get().get(), shellValidationContext.get()); - + var sc = initialStore.getStore().getOrStartSession().withoutLicenseCheck(); + var a = applicable.apply(entry.get().get(), sc); Platform.runLater(() -> { if (a == null) { window.close(); diff --git a/core/src/main/java/io/xpipe/core/store/ValidatableStore.java b/core/src/main/java/io/xpipe/core/store/ValidatableStore.java index 3fcde2c13..6cc5a6621 100644 --- a/core/src/main/java/io/xpipe/core/store/ValidatableStore.java +++ b/core/src/main/java/io/xpipe/core/store/ValidatableStore.java @@ -18,7 +18,7 @@ public interface ValidatableStore> extends DataSt * * @throws Exception if any part of the validation went wrong */ - T validate(T context) throws Exception; + T validate() throws Exception; T createContext() throws Exception; } diff --git a/ext/base/src/main/java/io/xpipe/ext/base/action/ScanStoreAction.java b/ext/base/src/main/java/io/xpipe/ext/base/action/ScanStoreAction.java index 9e344f0ca..f9b179f6b 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/action/ScanStoreAction.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/action/ScanStoreAction.java @@ -70,7 +70,7 @@ public class ScanStoreAction implements ActionProvider { @Override public void execute() { if (entry == null || entry.getStore() instanceof ShellStore) { - ScanAlert.showForShellStore(entry, null); + ScanAlert.showForShellStore(entry); } } }