feat: sponsor list

This commit is contained in:
huanghongxun 2021-08-23 00:30:07 +08:00
parent 7bf06684c4
commit c18b0510bc
3 changed files with 102 additions and 14 deletions

View File

@ -124,11 +124,6 @@ public class AboutPage extends StackPane {
legal.getContent().setAll(copyright, claim, openSource); legal.getContent().setAll(copyright, claim, openSource);
} }
ComponentList donations = new ComponentList();
{
// TODO
}
VBox content = new VBox(16); VBox content = new VBox(16);
content.setPadding(new Insets(10)); content.setPadding(new Insets(10));
content.getChildren().setAll( content.getChildren().setAll(
@ -142,10 +137,7 @@ public class AboutPage extends StackPane {
dep, dep,
ComponentList.createComponentListTitle(i18n("about.legal")), ComponentList.createComponentListTitle(i18n("about.legal")),
legal, legal
ComponentList.createComponentListTitle(i18n("about.donations")),
donations
); );

View File

@ -17,23 +17,39 @@
*/ */
package org.jackhuang.hmcl.ui.main; 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.geometry.VPos;
import javafx.scene.Cursor; import javafx.scene.Cursor;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment; 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.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; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public class SponsorPage extends StackPane { public class SponsorPage extends StackPane {
private final JFXListView<Sponsor> listView;
public SponsorPage() { public SponsorPage() {
VBox content = new VBox(); VBox content = new VBox();
content.setPadding(new Insets(10));
content.setSpacing(10);
content.setFillWidth(true); content.setFillWidth(true);
{ {
StackPane sponsorPane = new StackPane(); StackPane sponsorPane = new StackPane();
sponsorPane.getStyleClass().add("card");
sponsorPane.setCursor(Cursor.HAND); sponsorPane.setCursor(Cursor.HAND);
sponsorPane.setOnMouseClicked(e -> onSponsor()); sponsorPane.setOnMouseClicked(e -> onSponsor());
@ -64,13 +80,88 @@ public class SponsorPage extends StackPane {
content.getChildren().add(sponsorPane); content.getChildren().add(sponsorPane);
} }
ScrollPane scrollPane = new ScrollPane(); {
scrollPane.setContent(content); StackPane pane = new StackPane();
scrollPane.setFitToWidth(true); pane.getStyleClass().add("card");
getChildren().setAll(scrollPane); listView = new JFXListView<>();
listView.setCellFactory((listView) -> new JFXListCell<Sponsor>() {
@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() { private void onSponsor() {
FXUtils.openLink("https://hmcl.huangyuhui.net/api/redirect/sponsor"); FXUtils.openLink("https://hmcl.huangyuhui.net/api/redirect/sponsor");
} }
private void loadSponsorList() {
Task.<List<Sponsor>>supplyAsync(() -> HttpRequest.GET("https://hmcl.huangyuhui.net/api/sponsor").getJson(new TypeToken<List<Sponsor>>() {
}.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;
}
}
} }

View File

@ -24,6 +24,7 @@ import org.jackhuang.hmcl.util.gson.JsonUtils;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -70,6 +71,10 @@ public abstract class HttpRequest {
return JsonUtils.fromNonNullJson(getString(), typeOfT); return JsonUtils.fromNonNullJson(getString(), typeOfT);
} }
public <T> T getJson(Type type) throws IOException, JsonParseException {
return JsonUtils.fromNonNullJson(getString(), type);
}
public HttpRequest filter(ExceptionalBiConsumer<URL, Integer, IOException> responseCodeTester) { public HttpRequest filter(ExceptionalBiConsumer<URL, Integer, IOException> responseCodeTester) {
this.responseCodeTester = responseCodeTester; this.responseCodeTester = responseCodeTester;
return this; return this;