mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-04 16:45:44 +00:00
added ability to configure paginators to be used
added paginator for warnings command
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package dev.sheldan.abstracto.core.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class PaginatorConfiguration {
|
||||
private String headerText;
|
||||
private List<String> items;
|
||||
private Long timeoutSeconds;
|
||||
private Boolean showPageNumbers;
|
||||
private Boolean useNumberedItems;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
|
||||
import com.jagrosh.jdautilities.menu.Paginator;
|
||||
import dev.sheldan.abstracto.core.model.PaginatorConfiguration;
|
||||
import dev.sheldan.abstracto.templating.service.TemplateService;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class PaginatorServiceBean implements PaginatorService {
|
||||
|
||||
@Autowired
|
||||
private BotService botService;
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Autowired
|
||||
private Gson gson;
|
||||
|
||||
|
||||
@Override
|
||||
public Paginator createPaginatorFromTemplate(String templateKey, Object model, EventWaiter waiter) {
|
||||
String embedConfig = templateService.renderTemplate(templateKey + "_paginator", model);
|
||||
PaginatorConfiguration configuration = gson.fromJson(embedConfig, PaginatorConfiguration.class);
|
||||
botService.getInstance().addEventListener(waiter);
|
||||
List<String> items = configuration.getItems();
|
||||
int itemsPerPage = findAppropriateCountPerPage(items);
|
||||
|
||||
return new Paginator.Builder()
|
||||
.setItemsPerPage(itemsPerPage)
|
||||
.setText(configuration.getHeaderText())
|
||||
.showPageNumbers(ObjectUtils.defaultIfNull(configuration.getShowPageNumbers(), false))
|
||||
.setItems(configuration.getItems().toArray(new String[0]))
|
||||
.useNumberedItems(ObjectUtils.defaultIfNull(configuration.getUseNumberedItems(), false))
|
||||
.setEventWaiter(waiter)
|
||||
.setTimeout(ObjectUtils.defaultIfNull(configuration.getTimeoutSeconds(), 120L), TimeUnit.SECONDS)
|
||||
.setFinalAction(message -> {
|
||||
botService.getInstance().removeEventListener(waiter);
|
||||
message.delete().queue();
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
private int findAppropriateCountPerPage(List<String> items) {
|
||||
int currentMin = Integer.MAX_VALUE;
|
||||
// to be sure, because the paginator adds some characters here and there
|
||||
int carefulMax = MessageEmbed.TEXT_MAX_LENGTH - 50;
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
int count = 0;
|
||||
int length = 0;
|
||||
for (String innerItem : items) {
|
||||
length += innerItem.length();
|
||||
if (length > carefulMax) {
|
||||
currentMin = Math.min(currentMin, count);
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return currentMin;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user