Move switching stream resolution to a dedicated action

This commit is contained in:
0xb00bface 2021-09-10 13:00:29 +02:00
parent a3ee92f269
commit 45a385be11
2 changed files with 85 additions and 52 deletions

View File

@ -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<Void> 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<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);
}
}
}
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
}
}
}

View File

@ -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<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);
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<Model> selectedModels) {