update news tab testing

This commit is contained in:
J62 2025-03-14 13:50:32 -07:00
parent 7a1d0ae197
commit 9a5a469ad0
1 changed files with 48 additions and 27 deletions

View File

@ -12,11 +12,14 @@ import ctbrec.ui.tabs.TabSelectionListener;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
@ -27,16 +30,14 @@ import static ctbrec.io.HttpConstants.USER_AGENT;
@Slf4j
public class NewsTab extends Tab implements TabSelectionListener {
private static final String ACCESS_TOKEN = "a2804d73a89951a22e0f8483a6fcec8943afd88b7ba17c459c095aa9e6f94fd0";
private static final String URL = "https://mastodon.cloud/api/v1/accounts/480960/statuses?limit=20&exclude_replies=true";
private static final String URL = "https://git.ctbrec.com/api/v1/repos/j62/ctbrec/releases";
private final Config config;
private final VBox layout = new VBox();
private final ObjectMapper mapper = ObjectMapperFactory.getMapper();
public NewsTab(Config config) {
this.config = config;
setText("News");
setText("Releases");
layout.setMaxWidth(800);
layout.setAlignment(Pos.CENTER);
setContent(new ScrollPane(layout));
@ -44,14 +45,13 @@ public class NewsTab extends Tab implements TabSelectionListener {
@Override
public void selected() {
GlobalThreadPool.submit(this::loadToots);
GlobalThreadPool.submit(this::loadReleases);
}
private void loadToots() {
private void loadReleases() {
try {
var request = new Request.Builder()
.url(URL)
.header("Authorization", "Bearer " + ACCESS_TOKEN)
.header(USER_AGENT, "ctbrec " + Version.getVersion())
.build();
try (var response = CamrecApplication.httpClient.execute(request)) {
@ -60,8 +60,6 @@ public class NewsTab extends Tab implements TabSelectionListener {
log.debug(body);
if (body.startsWith("[")) {
onSuccess(body);
} else if (body.startsWith("{")) {
onError(body);
} else {
throw new IOException("Unexpected response: " + body);
}
@ -70,30 +68,53 @@ public class NewsTab extends Tab implements TabSelectionListener {
}
}
} catch (IOException e) {
log.info("Error while loading news", e);
Dialogs.showError(getTabPane().getScene(), "News", "Couldn't load news from mastodon", e);
}
}
private void onError(String body) throws IOException {
var json = new JSONObject(body);
if (json.has("error")) {
throw new IOException("Request not successful: " + json.getString("error"));
} else {
throw new IOException("Unexpected response: " + body);
log.info("Error while loading releases", e);
Dialogs.showError(getTabPane().getScene(), "Releases", "Couldn't load release information", e);
}
}
private void onSuccess(String body) throws IOException {
Status[] statusArray = mapper.readValue(body, Status[].class);
JSONArray releases = new JSONArray(body);
Platform.runLater(() -> {
layout.getChildren().clear();
for (Status status : statusArray) {
if (status.getInReplyToId() == null && !Objects.equals("direct", status.getVisibility())) {
var stp = new StatusPane(status, config.getDateTimeFormatter());
layout.getChildren().add(stp);
VBox.setMargin(stp, new Insets(10));
for (int i = 0; i < releases.length(); i++) {
JSONObject release = releases.getJSONObject(i);
String tagName = release.optString("tag_name", "Unknown Version");
String releaseName = release.optString("name", "No Name");
String description = release.optString("body", "No description available.");
JSONArray assets = release.optJSONArray("assets");
var releasePane = new VBox();
releasePane.setPadding(new Insets(10));
releasePane.setSpacing(5);
Label versionLabel = new Label("Version: " + tagName);
versionLabel.setStyle("-fx-font-weight: bold;");
Label nameLabel = new Label("Release: " + releaseName);
Label descLabel = new Label(description);
releasePane.getChildren().addAll(versionLabel, nameLabel, descLabel);
if (assets != null) {
Label assetsLabel = new Label("Assets:");
releasePane.getChildren().add(assetsLabel);
for (int j = 0; j < assets.length(); j++) {
JSONObject asset = assets.getJSONObject(j);
String assetName = asset.optString("name", "Unknown File");
String downloadUrl = asset.optString("browser_download_url", "#");
int size = asset.optInt("size", 0);
int downloads = asset.optInt("download_count", 0);
Hyperlink assetLink = new Hyperlink(assetName + " (" + size / 1024 / 1024 + " MB, " + downloads + " downloads)");
assetLink.setOnAction(event -> CamrecApplication.getHostServicesInstance().showDocument(downloadUrl));
releasePane.getChildren().add(assetLink);
}
}
layout.getChildren().add(releasePane);
VBox.setMargin(releasePane, new Insets(10));
}
});
}
@ -102,4 +123,4 @@ public class NewsTab extends Tab implements TabSelectionListener {
public void deselected() {
// nothing to do
}
}
}