package ctbrec.ui; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.function.Function; import ctbrec.HttpClient; import ctbrec.Model; import ctbrec.recorder.download.StreamSource; import javafx.concurrent.Task; import javafx.scene.control.ChoiceDialog; public class StreamSourceSelectionDialog { public static void show(Model model, HttpClient client, Function onSuccess, Function onFail) { Task> selectStreamSource = new Task>() { @Override protected List call() throws Exception { return model.getStreamSources(); } }; selectStreamSource.setOnSucceeded((e) -> { List sources; try { sources = selectStreamSource.get(); ChoiceDialog choiceDialog = new ChoiceDialog(sources.get(sources.size()-1), sources); choiceDialog.setTitle("Stream Quality"); choiceDialog.setHeaderText("Select your preferred stream quality"); choiceDialog.setResizable(true); Optional selectedSource = choiceDialog.showAndWait(); if(selectedSource.isPresent()) { int index = sources.indexOf(selectedSource.get()); model.setStreamUrlIndex(index); onSuccess.apply(model); } } catch (InterruptedException | ExecutionException e1) { onFail.apply(e1); } }); selectStreamSource.setOnFailed((e) -> { onFail.apply(selectStreamSource.getException()); }); new Thread(selectStreamSource).start(); } }