diff --git a/app/archive/archive.html b/app/archive/archive.html index d9ff685..7a4ea67 100644 --- a/app/archive/archive.html +++ b/app/archive/archive.html @@ -44,7 +44,7 @@ - +
{{o.name}}
@@ -90,4 +90,4 @@
- \ No newline at end of file + diff --git a/app/archive/archive.js b/app/archive/archive.js index fe12d8a..9ab4110 100644 --- a/app/archive/archive.js +++ b/app/archive/archive.js @@ -166,6 +166,10 @@ angular.module('jamstash.archive.controller', ['jamstash.archive.service']) $rootScope.removeSong(item, $scope.song); }; + $scope.toggleStar = function (item) { + //Do nothing: we aren't logged in archive.org, so we can't star anything there. + }; + /* Launch on Startup */ //$scope.getArtists(); $scope.getAlbums(); diff --git a/app/common/main-controller.js b/app/common/main-controller.js index e972870..55315f5 100644 --- a/app/common/main-controller.js +++ b/app/common/main-controller.js @@ -1,6 +1,6 @@ angular.module('JamStash') -.controller('AppController', ['$scope', '$rootScope', '$document', '$window', '$location', '$cookieStore', '$http', 'utils', 'globals', 'model', 'notifications', 'player', 'persistence', 'Page', - function($scope, $rootScope, $document, $window, $location, $cookieStore, $http, utils, globals, model, notifications, player, persistence, Page) { +.controller('AppController', ['$scope', '$rootScope', '$document', '$window', '$location', '$cookieStore', '$http', 'utils', 'globals', 'model', 'notifications', 'player', 'persistence', 'Page', 'subsonic', + function ($scope, $rootScope, $document, $window, $location, $cookieStore, $http, utils, globals, model, notifications, player, persistence, Page, subsonic) { 'use strict'; $rootScope.settings = globals.settings; @@ -380,27 +380,13 @@ angular.module('JamStash') }); }; - $scope.updateFavorite = function (item) { - var id = item.id; - var starred = item.starred; - var url; - if (starred) { - url = globals.BaseURL() + '/unstar.view?' + globals.BaseParams() + '&id=' + id; - item.starred = undefined; - } else { - url = globals.BaseURL() + '/star.view?' + globals.BaseParams() + '&id=' + id; - item.starred = true; - } - $.ajax({ - url: url, - method: 'GET', - dataType: globals.settings.Protocol, - timeout: globals.settings.Timeout, - success: function () { - notifications.updateMessage('Favorite Updated!', true); - } + $scope.toggleStar = function (item) { + subsonic.toggleStar(item).then(function (newStarred) { + item.starred = newStarred; + notifications.updateMessage('Favorite Updated!', true); }); }; + $scope.toTrusted = function (html) { return $sce.trustAsHtml(html); }; diff --git a/app/common/main-controller_test.js b/app/common/main-controller_test.js index 8c9b659..44fe035 100644 --- a/app/common/main-controller_test.js +++ b/app/common/main-controller_test.js @@ -1,13 +1,14 @@ describe("Main controller", function() { 'use strict'; - var controllerParams, $controller, scope, mockGlobals, player, utils, persistence; + var controllerParams, $controller, $q, scope, mockGlobals, player, utils, persistence, subsonic, notifications, + deferred; beforeEach(function() { mockGlobals = { settings: { SaveTrackPosition: false, ShowQueue: false, - Debug: true, + Debug: false, Jukebox: false } }; @@ -30,9 +31,21 @@ describe("Main controller", function() { "saveSettings" ]); - inject(function (_$controller_, $rootScope, _$document_, _$window_, _$location_, _$cookieStore_, _utils_, globals, _model_, _notifications_, _Page_) { + // Mock the subsonic service + subsonic = jasmine.createSpyObj("subsonic", [ + "toggleStar" + ]); + + // Mock the notifications service + notifications = jasmine.createSpyObj("notifications", [ + "updateMessage" + ]); + + inject(function (_$controller_, $rootScope, _$q_, _$document_, _$window_, _$location_, _$cookieStore_, _utils_, globals, _model_, _Page_) { scope = $rootScope.$new(); utils = _utils_; + $q = _$q_; + deferred = $q.defer(); spyOn(utils, "switchTheme"); @@ -47,37 +60,15 @@ describe("Main controller", function() { utils: utils, globals: globals, model: _model_, - notifications: _notifications_, + notifications: notifications, player: player, persistence: persistence, - Page: _Page_ + Page: _Page_, + subsonic: subsonic }; }); }); - xdescribe("updateFavorite -", function() { - - xit("when starring a song, it notifies the user that the star was saved", function() { - - }); - - xit("when starring an album, it notifies the user that the star was saved", function() { - - }); - - xit("when starring an artist, it notifies the user that the star was saved", function() { - - }); - - xit("given that the Subsonic server returns an error, when starring something, it notifies the user with the error message", function() { - //TODO: move to higher level - }); - - xit("given that the Subsonic server is unreachable, when starring something, it notifies the user with the HTTP error code", function() { - //TODO: move to higher level - }); - }); - xdescribe("toggleSetting -", function() { }); @@ -191,6 +182,34 @@ describe("Main controller", function() { }); }); + + describe("toggleStar() -", function() { + beforeEach(function() { + subsonic.toggleStar.and.returnValue(deferred.promise); + }); + + it("Given an artist that was not starred, when I toggle its star, then subsonic service will be called, the artist will be starred and a notification will be displayed", function() { + var artist = { id: 4218, starred: false }; + scope.toggleStar(artist); + deferred.resolve(true); + scope.$apply(); + + expect(subsonic.toggleStar).toHaveBeenCalledWith(artist); + expect(artist.starred).toBeTruthy(); + expect(notifications.updateMessage).toHaveBeenCalledWith('Favorite Updated!', true); + }); + + it("Given a song that was starred, when I toggle its star, then subsonic service will be called, the song will be starred and a notification will be displayed", function() { + var song = { id: 784, starred: true }; + scope.toggleStar(song); + deferred.resolve(false); + scope.$apply(); + + expect(subsonic.toggleStar).toHaveBeenCalledWith(song); + expect(song.starred).toBeFalsy(); + expect(notifications.updateMessage).toHaveBeenCalledWith('Favorite Updated!', true); + }); + }); }); describe("When starting up,", function() { diff --git a/app/common/songs.html b/app/common/songs.html index d93162e..8df712b 100644 --- a/app/common/songs.html +++ b/app/common/songs.html @@ -4,7 +4,7 @@ - +
diff --git a/app/common/songs_lite.html b/app/common/songs_lite.html index 9997004..2509e98 100644 --- a/app/common/songs_lite.html +++ b/app/common/songs_lite.html @@ -1,7 +1,7 @@
  • - +
    diff --git a/app/player/player.html b/app/player/player.html index 6c7d9f3..e9f4231 100644 --- a/app/player/player.html +++ b/app/player/player.html @@ -29,7 +29,7 @@ - + diff --git a/app/player/player.js b/app/player/player.js index e296a71..e2ea87c 100644 --- a/app/player/player.js +++ b/app/player/player.js @@ -8,7 +8,7 @@ angular.module('jamstash.player.controller', ['jamstash.player.service', 'jamstash.player.directive']) .controller('PlayerController', ['$scope', 'player', 'globals', - function($scope, player, globals){ + function ($scope, player, globals) { 'use strict'; $scope.getPlayingSong = player.getPlayingSong; @@ -31,6 +31,4 @@ angular.module('jamstash.player.controller', ['jamstash.player.service', 'jamsta $scope.previousTrack = player.previousTrack; $scope.nextTrack = player.nextTrack; - - //TODO: Hyz: updateFavorite - leave in rootScope ? }]); diff --git a/app/player/player_test.js b/app/player/player_test.js index 7991b5a..31296e6 100644 --- a/app/player/player_test.js +++ b/app/player/player_test.js @@ -35,6 +35,4 @@ describe("Player controller", function() { expect(player.nextTrack).toHaveBeenCalled(); }); - - // TODO: updateFavorite }); diff --git a/app/queue/queue.html b/app/queue/queue.html index 0ee61b1..cea869d 100644 --- a/app/queue/queue.html +++ b/app/queue/queue.html @@ -10,7 +10,7 @@
  • - +
    diff --git a/app/queue/queue.js b/app/queue/queue.js index 4264dd1..b35e09b 100644 --- a/app/queue/queue.js +++ b/app/queue/queue.js @@ -65,6 +65,4 @@ angular.module('jamstash.queue.controller', ['jamstash.player.service']) end = ui.item.index(); player.queue.splice(end, 0, player.queue.splice(start, 1)[0]); }; - - //TODO: Hyz: updateFavorite - leave in rootScope ? }]); diff --git a/app/subsonic/subsonic-service.js b/app/subsonic/subsonic-service.js index 077a232..d1086a6 100644 --- a/app/subsonic/subsonic-service.js +++ b/app/subsonic/subsonic-service.js @@ -5,10 +5,10 @@ * Also offers more fine-grained functionality that is not part of Subsonic's API. */ angular.module('jamstash.subsonic.service', ['angular-underscore/utils', - 'jamstash.settings.service', 'jamstash.utils', 'jamstash.model', 'jamstash.notifications', 'jamstash.player.service']) + 'jamstash.settings.service', 'jamstash.utils', 'jamstash.model']) -.factory('subsonic', ['$rootScope', '$http', '$q', 'globals', 'utils', 'map', 'notifications', 'player', - function ($rootScope, $http, $q, globals, utils, map, notifications, player) { +.factory('subsonic', ['$rootScope', '$http', '$q', 'globals', 'utils', 'map', + function ($rootScope, $http, $q, globals, utils, map) { 'use strict'; //TODO: Hyz: Remove when refactored @@ -587,6 +587,18 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils', return true; }); return promise; + }, + + toggleStar: function (item) { + var partialUrl = (item.starred) ? 'unstar.view' : 'star.view'; + var promise = subsonicService.subsonicRequest(partialUrl, { + params: { + id: item.id + } + }).then(function () { + return !item.starred; + }); + return promise; } // End subsonic }; diff --git a/app/subsonic/subsonic-service_test.js b/app/subsonic/subsonic-service_test.js index a576c3d..6274d62 100644 --- a/app/subsonic/subsonic-service_test.js +++ b/app/subsonic/subsonic-service_test.js @@ -570,6 +570,33 @@ describe("Subsonic service -", function() { expect(promise).toBeResolvedWith(true); }); + + describe("toggleStar() -", function() { + it("Given an item (can be an artist, an album or a song) that wasn't starred, when I toggle its star, then a promise will be resolved with true", function() { + var song = { id: 7748, starred: false }; + var url = 'http://demo.subsonic.com/rest/star.view?' + + 'c=Jamstash&callback=JSON_CALLBACK&f=jsonp'+'&id=7748'+'&p=enc:cGFzc3dvcmQ%3D&u=Hyzual&v=1.10.2'; + mockBackend.expectJSONP(url).respond(JSON.stringify(response)); + + var promise = subsonic.toggleStar(song); + mockBackend.flush(); + + expect(promise).toBeResolvedWith(true); + }); + + it("Given an item (can be an artist, an album or a song) that was starred, when I toggle its star, then a promise will be resolved with false", function() { + var album = { id: 6631, starred: true }; + var url = 'http://demo.subsonic.com/rest/unstar.view?' + + 'c=Jamstash&callback=JSON_CALLBACK&f=jsonp'+'&id=6631'+'&p=enc:cGFzc3dvcmQ%3D&u=Hyzual&v=1.10.2'; + mockBackend.expectJSONP(url).respond(JSON.stringify(response)); + + var promise = subsonic.toggleStar(album); + mockBackend.flush(); + + expect(promise).toBeResolvedWith(false); + }); + }); + describe("getArtists() -", function() { beforeEach(function() { url = 'http://demo.subsonic.com/rest/getIndexes.view?'+ diff --git a/app/subsonic/subsonic.html b/app/subsonic/subsonic.html index d42dd54..329bfef 100644 --- a/app/subsonic/subsonic.html +++ b/app/subsonic/subsonic.html @@ -54,7 +54,7 @@ - +