From 9a254c2af6f176c8a95ef6accfdb288ad159d125 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 20 Jun 2011 07:53:02 +0200 Subject: [PATCH] =?utf8?q?Add=20filter=20and=20sort=20options=20to=20?= =?utf8?q?=E2=80=9Cknown=20Sones=E2=80=9D=20page.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/KnownSonesPage.java | 44 +++++++++++++++++++++- src/main/resources/static/css/sone.css | 4 ++ src/main/resources/templates/knownSones.html | 44 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java index 7c8f9ad..0e42eb5 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -23,6 +23,8 @@ import java.util.List; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.collection.ReverseComparator; +import net.pterodactylus.util.filter.Filter; import net.pterodactylus.util.filter.Filters; import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; @@ -57,11 +59,49 @@ public class KnownSonesPage extends SoneTemplatePage { @Override protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); + String sortField = request.getHttpRequest().getParam("sort"); + String sortOrder = request.getHttpRequest().getParam("order"); + String followedSones = request.getHttpRequest().getParam("followedSones"); + final Sone currentSone = getCurrentSone(request.getToadletContext(), false); List knownSones = Filters.filteredList(new ArrayList(webInterface.getCore().getSones()), Sone.EMPTY_SONE_FILTER); - Collections.sort(knownSones, Sone.NICE_NAME_COMPARATOR); + if ((currentSone != null) && "show-only".equals(followedSones)) { + knownSones = Filters.filteredList(knownSones, new Filter() { + + @Override + public boolean filterObject(Sone sone) { + return currentSone.hasFriend(sone.getId()); + } + }); + templateContext.set("followedSones", "show-only"); + } else if ((currentSone != null) && "hide".equals(followedSones)) { + knownSones = Filters.filteredList(knownSones, new Filter() { + + @Override + public boolean filterObject(Sone sone) { + return !currentSone.hasFriend(sone.getId()); + } + }); + templateContext.set("followedSones", "hide"); + } + if ("name".equals(sortField)) { + if ("desc".equals(sortOrder)) { + Collections.sort(knownSones, new ReverseComparator(Sone.NICE_NAME_COMPARATOR)); + } else { + Collections.sort(knownSones, Sone.NICE_NAME_COMPARATOR); + } + templateContext.set("sort", "name"); + templateContext.set("order", "desc".equals(sortOrder) ? "desc" : "asc"); + } else if ("activity".equals(sortField)) { + if ("asc".equals(sortOrder)) { + Collections.sort(knownSones, new ReverseComparator(Sone.LAST_ACTIVITY_COMPARATOR)); + } else { + Collections.sort(knownSones, Sone.LAST_ACTIVITY_COMPARATOR); + } + templateContext.set("sort", "activity"); + templateContext.set("order", "asc".equals(sortOrder) ? "asc" : "desc"); + } Pagination sonePagination = new Pagination(knownSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); templateContext.set("pagination", sonePagination); templateContext.set("knownSones", sonePagination.getItems()); } - } diff --git a/src/main/resources/static/css/sone.css b/src/main/resources/static/css/sone.css index 1dda47e..d38cea7 100644 --- a/src/main/resources/static/css/sone.css +++ b/src/main/resources/static/css/sone.css @@ -680,3 +680,7 @@ textarea { color: red; font-style: italic; } + +#sone #sort-options { + margin-bottom: 1em; +} diff --git a/src/main/resources/templates/knownSones.html b/src/main/resources/templates/knownSones.html index 33f0e27..fde1060 100644 --- a/src/main/resources/templates/knownSones.html +++ b/src/main/resources/templates/knownSones.html @@ -1,8 +1,52 @@ <%include include/head.html> + +

<%= Page.KnownSones.Page.Title|l10n|html>

+ +
+
+
+ Sort: + + +
+ <%ifnull !currentSone> +
+ Followed Sones: + +
+ <%/if> +
+ +
+
+
<%= page|store key=pageParameter> -- 2.7.4