plugin hub panel: sort plugins by usercount

This commit is contained in:
Adam
2021-01-21 18:12:37 -05:00
parent e4e7d228d0
commit 321b976cf1
2 changed files with 59 additions and 10 deletions

View File

@@ -25,9 +25,11 @@
package net.runelite.client.externalplugins; package net.runelite.client.externalplugins;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.gson.JsonSyntaxException;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@@ -37,6 +39,7 @@ import java.security.cert.Certificate;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.inject.Inject; import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -172,4 +175,27 @@ public class ExternalPluginClient
} }
}); });
} }
public Map<String, Integer> getPluginCounts() throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase()
.newBuilder()
.addPathSegments("pluginhub")
.build();
try (Response res = okHttpClient.newCall(new Request.Builder().url(url).build()).execute())
{
if (res.code() != 200)
{
throw new IOException("Non-OK response code: " + res.code());
}
// CHECKSTYLE:OFF
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(res.body().byteStream()), new TypeToken<Map<String, Integer>>(){}.getType());
// CHECKSTYLE:ON
}
catch (JsonSyntaxException ex)
{
throw new IOException(ex);
}
}
} }

View File

@@ -125,10 +125,13 @@ class PluginHubPanel extends PluginPanel
@Getter @Getter
private final List<String> keywords = new ArrayList<>(); private final List<String> keywords = new ArrayList<>();
@Getter
private final int userCount;
@Getter @Getter
private final boolean installed; private final boolean installed;
PluginItem(ExternalPluginManifest newManifest, Collection<Plugin> loadedPlugins, boolean installed) PluginItem(ExternalPluginManifest newManifest, Collection<Plugin> loadedPlugins, int userCount, boolean installed)
{ {
ExternalPluginManifest loaded = null; ExternalPluginManifest loaded = null;
if (!loadedPlugins.isEmpty()) if (!loadedPlugins.isEmpty())
@@ -137,6 +140,7 @@ class PluginHubPanel extends PluginPanel
} }
manifest = newManifest == null ? loaded : newManifest; manifest = newManifest == null ? loaded : newManifest;
this.userCount = userCount;
this.installed = installed; this.installed = installed;
if (manifest != null) if (manifest != null)
@@ -194,10 +198,7 @@ class PluginHubPanel extends PluginPanel
{ {
BufferedImage img = externalPluginClient.downloadIcon(manifest); BufferedImage img = externalPluginClient.downloadIcon(manifest);
SwingUtilities.invokeLater(() -> SwingUtilities.invokeLater(() -> icon.setIcon(new ImageIcon(img)));
{
icon.setIcon(new ImageIcon(img));
});
} }
catch (IOException e) catch (IOException e)
{ {
@@ -521,11 +522,21 @@ class PluginHubPanel extends PluginPanel
return; return;
} }
reloadPluginList(manifest); Map<String, Integer> pluginCounts = Collections.emptyMap();
try
{
pluginCounts = externalPluginClient.getPluginCounts();
}
catch (IOException e)
{
log.warn("unable to download plugin counts", e);
}
reloadPluginList(manifest, pluginCounts);
}); });
} }
private void reloadPluginList(List<ExternalPluginManifest> manifest) private void reloadPluginList(List<ExternalPluginManifest> manifest, Map<String, Integer> pluginCounts)
{ {
Map<String, ExternalPluginManifest> manifests = manifest.stream() Map<String, ExternalPluginManifest> manifests = manifest.stream()
.collect(ImmutableMap.toImmutableMap(ExternalPluginManifest::getInternalName, Function.identity())); .collect(ImmutableMap.toImmutableMap(ExternalPluginManifest::getInternalName, Function.identity()));
@@ -547,7 +558,8 @@ class PluginHubPanel extends PluginPanel
{ {
plugins = Sets.union(manifests.keySet(), loadedPlugins.keySet()) plugins = Sets.union(manifests.keySet(), loadedPlugins.keySet())
.stream() .stream()
.map(id -> new PluginItem(manifests.get(id), loadedPlugins.get(id), installed.contains(id))) .map(id -> new PluginItem(manifests.get(id), loadedPlugins.get(id),
pluginCounts.getOrDefault(id, -1), installed.contains(id)))
.collect(Collectors.toList()); .collect(Collectors.toList());
refreshing.setVisible(false); refreshing.setVisible(false);
@@ -575,7 +587,11 @@ class PluginHubPanel extends PluginPanel
else else
{ {
stream stream
.sorted(Comparator.comparing(PluginItem::isInstalled).thenComparing(p -> p.manifest.getDisplayName())) .sorted(Comparator.comparing(PluginItem::isInstalled)
.thenComparingInt(PluginItem::getUserCount)
.reversed()
.thenComparing(p -> p.manifest.getDisplayName())
)
.forEach(mainPanel::add); .forEach(mainPanel::add);
} }
@@ -594,6 +610,13 @@ class PluginHubPanel extends PluginPanel
@Subscribe @Subscribe
private void onExternalPluginsChanged(ExternalPluginsChanged ev) private void onExternalPluginsChanged(ExternalPluginsChanged ev)
{ {
SwingUtilities.invokeLater(() -> reloadPluginList(ev.getLoadedManifest())); Map<String, Integer> pluginCounts = Collections.emptyMap();
if (plugins != null)
{
pluginCounts = plugins.stream()
.collect(Collectors.toMap(pi -> pi.manifest.getInternalName(), PluginItem::getUserCount));
}
reloadPluginList(ev.getLoadedManifest(), pluginCounts);
} }
} }