forked from j62/ctbrec
1
0
Fork 0
ctbrec/src/main/java/ctbrec/ui/StreamSourceSelectionDialog...

46 lines
1.8 KiB
Java

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<Model,Void> onSuccess, Function<Throwable, Void> onFail) {
Task<List<StreamSource>> selectStreamSource = new Task<List<StreamSource>>() {
@Override
protected List<StreamSource> call() throws Exception {
return model.getStreamSources();
}
};
selectStreamSource.setOnSucceeded((e) -> {
List<StreamSource> sources;
try {
sources = selectStreamSource.get();
ChoiceDialog<StreamSource> choiceDialog = new ChoiceDialog<StreamSource>(sources.get(sources.size()-1), sources);
choiceDialog.setTitle("Stream Quality");
choiceDialog.setHeaderText("Select your preferred stream quality");
choiceDialog.setResizable(true);
Optional<StreamSource> 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();
}
}