Fix exception caused by loading the changelog too early

This commit is contained in:
0xb00bface 2021-01-03 17:53:13 +01:00
parent 08f481e6c9
commit c462aefd4f
1 changed files with 23 additions and 7 deletions

View File

@ -1,5 +1,10 @@
package ctbrec.ui.tabs;
import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.io.HttpException;
import ctbrec.ui.CamrecApplication;
import ctbrec.ui.CamrecApplication.Release;
@ -15,10 +20,8 @@ import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UpdateTab extends Tab {
public class UpdateTab extends Tab implements TabSelectionListener {
private static final Logger LOG = LoggerFactory.getLogger(UpdateTab.class);
@ -37,14 +40,17 @@ public class UpdateTab extends Tab {
vbox.setAlignment(Pos.CENTER);
changelog = new TextArea();
changelog.setEditable(false);
changelog.setText("Loading changelog...");
vbox.getChildren().add(changelog);
VBox.setVgrow(changelog, Priority.ALWAYS);
setContent(vbox);
}
new Thread(() -> {
public void loadChangeLog() {
CompletableFuture.runAsync(() -> {
Request req = new Request.Builder().url("https://pastebin.com/raw/fiAPtM0s").build();
try(Response resp = CamrecApplication.httpClient.execute(req)) {
if(resp.isSuccessful()) {
try (Response resp = CamrecApplication.httpClient.execute(req)) {
if (resp.isSuccessful()) {
changelog.setText(resp.body().string());
} else {
throw new HttpException(resp.code(), resp.message());
@ -53,6 +59,16 @@ public class UpdateTab extends Tab {
LOG.error("Couldn't download the changelog", e1);
Dialogs.showError(getTabPane().getScene(), "Communication error", "Couldn't download the changelog", e1);
}
}).start();
});
}
@Override
public void selected() {
loadChangeLog();
}
@Override
public void deselected() {
// nothing to do
}
}