From c18b0510bce75f599c7d1f715143b2d98c7a99f4 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Mon, 23 Aug 2021 00:30:07 +0800 Subject: [PATCH] feat: sponsor list --- .../org/jackhuang/hmcl/ui/main/AboutPage.java | 10 +- .../jackhuang/hmcl/ui/main/SponsorPage.java | 101 +++++++++++++++++- .../jackhuang/hmcl/util/io/HttpRequest.java | 5 + 3 files changed, 102 insertions(+), 14 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/AboutPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/AboutPage.java index 34ec25827..0f05b5fc8 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/AboutPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/AboutPage.java @@ -124,11 +124,6 @@ public class AboutPage extends StackPane { legal.getContent().setAll(copyright, claim, openSource); } - ComponentList donations = new ComponentList(); - { - // TODO - } - VBox content = new VBox(16); content.setPadding(new Insets(10)); content.getChildren().setAll( @@ -142,10 +137,7 @@ public class AboutPage extends StackPane { dep, ComponentList.createComponentListTitle(i18n("about.legal")), - legal, - - ComponentList.createComponentListTitle(i18n("about.donations")), - donations + legal ); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SponsorPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SponsorPage.java index ec8cadc6b..dd0c47825 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SponsorPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SponsorPage.java @@ -17,23 +17,39 @@ */ package org.jackhuang.hmcl.ui.main; +import com.google.gson.annotations.SerializedName; +import com.google.gson.reflect.TypeToken; +import com.jfoenix.controls.JFXListCell; +import com.jfoenix.controls.JFXListView; +import javafx.geometry.Insets; import javafx.geometry.VPos; import javafx.scene.Cursor; import javafx.scene.control.Label; -import javafx.scene.control.ScrollPane; import javafx.scene.layout.*; import javafx.scene.text.TextAlignment; +import org.jackhuang.hmcl.task.Schedulers; +import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.ui.FXUtils; +import org.jackhuang.hmcl.util.io.HttpRequest; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public class SponsorPage extends StackPane { + private final JFXListView listView; + public SponsorPage() { VBox content = new VBox(); + content.setPadding(new Insets(10)); + content.setSpacing(10); content.setFillWidth(true); { StackPane sponsorPane = new StackPane(); + sponsorPane.getStyleClass().add("card"); sponsorPane.setCursor(Cursor.HAND); sponsorPane.setOnMouseClicked(e -> onSponsor()); @@ -64,13 +80,88 @@ public class SponsorPage extends StackPane { content.getChildren().add(sponsorPane); } - ScrollPane scrollPane = new ScrollPane(); - scrollPane.setContent(content); - scrollPane.setFitToWidth(true); - getChildren().setAll(scrollPane); + { + StackPane pane = new StackPane(); + pane.getStyleClass().add("card"); + listView = new JFXListView<>(); + listView.setCellFactory((listView) -> new JFXListCell() { + @Override + public void updateItem(Sponsor item, boolean empty) { + super.updateItem(item, empty); + if (!empty) { + setText(item.getName()); + setGraphic(null); + } + } + }); + VBox.setVgrow(pane, Priority.ALWAYS); + pane.getChildren().setAll(listView); + content.getChildren().add(pane); + } + + loadSponsorList(); + + getChildren().setAll(content); } private void onSponsor() { FXUtils.openLink("https://hmcl.huangyuhui.net/api/redirect/sponsor"); } + + private void loadSponsorList() { + Task.>supplyAsync(() -> HttpRequest.GET("https://hmcl.huangyuhui.net/api/sponsor").getJson(new TypeToken>() { + }.getType()) + ).thenAcceptAsync(Schedulers.javafx(), sponsors -> { + listView.getItems().setAll(sponsors); + }).start(); + } + + private static class Sponsor { + @SerializedName("name") + private final String name; + + @SerializedName("create_time") + private final Date createTime; + + @SerializedName("money") + private final BigDecimal money; + + @SerializedName("contact") + private final String contact; + + @SerializedName("afdian_id") + private final String afdianId; + + public Sponsor() { + this("", new Date(), BigDecimal.ZERO, "", ""); + } + + public Sponsor(String name, Date createTime, BigDecimal money, String contact, String afdianId) { + this.name = name; + this.createTime = createTime; + this.money = money; + this.contact = contact; + this.afdianId = afdianId; + } + + public String getName() { + return name; + } + + public Date getCreateTime() { + return createTime; + } + + public BigDecimal getMoney() { + return money; + } + + public String getContact() { + return contact; + } + + public String getAfdianId() { + return afdianId; + } + } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java index 0049545aa..41670c862 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java @@ -24,6 +24,7 @@ import org.jackhuang.hmcl.util.gson.JsonUtils; import java.io.IOException; import java.io.OutputStream; +import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -70,6 +71,10 @@ public abstract class HttpRequest { return JsonUtils.fromNonNullJson(getString(), typeOfT); } + public T getJson(Type type) throws IOException, JsonParseException { + return JsonUtils.fromNonNullJson(getString(), type); + } + public HttpRequest filter(ExceptionalBiConsumer responseCodeTester) { this.responseCodeTester = responseCodeTester; return this;