Add possibility to switch back to best stream quality

This commit is contained in:
0xboobface 2019-12-30 19:34:58 +01:00
parent 643650d69e
commit f312d5ed58
1 changed files with 23 additions and 7 deletions

View File

@ -17,21 +17,26 @@ import javafx.scene.image.Image;
import javafx.stage.Stage; import javafx.stage.Stage;
public class StreamSourceSelectionDialog { public class StreamSourceSelectionDialog {
private static final StreamSource BEST = new BestStreamSource();
private StreamSourceSelectionDialog() {}
public static void show(Scene parent, Model model, Function<Model,Void> onSuccess, Function<Throwable, Void> onFail) { public static void show(Scene parent, Model model, Function<Model,Void> onSuccess, Function<Throwable, Void> onFail) {
Task<List<StreamSource>> selectStreamSource = new Task<List<StreamSource>>() { Task<List<StreamSource>> selectStreamSource = new Task<List<StreamSource>>() {
@Override @Override
protected List<StreamSource> call() throws Exception { protected List<StreamSource> call() throws Exception {
List<StreamSource> sources = model.getStreamSources(); List<StreamSource> sources = model.getStreamSources();
Collections.sort(sources); Collections.sort(sources);
sources.add(BEST);
return sources; return sources;
} }
}; };
selectStreamSource.setOnSucceeded((e) -> { selectStreamSource.setOnSucceeded(e -> {
List<StreamSource> sources; List<StreamSource> sources;
try { try {
sources = selectStreamSource.get(); sources = selectStreamSource.get();
int selectedIndex = model.getStreamUrlIndex() > -1 ? Math.min(model.getStreamUrlIndex(), sources.size()-1) : sources.size()-1; int selectedIndex = model.getStreamUrlIndex() > -1 ? Math.min(model.getStreamUrlIndex(), sources.size()-1) : sources.size()-1;
ChoiceDialog<StreamSource> choiceDialog = new ChoiceDialog<StreamSource>(sources.get(selectedIndex), sources); ChoiceDialog<StreamSource> choiceDialog = new ChoiceDialog<>(sources.get(selectedIndex), sources);
choiceDialog.setTitle("Stream Quality"); choiceDialog.setTitle("Stream Quality");
choiceDialog.setHeaderText("Select your preferred stream quality"); choiceDialog.setHeaderText("Select your preferred stream quality");
choiceDialog.setResizable(true); choiceDialog.setResizable(true);
@ -41,17 +46,28 @@ public class StreamSourceSelectionDialog {
stage.getIcons().add(new Image(icon)); stage.getIcons().add(new Image(icon));
Optional<StreamSource> selectedSource = choiceDialog.showAndWait(); Optional<StreamSource> selectedSource = choiceDialog.showAndWait();
if(selectedSource.isPresent()) { if(selectedSource.isPresent()) {
int index = sources.indexOf(selectedSource.get()); int index = -1;
if (selectedSource.get() != BEST) {
index = sources.indexOf(selectedSource.get());
}
model.setStreamUrlIndex(index); model.setStreamUrlIndex(index);
onSuccess.apply(model); onSuccess.apply(model);
} }
} catch (InterruptedException | ExecutionException e1) { } catch (ExecutionException e1) {
onFail.apply(e1);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
onFail.apply(e1); onFail.apply(e1);
} }
}); });
selectStreamSource.setOnFailed((e) -> { selectStreamSource.setOnFailed(e -> onFail.apply(selectStreamSource.getException()));
onFail.apply(selectStreamSource.getException());
});
new Thread(selectStreamSource).start(); new Thread(selectStreamSource).start();
} }
private static class BestStreamSource extends StreamSource {
@Override
public String toString() {
return "Best";
}
}
} }