Made YouTube video upload asynchronous from the GUI

This commit is contained in:
CrushedPixel
2016-09-03 21:24:14 +02:00
committed by johni0702
parent 39834ce6b4
commit 2a73faed6c
2 changed files with 20 additions and 9 deletions

View File

@@ -184,11 +184,14 @@ public class GuiYoutubeUpload extends GuiScreen {
uploadButton.setEnabled();
if (uploading) {
uploadButton.onClick(() -> {
try {
upload.cancel();
} catch (InterruptedException e) {
e.printStackTrace();
}
setState(false);
new Thread(() -> {
try {
upload.cancel();
} catch(InterruptedException e) {
e.printStackTrace();
}
}).start();
}).setI18nLabel("replaymod.gui.cancel");
} else {
uploadButton.onClick(() -> {
@@ -219,8 +222,9 @@ public class GuiYoutubeUpload extends GuiScreen {
@Override
public void onFailure(Throwable t) {
if (t instanceof InterruptedException) {
progressBar.setLabel("0%");
if (t instanceof InterruptedException || upload.isCancelled()) {
progressBar.setProgress(0);
progressBar.setLabel("%d%%");
} else {
t.printStackTrace();
progressBar.setLabel(t.getLocalizedMessage());

View File

@@ -61,6 +61,9 @@ public class YoutubeUploader {
@Getter
private State state;
@Getter
private volatile boolean cancelled;
public YoutubeUploader(Minecraft minecraft, File videoFile, int videoFrames,
String thumbnailFormat, byte[] thumbnailImage,
RenderSettings settings, VideoVisibility videoVisibility, VideoSnippet videoSnippet)
@@ -78,6 +81,8 @@ public class YoutubeUploader {
}
public ListenableFuture<Video> upload() throws IOException {
cancelled = false;
final SettableFuture<Video> future = SettableFuture.create();
thread = new Thread(() -> {
try {
@@ -108,9 +113,11 @@ public class YoutubeUploader {
return future;
}
//I blame the Google SDK for not supporting "proper" upload cancellation
@SuppressWarnings("unchecked")
public void cancel() throws InterruptedException {
thread.interrupt();
thread.join();
thread.stop();
cancelled = true;
}
private Credential auth() throws IOException {