diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-23 12:53:03 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-23 12:53:03 +0200 |
commit | f1261587eb4f8283915f18d162173a6a3a8012b0 (patch) | |
tree | 1b617ad37c0e35f051d75716c2c4d58e0ea7e6a5 /js/service/FeedResource.js | |
parent | c12a2504cd25ea03077a09101155654efc10d971 (diff) |
cache unread count
Diffstat (limited to 'js/service/FeedResource.js')
-rw-r--r-- | js/service/FeedResource.js | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/js/service/FeedResource.js b/js/service/FeedResource.js index 548c5a27c..5880c83fd 100644 --- a/js/service/FeedResource.js +++ b/js/service/FeedResource.js @@ -15,6 +15,31 @@ app.factory('FeedResource', (Resource, $http, BASE_URL) => { constructor ($http, BASE_URL) { super($http, BASE_URL, 'url'); this.ids = {}; + this.unreadCount = 0; + this.folderUnreadCount = {}; + } + + + receive (data) { + super.receive(data); + this.updateUnreadCache(); + } + + + updateUnreadCache () { + this.unreadCount = 0; + this.folderUnreadCount = {}; + + for (let value of this.values) { + if (value.unreadCount) { + this.unreadCount += value.unreadCount; + } + if (value.folderId !== undefined) { + this.folderUnreadCount[value.folderId] = + this.folderUnreadCount[value.folderId] || 0; + this.folderUnreadCount[value.folderId] += value.unreadCount; + } + } } @@ -37,29 +62,47 @@ app.factory('FeedResource', (Resource, $http, BASE_URL) => { for (let feed of this.values) { feed.unreadCount = 0; } + this.unreadCount = 0; + this.folderUnreadCount = {}; } markFeedRead (feedId) { this.ids[feedId].unreadCount = 0; + this.updateUnreadCache(); + } + + markFolderRead (folderId) { + for (let feed of this.values) { + if (feed.folderId === folderId) { + feed.unreadCount = 0; + } + } + this.updateUnreadCache(); } markItemOfFeedRead (feedId) { this.ids[feedId].unreadCount -= 1; + this.updateUnreadCache(); } markItemOfFeedUnread (feedId) { this.ids[feedId].unreadCount += 1; + this.updateUnreadCache(); } getUnreadCount () { - return this.values.reduce((sum, feed) => sum + feed.unreadCount, 0); + return this.unreadCount; } + getFolderUnreadCount (folderId) { + return this.folderUnreadCount[folderId] || 0; + } + } return new FeedResource($http, BASE_URL); |