Reset Selected Resolution if model offline (credit @Gabi_uy)

This commit is contained in:
Jafea7 2025-09-10 21:28:20 +10:00
parent 180bfe72c2
commit 0f8ff720b7
1 changed files with 65 additions and 23 deletions

View File

@ -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<StreamSource> 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<StreamSource> 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() {