infinite-scrolling

This commit is contained in:
Jeremie Panzer
2013-03-25 13:52:02 +01:00
parent f0c4733b76
commit 0443fbf267
8 changed files with 82 additions and 18 deletions

View File

@@ -39,21 +39,39 @@ public class FeedEntryService extends GenericDAO<FeedEntry, Long> {
em.merge(feed);
}
public List<FeedEntry> getUnreadEntries(Feed feed, User user) {
private TypedQuery<FeedEntry> unreadQuery(Feed feed, User user) {
String query = "select e from FeedEntry e where e.feed=:feed and not exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read = true)";
TypedQuery<FeedEntry> typedQuery = em.createQuery(query,
FeedEntry.class);
typedQuery.setParameter("feed", feed);
typedQuery.setParameter("user", user);
return typedQuery.getResultList();
return typedQuery;
}
public List<FeedEntry> getAllEntries(Feed feed) {
public List<FeedEntry> getUnreadEntries(Feed feed, User user) {
return unreadQuery(feed, user).getResultList();
}
public List<FeedEntry> getUnreadEntries(Feed feed, User user, int offset,
int limit) {
return unreadQuery(feed, user).setFirstResult(offset)
.setMaxResults(limit).getResultList();
}
private TypedQuery<FeedEntry> allQuery(Feed feed) {
String query = "select e from FeedEntry e where e.feed=:feed";
TypedQuery<FeedEntry> typedQuery = em.createQuery(query,
FeedEntry.class);
typedQuery.setParameter("feed", feed);
return typedQuery.getResultList();
return typedQuery;
}
public List<FeedEntry> getAllEntries(Feed feed) {
return allQuery(feed).getResultList();
}
public List<FeedEntry> getAllEntries(Feed feed, int offset, int limit) {
return allQuery(feed).setFirstResult(offset).setMaxResults(limit)
.getResultList();
}
}

View File

@@ -50,9 +50,8 @@ public class FeedTimer {
feedEntryService
.updateEntries(feed.getUrl(), feed.getEntries());
} catch (Exception e) {
log.info(
"Unable to refresh feed " + key + " : "
+ e.getMessage(), e);
log.info("Unable to refresh feed " + key + " : "
+ e.getMessage());
Feed feed = feeds.get(key);
feed.setLastUpdated(Calendar.getInstance().getTime());

View File

@@ -39,6 +39,7 @@
<script src="vendor/angular/angular-resource.min.js"></script>
<script src="vendor/angular/angular-sanitize.min.js"></script>
<script src="vendor/angular-upload/ng-upload.min.js"></script>
<script src="vendor/angular-infinite-scroll/ng-infinite-scroll.min.js"></script>
<script src="vendor/angular-ui/angular-ui.min.js"></script>
<script src="vendor/angular-ui-bootstrap/ui-bootstrap-tpls-0.2.0.min.js"></script>
<script src="js/main.js"></script>

View File

@@ -5,9 +5,11 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import org.apache.commons.lang.ObjectUtils;
@@ -27,7 +29,9 @@ public class EntriesREST extends AbstractREST {
@Path("get/{type}/{id}/{readType}")
@GET
public Entries getEntries(@PathParam("type") String type,
@PathParam("id") String id, @PathParam("readType") String readType) {
@PathParam("id") String id, @PathParam("readType") String readType,
@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("-1") @QueryParam("limit") int limit) {
Entries entries = new Entries();
boolean unreadOnly = "unread".equals(readType);
@@ -36,7 +40,8 @@ public class EntriesREST extends AbstractREST {
FeedSubscription subscription = feedSubscriptionService.findById(
getUser(), Long.valueOf(id));
entries.setName(subscription.getTitle());
entries.getEntries().addAll(buildEntries(subscription, unreadOnly));
entries.getEntries().addAll(
buildEntries(subscription, offset, limit, unreadOnly));
} else {
FeedCategory feedCategory = "all".equals(id) ? null
@@ -48,7 +53,7 @@ public class EntriesREST extends AbstractREST {
entries.setName("all".equals(id) ? "All" : feedCategory.getName());
for (FeedSubscription subscription : subscriptions) {
entries.getEntries().addAll(
buildEntries(subscription, unreadOnly));
buildEntries(subscription, offset, limit, unreadOnly));
}
}
@@ -59,19 +64,27 @@ public class EntriesREST extends AbstractREST {
return ObjectUtils.compare(e2.getDate(), e1.getDate());
}
});
int lastIndex = entries.getEntries().size()
- (entries.getEntries().isEmpty() ? 0 : 1);
int from = Math.min(lastIndex, offset);
int to = limit == -1 ? lastIndex : Math.min(lastIndex, offset + limit);
List<Entry> subList = entries.getEntries().subList(from, to);
entries.setEntries(Lists.newArrayList(subList));
return entries;
}
private List<Entry> buildEntries(FeedSubscription subscription,
boolean unreadOnly) {
private List<Entry> buildEntries(FeedSubscription subscription, int offset,
int limit, boolean unreadOnly) {
List<FeedEntry> feedEntries = null;
if (unreadOnly) {
feedEntries = feedEntryService.getUnreadEntries(
subscription.getFeed(), getUser());
subscription.getFeed(), getUser(), offset, limit);
} else {
feedEntries = feedEntryService
.getAllEntries(subscription.getFeed());
feedEntries = feedEntryService.getAllEntries(
subscription.getFeed(), offset, limit);
}
List<Entry> entries = Lists.newArrayList();