From 42f9b38f68cc07cd494dbbc19c3554815bd5d600 Mon Sep 17 00:00:00 2001 From: Athou Date: Thu, 21 Mar 2013 23:10:05 +0100 Subject: [PATCH] category formatting --- .../frontend/rest/FeedSubscriptionsREST.java | 139 ++++++++++++++++++ src/main/webapp/directives/category.html | 2 +- src/main/webapp/js/controllers.js | 32 +++- src/main/webapp/js/directives.js | 5 +- src/main/webapp/templates/feeds.html | 2 +- 5 files changed, 173 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java diff --git a/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java b/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java new file mode 100644 index 00000000..2216e79f --- /dev/null +++ b/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java @@ -0,0 +1,139 @@ +package com.commafeed.frontend.rest; + +import java.util.List; + +import javax.inject.Inject; + +import org.apache.commons.lang.ObjectUtils; + +import com.commafeed.backend.dao.FeedCategoryService; +import com.commafeed.backend.dao.FeedSubscriptionService; +import com.commafeed.frontend.CommaFeedSession; +import com.commafeed.frontend.pages.JSONPage; +import com.commafeed.frontend.utils.ModelFactory.MF; +import com.commafeed.model.FeedCategory; +import com.commafeed.model.FeedSubscription; +import com.commafeed.model.User; +import com.google.common.collect.Lists; + +@SuppressWarnings("serial") +public class FeedSubscriptionsREST extends JSONPage { + + @Inject + FeedSubscriptionService feedSubscriptionService; + + @Inject + FeedCategoryService FeedCategoryService; + + @Override + protected Object getObject() { + + User user = CommaFeedSession.get().getUser(); + List categories = FeedCategoryService.findAll(user); + + Category root = new Category(); + addChildren(categories, root); + for (FeedSubscription subscription : feedSubscriptionService + .findByField(MF.i(MF.p(FeedSubscription.class).getCategory()), + null)) { + Subscription sub = new Subscription(); + sub.setId(subscription.getId()); + sub.setName(subscription.getTitle()); + sub.setUnread(77); + } + return root; + } + + private void addChildren(List categories, Category current) { + for (FeedCategory category : categories) { + if ((category.getParent() == null && current.getId() == null) + || (category.getParent() != null && (ObjectUtils.equals( + category.getParent().getId(), current.getId())))) { + Category child = new Category(); + child.setId(category.getId()); + child.setName(category.getName()); + addChildren(categories, child); + for (FeedSubscription subscription : category + .getSubscriptions()) { + Subscription sub = new Subscription(); + sub.setId(subscription.getId()); + sub.setName(subscription.getTitle()); + sub.setUnread(77); + } + current.getChildren().add(child); + } + } + } + + public static class Category { + private Long id; + private String name; + private List children = Lists.newArrayList(); + private List feeds = Lists.newArrayList(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + + public List getFeeds() { + return feeds; + } + + public void setFeeds(List feeds) { + this.feeds = feeds; + } + + } + + public static class Subscription { + private Long id; + private String name; + private int unread; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getUnread() { + return unread; + } + + public void setUnread(int unread) { + this.unread = unread; + } + + } + +} \ No newline at end of file diff --git a/src/main/webapp/directives/category.html b/src/main/webapp/directives/category.html index cb7b0c67..cd3be7d9 100644 --- a/src/main/webapp/directives/category.html +++ b/src/main/webapp/directives/category.html @@ -1,6 +1,6 @@
  • diff --git a/src/main/webapp/js/controllers.js b/src/main/webapp/js/controllers.js index 827c8410..27134b98 100644 --- a/src/main/webapp/js/controllers.js +++ b/src/main/webapp/js/controllers.js @@ -12,14 +12,16 @@ module.controller('CategoryTreeCtrl', name : "News", feeds : [ { id : "2", - name : "Cyanide & Happiness" + name : "Cyanide & Happiness", + unread : 34 } ], children : [ { id : "2", name : "Comics", feeds : [ { id : "1", - name : "Dilbert" + name : "Dilbert", + unread : 4 } ] } ] }, { @@ -27,11 +29,35 @@ module.controller('CategoryTreeCtrl', name : "Blogs", feeds : [ { id : "3", - name : "Engadget" + name : "Engadget", + unread : 0 } ] } ] }; + var unreadCount = function(category) { + var count = 0; + console.log(category) + for ( var child in category.children) { + count = count + unreadCount(child); + } + for ( var feed in category.feeds) { + if (feed.unread) { + count = count + feed.unread; + } + } + return count; + } + + $scope.formatCategoryName = function(category) { + var count = unreadCount(category); + var label = category.name; + if (count > 0) { + label = label + " (" + count + ")"; + } + return label; + } + $scope.feedClicked = function(id) { $location.path('/feeds/view/feed/' + id); }; diff --git a/src/main/webapp/js/directives.js b/src/main/webapp/js/directives.js index 99f368e1..232c4c7a 100644 --- a/src/main/webapp/js/directives.js +++ b/src/main/webapp/js/directives.js @@ -7,7 +7,8 @@ module.directive('category', function($compile) { selectedType: '=', selectedId: '=', feedClick: '&', - categoryClick: '&' + categoryClick: '&', + formatCategoryName: '&' }, restrict : 'E', replace: true, @@ -15,7 +16,7 @@ module.directive('category', function($compile) { link: function(scope, element) { if (scope.node.children) { var ul = element.find('ul'); - ul.prepend(''); + ul.prepend(''); $compile(ul.contents())(scope); } } diff --git a/src/main/webapp/templates/feeds.html b/src/main/webapp/templates/feeds.html index 18594bd6..ac337284 100644 --- a/src/main/webapp/templates/feeds.html +++ b/src/main/webapp/templates/feeds.html @@ -4,7 +4,7 @@
    • - +