diff --git a/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java b/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java index 7603f81c..00d5ceb7 100644 --- a/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java +++ b/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java @@ -38,32 +38,74 @@ public class SwitchStreamResolutionAction { var couldntSwitchHeaderText = "Couldn't switch stream resolution"; return CompletableFuture.supplyAsync(() -> { - checkOnlineState(); - return selectedModel; + try { + checkOnlineState(); + return Boolean.TRUE; // model is online + } catch (ModelOfflineException e) { + return Boolean.FALSE; // model is offline + } }, GlobalThreadPool.get()) - .thenAccept(m -> Platform.runLater(() -> { - StreamSourceSelectionDialog dialog = new StreamSourceSelectionDialog(source.getScene(), selectedModel); - Optional selectedSource = dialog.showAndWait(); - if (selectedSource.isPresent()) { - StreamSource src = selectedSource.get(); - if (src != StreamSourceSelectionDialog.LOADING) { - int index = dialog.indexOf(selectedSource.get()); - selectedModel.setStreamUrlIndex(index); - try { - recorder.switchStreamSource(selectedModel); - } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | IOException e) { - log.error(couldntSwitchHeaderText, e); - Dialogs.showError(source.getScene(), "Couldn't switch stream resolution", "Error while switching stream resolution", e); - } + .thenAccept(isOnline -> Platform.runLater(() -> { + if (isOnline) { + // --- model is online: open selection dialog --- + StreamSourceSelectionDialog dialog = + new StreamSourceSelectionDialog(source.getScene(), selectedModel); + Optional selectedSource = dialog.showAndWait(); + if (selectedSource.isPresent()) { + StreamSource src = selectedSource.get(); + if (src != StreamSourceSelectionDialog.LOADING) { + int index = dialog.indexOf(src); + selectedModel.setStreamUrlIndex(index); + try { + recorder.switchStreamSource(selectedModel); + } catch (InvalidKeyException | NoSuchAlgorithmException | + IllegalStateException | IOException e) { + log.error(couldntSwitchHeaderText, e); + Dialogs.showError(source.getScene(), couldntSwitchHeaderText, + "Error while switching stream resolution", e); } } - source.setCursor(Cursor.DEFAULT); - })) - .exceptionally(ex -> { - Dialogs.showError(source.getScene(), couldntSwitchHeaderText, "The resolution can only be changed when the model is online", null); - Platform.runLater(() -> source.setCursor(Cursor.DEFAULT)); - return null; - }); + } + } else { + // --- model is offline: ask if user wants to reset --- + boolean confirmed = Dialogs.showConfirmDialog( + "Model is offline", + "Yes to reset to Default (Best resolution),\nNo to leave existing resolution unchanged.", + "", + source.getScene() + ); + if (confirmed) { + selectedModel.setStreamUrlIndex(-1); + try { + recorder.switchStreamSource(selectedModel); // persist the change + } catch (InvalidKeyException | NoSuchAlgorithmException | + IllegalStateException | IOException e) { + log.error("Couldn't update recorder with reset stream index", e); + Dialogs.showError(source.getScene(), + "Couldn't reset stream resolution", + "Error while updating stream resolution", e); + } + + // show confirmation popup + Dialogs.showError( + source.getScene(), + "Stream Resolution Reset", + "Stream resolution has been reset to Best (default).", + null + ); + } + } + source.setCursor(Cursor.DEFAULT); + })) + .exceptionally(ex -> { + log.error("Unexpected error while switching resolution", ex); + Platform.runLater(() -> { + Dialogs.showError(source.getScene(), couldntSwitchHeaderText, + "Unexpected error occurred", ex); + source.setCursor(Cursor.DEFAULT); + }); + return null; + }); } private void checkOnlineState() {