From f312d5ed582b65aa6b687794d0bef9608d103987 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Mon, 30 Dec 2019 19:34:58 +0100 Subject: [PATCH] Add possibility to switch back to best stream quality --- .../ui/StreamSourceSelectionDialog.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/StreamSourceSelectionDialog.java b/client/src/main/java/ctbrec/ui/StreamSourceSelectionDialog.java index c4b07242..2aa64e8f 100644 --- a/client/src/main/java/ctbrec/ui/StreamSourceSelectionDialog.java +++ b/client/src/main/java/ctbrec/ui/StreamSourceSelectionDialog.java @@ -17,21 +17,26 @@ import javafx.scene.image.Image; import javafx.stage.Stage; public class StreamSourceSelectionDialog { + private static final StreamSource BEST = new BestStreamSource(); + + private StreamSourceSelectionDialog() {} + public static void show(Scene parent, Model model, Function onSuccess, Function onFail) { Task> selectStreamSource = new Task>() { @Override protected List call() throws Exception { List sources = model.getStreamSources(); Collections.sort(sources); + sources.add(BEST); return sources; } }; - selectStreamSource.setOnSucceeded((e) -> { + selectStreamSource.setOnSucceeded(e -> { List sources; try { sources = selectStreamSource.get(); int selectedIndex = model.getStreamUrlIndex() > -1 ? Math.min(model.getStreamUrlIndex(), sources.size()-1) : sources.size()-1; - ChoiceDialog choiceDialog = new ChoiceDialog(sources.get(selectedIndex), sources); + ChoiceDialog choiceDialog = new ChoiceDialog<>(sources.get(selectedIndex), sources); choiceDialog.setTitle("Stream Quality"); choiceDialog.setHeaderText("Select your preferred stream quality"); choiceDialog.setResizable(true); @@ -41,17 +46,28 @@ public class StreamSourceSelectionDialog { stage.getIcons().add(new Image(icon)); Optional selectedSource = choiceDialog.showAndWait(); if(selectedSource.isPresent()) { - int index = sources.indexOf(selectedSource.get()); + int index = -1; + if (selectedSource.get() != BEST) { + index = sources.indexOf(selectedSource.get()); + } model.setStreamUrlIndex(index); onSuccess.apply(model); } - } catch (InterruptedException | ExecutionException e1) { + } catch (ExecutionException e1) { + onFail.apply(e1); + } catch (InterruptedException e1) { + Thread.currentThread().interrupt(); onFail.apply(e1); } }); - selectStreamSource.setOnFailed((e) -> { - onFail.apply(selectStreamSource.getException()); - }); + selectStreamSource.setOnFailed(e -> onFail.apply(selectStreamSource.getException())); new Thread(selectStreamSource).start(); } + + private static class BestStreamSource extends StreamSource { + @Override + public String toString() { + return "Best"; + } + } }