From 45a385be118011864a665203d8dc44a1cf44d8b3 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Fri, 10 Sep 2021 13:00:29 +0200 Subject: [PATCH] Move switching stream resolution to a dedicated action --- .../action/SwitchStreamResolutionAction.java | 83 +++++++++++++++++++ .../ctbrec/ui/menu/ModelMenuContributor.java | 54 +----------- 2 files changed, 85 insertions(+), 52 deletions(-) create mode 100644 client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java diff --git a/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java b/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java new file mode 100644 index 00000000..c8a438c6 --- /dev/null +++ b/client/src/main/java/ctbrec/ui/action/SwitchStreamResolutionAction.java @@ -0,0 +1,83 @@ +package ctbrec.ui.action; + +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import org.eclipse.jetty.io.RuntimeIOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ctbrec.GlobalThreadPool; +import ctbrec.Model; +import ctbrec.recorder.Recorder; +import ctbrec.recorder.download.StreamSource; +import ctbrec.sites.ModelOfflineException; +import ctbrec.ui.StreamSourceSelectionDialog; +import ctbrec.ui.controls.Dialogs; +import javafx.application.Platform; +import javafx.scene.Cursor; +import javafx.scene.Node; + +public class SwitchStreamResolutionAction { + private static final Logger LOG = LoggerFactory.getLogger(SwitchStreamResolutionAction.class); + + private Node source; + private Model selectedModel; + private Recorder recorder; + + public SwitchStreamResolutionAction(Node source, Model selectedModel, Recorder recorder) { + this.source = source; + this.selectedModel = selectedModel; + this.recorder = recorder; + } + + public CompletableFuture execute() { + source.setCursor(Cursor.WAIT); + var couldntSwitchHeaderText = "Couldn't switch stream resolution"; + + return CompletableFuture.supplyAsync(() -> { + checkOnlineState(); + return selectedModel; + }, 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); + } + } + } + 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; + }); + } + + private void checkOnlineState() { + try { + if (!selectedModel.isOnline(true)) { + throw new ModelOfflineException(selectedModel); + } + } catch (IOException | ExecutionException e) { + throw new RuntimeIOException(e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); // NOSONAR + } + } +} diff --git a/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java b/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java index 580f2916..be030554 100644 --- a/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java +++ b/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java @@ -1,11 +1,7 @@ package ctbrec.ui.menu; -import java.io.IOException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Optional; -import java.util.concurrent.ExecutionException; import java.util.function.Consumer; import org.slf4j.Logger; @@ -15,10 +11,7 @@ import ctbrec.Config; import ctbrec.Model; import ctbrec.ModelGroup; import ctbrec.recorder.Recorder; -import ctbrec.recorder.download.StreamSource; -import ctbrec.ui.AutosizeAlert; import ctbrec.ui.DesktopIntegration; -import ctbrec.ui.StreamSourceSelectionDialog; import ctbrec.ui.action.AbstractModelAction.Result; import ctbrec.ui.action.AddToGroupAction; import ctbrec.ui.action.EditNotesAction; @@ -31,14 +24,13 @@ import ctbrec.ui.action.SetPortraitAction; import ctbrec.ui.action.SetStopDateAction; import ctbrec.ui.action.StartRecordingAction; import ctbrec.ui.action.StopRecordingAction; +import ctbrec.ui.action.SwitchStreamResolutionAction; import ctbrec.ui.action.TipAction; import ctbrec.ui.action.TriConsumer; -import ctbrec.ui.controls.Dialogs; import ctbrec.ui.tabs.FollowedTab; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Node; -import javafx.scene.control.Alert; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; import javafx.scene.control.SeparatorMenuItem; @@ -239,49 +231,7 @@ public class ModelMenuContributor { } private void switchStreamSource(Model selectedModel) { - var couldntSwitchHeaderText = "Couldn't switch stream resolution"; - - try { - if (!selectedModel.isOnline()) { - Dialogs.showError(source.getScene(), couldntSwitchHeaderText, "The resolution can only be changed when the model is online", null); - return; - } - } catch (InterruptedException e1) { - Thread.currentThread().interrupt(); - Dialogs.showError(source.getScene(), couldntSwitchHeaderText, "An error occured while checking, if the model is online", null); - return; - } catch (IOException | ExecutionException e1) { - Dialogs.showError(source.getScene(), couldntSwitchHeaderText, "An error occured while checking, if the model is online", null); - return; - } - - 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); - showStreamSwitchErrorDialog(e); - } - } - } - } - - private void showStreamSwitchErrorDialog(Throwable throwable) { - showErrorDialog(throwable, "Couldn't switch stream resolution", "Error while switching stream resolution"); - } - - private void showErrorDialog(Throwable throwable, String header, String msg) { - Alert alert = new AutosizeAlert(Alert.AlertType.ERROR, source.getScene()); - alert.setTitle("Error"); - alert.setHeaderText(header); - alert.setContentText(msg + ": " + throwable.getLocalizedMessage()); - alert.showAndWait(); + new SwitchStreamResolutionAction(source, selectedModel, recorder).execute(); } private void addGroupMenu(ContextMenu menu, List selectedModels) {