1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2025-04-09 10:15:44 +02:00

cleaned up feedcontroller

This commit is contained in:
Bernhard Posselt 2013-04-04 21:57:17 +02:00
parent 76267670f3
commit d001da3394
10 changed files with 209 additions and 304 deletions

View File

@ -29,14 +29,12 @@ angular.module('News').controller 'SettingsController',
angular.module('News').controller 'FeedController',
['$scope', '_FeedController', 'FolderModel', 'FeedModel', 'ActiveFeed',
'ShowAll', 'FeedType', 'StarredCount', 'Persistence', 'FolderBl', 'FeedBl',
($scope, _FeedController, FolderModel, FeedModel, ActiveFeed,
ShowAll, FeedType, StarredCount, Persistence, FolderBl, FeedBl)->
['$scope', '_FeedController', 'Persistence', 'FolderBl', 'FeedBl',
'unreadCountFormatter',
($scope, _FeedController, Persistence, FolderBl, FeedBl, unreadCountFormatter)->
return new _FeedController($scope, FolderModel, FeedModel, ActiveFeed,
ShowAll, FeedType, StarredCount, Persistence,
FolderBl, FeedBl)
return new _FeedController($scope, Persistence, FolderBl, FeedBl,
unreadCountFormatter)
]
angular.module('News').controller 'ItemController',

View File

@ -25,66 +25,24 @@ angular.module('News').factory '_FeedController', ->
class FeedController
constructor: (@$scope, @_folderModel, @_feedModel, @_active,
@_showAll, @_feedType, @_starredCount, @_persistence,
@_folderBl, @_feedBl) ->
constructor: (@$scope, @_persistence, @_folderBl, @_feedBl,
@_unreadCountFormatter) ->
@_isAddingFolder = false
@_isAddingFeed = false
# bind internal stuff to scope
@$scope.feeds = @_feedModel.getAll()
@$scope.folders = @_folderModel.getAll()
@$scope.feedType = @_feedType
@$scope.folderBl = @_folderBl
@$scope.feedBl = @_feedBl
@$scope.unreadCountFormatter = @_unreadCountFormatter
@$scope.isShown = (type, id) =>
return true
@$scope.getUnreadCount = =>
return @_transFormCount(@_feedBl.getUnreadCount())
@$scope.getStarredCount = =>
return @_transFormCount(@_starredCount.getStarredCount())
@$scope.getFeedUnreadCount = (feedId) =>
return @_transFormCount(@_feedBl.getFeedUnreadCount(feedId))
@$scope.getUnreadCount = (folderId) =>
return @_transFormCount(@_feedBl.getFolderUnreadCount(folderId))
@$scope.isShowAll = =>
return @isShowAll()
@$scope.loadFeed = (type, id) =>
@loadFeed(type, id)
@$scope.hasFeeds = (folderId) =>
return @hasFeeds(folderId)
@$scope.delete = (type, id) =>
@delete(type, id)
@$scope.markAllRead = (type, id) =>
@markAllRead(type, id)
@$scope.getFeedsOfFolder = (folderId) =>
return @getFeedsOfFolder(folderId)
@$scope.setShowAll = (showAll) =>
@setShowAll(showAll)
@$scope.isAddingFolder = =>
return @_isAddingFolder
@$scope.isAddingFeed = =>
return @_isAddingFeed
@$scope.toggleFolder = (folderId) =>
@toggleFolder(folderId)
@$scope.addFeed = (feedUrl, parentFolderId=0) =>
@$scope.feedEmptyError = false
@$scope.feedError = false
@ -129,54 +87,4 @@ angular.module('News').factory '_FeedController', ->
@_isAddingFolder = false
isFeedActive: (type, id) ->
return type == @_active.getType() and id == @_active.getId()
#isShown: (type, id) ->
# hasUnread = @getUnreadCount(type, id) > 0
# if hasUnread
# return true
# else
# if @isShowAll()
# switch type
# when @_feedType.Subscriptions
# return @_feedModel.size() > 0
# when @_feedType.Folder
# return @_folderModel.size() > 0
# when @_feedType.Feed
# return @_feedModel.size() > 0
# return false
isShowAll: ->
return @_showAll.getShowAll()
_transFormCount: (count) ->
if count > 999
count = '999+'
return count
loadFeed: (type, id) ->
# TODO: use polymorphism instead of switches
if type != @_active.getType() or id != @_active.getId()
@_itemModel.clear()
@_persistence.getItems(type, id, 0)
@_active.handle({id: id, type: type})
else
lastModified = @_itemModel.getHighestId()
@_persistence.getItems(type, id, 0, null, lastModified)
setShowAll: (showAll) ->
@_showAll.setShowAll(showAll)
if showAll
@_persistence.userSettingsReadShow()
else
@_persistence.userSettingsReadHide()
return FeedController

View File

@ -90,6 +90,18 @@ angular.module('News').factory 'FeedBl',
@_persistence.moveFeed(feedId, folderId)
setShowAll: (showAll) ->
@_showAll.setShowAll(showAll)
if showAll
@_persistence.userSettingsReadShow()
else
@_persistence.userSettingsReadHide()
getAll: ->
return @_feedModel.getAll()
return new FeedBl(ShowAll, FeedModel, Persistence, ActiveFeed, FeedType,
ItemModel)

View File

@ -61,7 +61,7 @@ ItemModel, ShowAll)->
getUnreadCount: (folderId) ->
return @_feedBl.getFolderUnreadCount(folderId)
isVisible: (folderId) ->
if @_showAll.getShowAll()
@ -77,6 +77,10 @@ ItemModel, ShowAll)->
return false
getAll: ->
return @_folderModel.getAll()
return new FolderBl(FolderModel, FeedBl, ShowAll, ActiveFeed, Persistence,
FeedType, ItemModel)

View File

@ -0,0 +1,28 @@
###
ownCloud - News
@author Bernhard Posselt
@copyright 2012 Bernhard Posselt nukeawhale@gmail.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU AFFERO GENERAL PUBLIC LICENSE for more details.
You should have received a copy of the GNU Affero General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
###
angular.module('News').factory 'unreadCountFormatter', ->
return (unreadCount) ->
if unreadCount > 999
unreadCount = '999+'
return unreadCount

View File

@ -171,8 +171,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
]);
angular.module('News').controller('FeedController', [
'$scope', '_FeedController', 'FolderModel', 'FeedModel', 'ActiveFeed', 'ShowAll', 'FeedType', 'StarredCount', 'Persistence', 'FolderBl', 'FeedBl', function($scope, _FeedController, FolderModel, FeedModel, ActiveFeed, ShowAll, FeedType, StarredCount, Persistence, FolderBl, FeedBl) {
return new _FeedController($scope, FolderModel, FeedModel, ActiveFeed, ShowAll, FeedType, StarredCount, Persistence, FolderBl, FeedBl);
'$scope', '_FeedController', 'Persistence', 'FolderBl', 'FeedBl', 'unreadCountFormatter', function($scope, _FeedController, Persistence, FolderBl, FeedBl, unreadCountFormatter) {
return new _FeedController($scope, Persistence, FolderBl, FeedBl, unreadCountFormatter);
}
]);
@ -214,70 +214,24 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
var FeedController;
FeedController = (function() {
function FeedController($scope, _folderModel, _feedModel, _active, _showAll, _feedType, _starredCount, _persistence, _folderBl, _feedBl) {
function FeedController($scope, _persistence, _folderBl, _feedBl, _unreadCountFormatter) {
var _this = this;
this.$scope = $scope;
this._folderModel = _folderModel;
this._feedModel = _feedModel;
this._active = _active;
this._showAll = _showAll;
this._feedType = _feedType;
this._starredCount = _starredCount;
this._persistence = _persistence;
this._folderBl = _folderBl;
this._feedBl = _feedBl;
this._unreadCountFormatter = _unreadCountFormatter;
this._isAddingFolder = false;
this._isAddingFeed = false;
this.$scope.feeds = this._feedModel.getAll();
this.$scope.folders = this._folderModel.getAll();
this.$scope.feedType = this._feedType;
this.$scope.folderBl = this._folderBl;
this.$scope.feedBl = this._feedBl;
this.$scope.isShown = function(type, id) {
return true;
};
this.$scope.getUnreadCount = function() {
return _this._transFormCount(_this._feedBl.getUnreadCount());
};
this.$scope.getStarredCount = function() {
return _this._transFormCount(_this._starredCount.getStarredCount());
};
this.$scope.getFeedUnreadCount = function(feedId) {
return _this._transFormCount(_this._feedBl.getFeedUnreadCount(feedId));
};
this.$scope.getUnreadCount = function(folderId) {
return _this._transFormCount(_this._feedBl.getFolderUnreadCount(folderId));
};
this.$scope.isShowAll = function() {
return _this.isShowAll();
};
this.$scope.loadFeed = function(type, id) {
return _this.loadFeed(type, id);
};
this.$scope.hasFeeds = function(folderId) {
return _this.hasFeeds(folderId);
};
this.$scope["delete"] = function(type, id) {
return _this["delete"](type, id);
};
this.$scope.markAllRead = function(type, id) {
return _this.markAllRead(type, id);
};
this.$scope.getFeedsOfFolder = function(folderId) {
return _this.getFeedsOfFolder(folderId);
};
this.$scope.setShowAll = function(showAll) {
return _this.setShowAll(showAll);
};
this.$scope.unreadCountFormatter = this._unreadCountFormatter;
this.$scope.isAddingFolder = function() {
return _this._isAddingFolder;
};
this.$scope.isAddingFeed = function() {
return _this._isAddingFeed;
};
this.$scope.toggleFolder = function(folderId) {
return _this.toggleFolder(folderId);
};
this.$scope.addFeed = function(feedUrl, parentFolderId) {
var onError, onSuccess;
if (parentFolderId == null) {
@ -327,45 +281,6 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
};
}
FeedController.prototype.isFeedActive = function(type, id) {
return type === this._active.getType() && id === this._active.getId();
};
FeedController.prototype.isShowAll = function() {
return this._showAll.getShowAll();
};
FeedController.prototype._transFormCount = function(count) {
if (count > 999) {
count = '999+';
}
return count;
};
FeedController.prototype.loadFeed = function(type, id) {
var lastModified;
if (type !== this._active.getType() || id !== this._active.getId()) {
this._itemModel.clear();
this._persistence.getItems(type, id, 0);
return this._active.handle({
id: id,
type: type
});
} else {
lastModified = this._itemModel.getHighestId();
return this._persistence.getItems(type, id, 0, null, lastModified);
}
};
FeedController.prototype.setShowAll = function(showAll) {
this._showAll.setShowAll(showAll);
if (showAll) {
return this._persistence.userSettingsReadShow();
} else {
return this._persistence.userSettingsReadHide();
}
};
return FeedController;
})();
@ -718,6 +633,19 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
}
};
FeedBl.prototype.setShowAll = function(showAll) {
this._showAll.setShowAll(showAll);
if (showAll) {
return this._persistence.userSettingsReadShow();
} else {
return this._persistence.userSettingsReadHide();
}
};
FeedBl.prototype.getAll = function() {
return this._feedModel.getAll();
};
return FeedBl;
})(_Bl);
@ -828,6 +756,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
}
};
FolderBl.prototype.getAll = function() {
return this._folderModel.getAll();
};
return FolderBl;
})(_Bl);
@ -2177,4 +2109,41 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
}).call(this);
// Generated by CoffeeScript 1.4.0
/*
ownCloud - News
@author Bernhard Posselt
@copyright 2012 Bernhard Posselt nukeawhale@gmail.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU AFFERO GENERAL PUBLIC LICENSE for more details.
You should have received a copy of the GNU Affero General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
(function() {
angular.module('News').factory('unreadCountFormatter', function() {
return function(unreadCount) {
if (unreadCount > 999) {
unreadCount = '999+';
}
return unreadCount;
};
});
}).call(this);
})(window.angular, jQuery);

View File

@ -23,130 +23,43 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
describe '_FeedController', ->
beforeEach module 'News'
beforeEach inject (@_FeedController, @ActiveFeed, @ShowAll, @FeedType,
@StarredCount, @FeedModel, @FolderModel, @FeedBl) =>
@scope =
$on: ->
@persistence =
getItems: ->
@controller = new @_FeedController(@scope, @FolderModel, @FeedModel,
@ActiveFeed, @ShowAll, @FeedType,
@StarredCount, @persistence,
@FeedBl)
beforeEach =>
angular.module('News').factory 'Persistence', =>
@persistence = {}
xit 'should make folders available', =>
@FolderModel.getAll = jasmine.createSpy('FolderModel')
new @_FeedController(@scope, @FolderModel, @FeedModel, @_ActiveFeed)
beforeEach inject (@_FeedController, @FolderBl, @FeedBl, $rootScope,
@unreadCountFormatter) =>
@scope = $rootScope.$new()
expect(@FolderModel.getAll).toHaveBeenCalled()
xit 'should make feeds availabe', =>
@FeedModel.getAll = jasmine.createSpy('FeedModel')
new @_FeedController(@scope, @FolderModel, @FeedModel, @_ActiveFeed)
expect(@FeedModel.getAll).toHaveBeenCalled()
xit 'should make feedtype available', =>
expect(@scope.feedType).toBe(@FeedType)
xit 'should check the active feed', =>
@ActiveFeed.getType = =>
return @FeedType.Feed
@ActiveFeed.getId = =>
return 5
expect(@scope.isFeedActive(@FeedType.Feed, 5)).toBeTruthy()
xit 'should provide ShowAll', =>
expect(@scope.isShowAll()).toBeFalsy()
@ShowAll.setShowAll(true)
expect(@scope.isShowAll()).toBeTruthy()
xit 'should handle show all correctly', =>
@persistence.userSettingsReadHide = jasmine.createSpy('hide')
@persistence.userSettingsReadShow = jasmine.createSpy('show')
@scope.setShowAll(true)
expect(@ShowAll.getShowAll()).toBeTruthy()
expect(@persistence.userSettingsReadShow).toHaveBeenCalled()
expect(@persistence.userSettingsReadHide).not.toHaveBeenCalled()
xit 'should handle hide all correctly', =>
@persistence.userSettingsReadHide = jasmine.createSpy('hide')
@persistence.userSettingsReadShow = jasmine.createSpy('show')
@scope.setShowAll(false)
expect(@ShowAll.getShowAll()).toBeFalsy()
expect(@persistence.userSettingsReadShow).not.toHaveBeenCalled()
expect(@persistence.userSettingsReadHide).toHaveBeenCalled()
xit 'should get the correct count for starred items', =>
@StarredCount.setStarredCount(133)
count = @scope.getUnreadCount(@FeedType.Starred, 0)
expect(count).toBe(133)
xit 'should set the count to 999+ if the count is over 999', =>
@StarredCount.setStarredCount(1000)
count = @scope.getUnreadCount(@FeedType.Starred, 0)
expect(count).toBe('999+')
@controller = new @_FeedController(@scope, @persistence, @FolderBl,
@FeedBl, @unreadCountFormatter)
xit 'should set active feed to new feed if changed', =>
@ActiveFeed.handle({id: 3, type: 3})
@scope.loadFeed(4, 3)
expect(@ActiveFeed.getId()).toBe(3)
expect(@ActiveFeed.getType()).toBe(4)
xit 'should return true when calling isShown and there are feeds', =>
@FeedModel.add({id: 3})
@ShowAll.setShowAll(true)
expect(@scope.isShown(3, 4)).toBeTruthy()
@ShowAll.setShowAll(false)
expect(@scope.isShown(3, 4)).toBeFalsy()
xit 'should return true if ShowAll is false but unreadcount is not 0', =>
@ShowAll.setShowAll(false)
@FeedModel.add({id: 4, unreadCount: 0, urlHash: 'a1'})
expect(@scope.isShown(@FeedType.Feed, 4)).toBeFalsy()
@FeedModel.add({id: 4, unreadCount: 12, urlHash: 'a2'})
expect(@scope.isShown(@FeedType.Feed, 4)).toBeTruthy()
xit 'isAddingFolder should return false in the beginning', =>
it 'isAddingFolder should return false in the beginning', =>
expect(@scope.isAddingFolder()).toBeFalsy()
xit 'isAddingFeed should return false in the beginning', =>
it 'isAddingFeed should return false in the beginning', =>
expect(@scope.isAddingFeed()).toBeFalsy()
xit 'should not add folders that have no name', =>
it 'should make unreadCountFormatter available', =>
expect(@scope.unreadCountFormatter).toBe(@unreadCountFormatter)
it 'should make FeedBl available', =>
expect(@scope.feedBl).toBe(@FeedBl)
it 'should make FolderBl available', =>
expect(@scope.folderBl).toBe(@FolderBl)
it 'should not add folders that have no name', =>
@persistence.createFolder = jasmine.createSpy('create')
@scope.addFolder(' ')

View File

@ -152,4 +152,30 @@ describe 'FeedBl', ->
@FeedModel.add({id: 2, unreadCount:134, urlHash: 'a1', folderId: 3})
@FeedBl.move(2, 3)
expect(@persistence.moveFeed).not.toHaveBeenCalled()
expect(@persistence.moveFeed).not.toHaveBeenCalled()
it 'should set the show all setting', =>
@persistence.userSettingsReadShow = jasmine.createSpy('Show All')
@FeedBl.setShowAll(true)
expect(@persistence.userSettingsReadShow).toHaveBeenCalled()
it 'should set the hide read setting', =>
@persistence.userSettingsReadHide = jasmine.createSpy('Hide Read')
@FeedBl.setShowAll(false)
expect(@persistence.userSettingsReadHide).toHaveBeenCalled()
it 'should return all feeds', =>
item1 = {id: 2, unreadCount:134, urlHash: 'a1', folderId: 3}
item2 = {id: 4, unreadCount:134, urlHash: 'a2', folderId: 3}
@FeedModel.add(item1)
@FeedModel.add(item2)
expect(@FeedBl.getAll()).toContain(item1)
expect(@FeedBl.getAll()).toContain(item2)

View File

@ -118,3 +118,15 @@ describe 'FolderBl', ->
@ActiveFeed.handle({type: @FeedType.Folder, id:2})
expect(@FolderBl.isVisible(3)).toBe(true)
it 'should return all folders', =>
item1 = {id: 3, open: false}
item2 = {id: 4, open: true}
@FolderModel.add(item1)
@FolderModel.add(item2)
expect(@FolderBl.getAll()).toContain(item1)
expect(@FolderBl.getAll()).toContain(item2)

View File

@ -0,0 +1,35 @@
###
ownCloud - News
@author Bernhard Posselt
@copyright 2012 Bernhard Posselt nukeawhale@gmail.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU AFFERO GENERAL PUBLIC LICENSE for more details.
You should have received a copy of the GNU Affero General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
###
describe 'unreadCountFormatter', ->
beforeEach module 'News'
beforeEach inject (@unreadCountFormatter) =>
it 'should return the normal count if its below 999', =>
expect(@unreadCountFormatter(999)).toBe(999)
it 'should set the count to 999+ if the count is over 999', =>
expect(@unreadCountFormatter(1000)).toBe('999+')