Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve uploading experience with an Activity #2

Merged
merged 14 commits into from
Jan 5, 2024
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ labyMod {
author = "RappyTV"
description = "Upload your minecraft screenshots to your custom destinations."
minecraftVersion = "*"
version = System.getenv().getOrDefault("VERSION", "1.0.1")
version = System.getenv().getOrDefault("VERSION", "1.0.2")
}

minecraft {
Expand Down
112 changes: 112 additions & 0 deletions core/src/main/java/com/rappytv/uploader/activity/UploadActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.rappytv.uploader.activity;

import com.rappytv.uploader.api.ApiRequest;
import com.rappytv.uploader.api.Uploader;
import com.rappytv.uploader.api.Uploaders;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.format.NamedTextColor;
import net.labymod.api.client.gui.screen.Parent;
import net.labymod.api.client.gui.screen.activity.AutoActivity;
import net.labymod.api.client.gui.screen.activity.Link;
import net.labymod.api.client.gui.screen.activity.types.SimpleActivity;
import net.labymod.api.client.gui.screen.widget.Widget;
import net.labymod.api.client.gui.screen.widget.widgets.ComponentWidget;
import net.labymod.api.client.gui.screen.widget.widgets.input.ButtonWidget;
import net.labymod.api.client.gui.screen.widget.widgets.layout.FlexibleContentWidget;
import net.labymod.api.client.gui.screen.widget.widgets.layout.list.HorizontalListWidget;
import net.labymod.api.client.gui.screen.widget.widgets.layout.list.VerticalListWidget;
import net.labymod.api.client.gui.screen.widget.widgets.renderer.IconWidget;
import net.labymod.api.notification.Notification;
import java.io.File;

@Link("upload.lss")
@AutoActivity
public class UploadActivity extends SimpleActivity {

private final File file;

public UploadActivity(File file) {
this.file = file;
}

@Override
public void initialize(Parent parent) {
super.initialize(parent);

FlexibleContentWidget windowWidget = new FlexibleContentWidget().addId("window");
HorizontalListWidget headerWidget = new HorizontalListWidget().addId("header");
ComponentWidget titleWidget = ComponentWidget.i18n("uploader.activity.title").addId("title");
VerticalListWidget<Widget> content = new VerticalListWidget<>().addId("content");

headerWidget.addEntry(titleWidget);

for(Uploaders uploaderId : Uploaders.values()) {
Uploader uploader = uploaderId.getUploader();

HorizontalListWidget uploaderWidget = new HorizontalListWidget().addId("uploader");
IconWidget icon = new IconWidget(uploader.getIcon()).addId("icon");
ComponentWidget name = ComponentWidget.text(uploader.getName()).addId("name");
ButtonWidget button = new ButtonWidget().addId("button");
if(uploader.getAuth()[1].isBlank()) {
button.setEnabled(false);
button.updateComponent(Component.translatable("uploader.activity.noAuth", NamedTextColor.RED));
} else button.updateComponent(Component.translatable("uploader.activity.button"));

button.setActionListener(() -> {
button.setEnabled(false);
button.updateComponent(Component.translatable("uploader.activity.uploading", NamedTextColor.AQUA));
ApiRequest request = new ApiRequest(uploader, file);
request.sendAsyncRequest().thenAccept((result) -> {
if(request.isSuccessful()) {
button.setEnabled(true);
button.updateComponent(Component.translatable("uploader.activity.copy", NamedTextColor.GREEN));
Laby.labyAPI().notificationController().push(
Notification.builder()
.title(Component.translatable("uploader.toast.success"))
.text(Component.translatable("uploader.activity.uploaded", Component.text(uploader.getName())))
.build()
);
button.setActionListener(() -> {
Laby.labyAPI().notificationController().push(
Notification.builder()
.title(Component.translatable("uploader.toast.success"))
.text(Component.translatable("uploader.activity.copied"))
.build()
);
Laby.labyAPI().minecraft().chatExecutor().copyToClipboard(!request.getUploadLink().isBlank() ? request.getUploadLink() : "");
});
} else {
button.setEnabled(true);
button.updateComponent(Component.translatable("uploader.activity.error", NamedTextColor.RED));
Laby.labyAPI().notificationController().push(
Notification.builder()
.title(Component.translatable("uploader.toast.error"))
.text(Component.text(request.getError()))
.build()
);
}
}).exceptionally((e) -> {
button.setEnabled(true);
button.updateComponent(Component.translatable("uploader.activity.error", NamedTextColor.RED));
Laby.labyAPI().notificationController().push(
Notification.builder()
.title(Component.translatable("uploader.toast.error"))
.text(Component.text(e.getMessage()))
.build()
);
return null;
});
});

uploaderWidget.addEntry(icon);
uploaderWidget.addEntry(name);
uploaderWidget.addEntry(button);
content.addChild(uploaderWidget);
}

windowWidget.addContent(headerWidget);
windowWidget.addContent(content);
this.document.addChild(windowWidget);
}
}
7 changes: 4 additions & 3 deletions core/src/main/java/com/rappytv/uploader/api/ApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@ public CompletableFuture<Void> sendAsyncRequest() {
.uri(new URI(uploader.getUri()))
.header("Content-Type", data.getContentType())
.header(uploader.getAuth()[0], uploader.getAuth()[1])
.method(uploader.getMethod(), data.getBodyPublisher())
.method("POST", data.getBodyPublisher())
.build();

HttpClient client = HttpClient.newHttpClient();
client
.sendAsync(request, BodyHandlers.ofString())
.thenAccept((response) -> {
int status = uploader.getStatus(response);
successful = status >= 200 && status <= 299;
successful = response.statusCode() >= 200 && response.statusCode() <= 299;
uploadLink = uploader.resolveUrl(response);
if(!successful) error = uploader.getError(response);
future.complete(null);
})
.exceptionally((e) -> {
future.completeExceptionally(e);
successful = false;
error = e.getMessage();
return null;
});
} catch (Exception e) {
error = e.getMessage();
successful = false;
future.completeExceptionally(e);
}

Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/com/rappytv/uploader/api/Uploader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.rappytv.uploader.api;

import com.rappytv.uploader.UploaderAddon;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.client.resources.ResourceLocation;
import java.io.File;
import java.io.IOException;
import java.net.http.HttpResponse;
Expand All @@ -10,10 +12,13 @@
public abstract class Uploader {

private static final Map<String, Uploader> uploaders = new HashMap<>();
protected static final ResourceLocation icons = ResourceLocation.create("uploader", "themes/vanilla/textures/settings.png");
protected final UploaderAddon addon;
private final String name;

public Uploader(String name, UploaderAddon addon) {
this.addon = addon;
this.name = name;
uploaders.put(name.toLowerCase(), this);
}

Expand All @@ -25,10 +30,13 @@ public static Uploader get(String id) {
return uploaders.get(id.toLowerCase());
}

public abstract String getMethod();
public String getName() {
return name;
}

public abstract Icon getIcon();
public abstract String getUri();
public abstract String[] getAuth();
public abstract int getStatus(HttpResponse<String> response);
public abstract String getError(HttpResponse<String> response);
public abstract String resolveUrl(HttpResponse<String> response);
public MultipartData getMultipartData(File file) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import com.google.gson.JsonParser;
import com.rappytv.uploader.UploaderAddon;
import com.rappytv.uploader.api.Uploader;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.util.I18n;
import java.net.http.HttpResponse;

public class EShareUploader extends Uploader {

public EShareUploader(UploaderAddon addon) {
super("eshare", addon);
super("EShare", addon);
}

@Override
public String getMethod() {
return "POST";
public Icon getIcon() {
return Icon.sprite32(icons, 0, 1);
}

@Override
Expand All @@ -28,11 +29,6 @@ public String[] getAuth() {
return new String[]{"api-key", addon.configuration().eshare().auth()};
}

@Override
public int getStatus(HttpResponse<String> response) {
return response.statusCode();
}

@Override
public String getError(HttpResponse<String> response) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rappytv.uploader.UploaderAddon;
import com.rappytv.uploader.api.MultipartData;
import com.rappytv.uploader.api.Uploader;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.util.I18n;
import java.io.File;
import java.io.IOException;
Expand All @@ -13,12 +14,12 @@
public class ImgurUploader extends Uploader {

public ImgurUploader(UploaderAddon addon) {
super("imgur", addon);
super("Imgur", addon);
}

@Override
public String getMethod() {
return "POST";
public Icon getIcon() {
return Icon.sprite32(icons, 1, 1);
}

@Override
Expand All @@ -31,11 +32,6 @@ public String[] getAuth() {
return new String[]{"Authorization", "Client-ID (this is just a placeholder)"};
}

@Override
public int getStatus(HttpResponse<String> response) {
return response.statusCode();
}

@Override
public String getError(HttpResponse<String> response) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rappytv.uploader.UploaderAddon;
import com.rappytv.uploader.api.MultipartData;
import com.rappytv.uploader.api.Uploader;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.util.I18n;
import java.io.File;
import java.io.IOException;
Expand All @@ -13,12 +14,12 @@
public class XBackBoneUploader extends Uploader {

public XBackBoneUploader(UploaderAddon addon) {
super("xbackbone", addon);
super("XBackBone", addon);
}

@Override
public String getMethod() {
return "POST";
public Icon getIcon() {
return Icon.sprite32(icons, 2, 1);
}

@Override
Expand All @@ -31,11 +32,6 @@ public String[] getAuth() {
return new String[]{"token", addon.configuration().xbackbone().auth()};
}

@Override
public int getStatus(HttpResponse<String> response) {
return response.statusCode();
}

@Override
public String getError(HttpResponse<String> response) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import com.google.gson.JsonParser;
import com.rappytv.uploader.UploaderAddon;
import com.rappytv.uploader.api.Uploader;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.util.I18n;
import java.net.http.HttpResponse;

public class ZiplineUploader extends Uploader {

public ZiplineUploader(UploaderAddon addon) {
super("zipline", addon);
super("Zipline", addon);
}

@Override
public String getMethod() {
return "POST";
public Icon getIcon() {
return Icon.sprite32(icons, 3, 1);
}

@Override
Expand All @@ -28,18 +29,6 @@ public String[] getAuth() {
return new String[]{"Authorization", addon.configuration().zipline().auth()};
}

@Override
public int getStatus(HttpResponse<String> response) {
try {
JsonObject object = JsonParser.parseString(response.body()).getAsJsonObject();

return object.has("code") ? object.get("code").getAsInt() : response.statusCode();
} catch (Exception e) {
e.printStackTrace();
return response.statusCode();
}
}

@Override
public String getError(HttpResponse<String> response) {
try {
Expand Down
Loading