Refactor Jukebox control

- Moves the functions to make the jukebox-related API calls to the
  subsonic service
- Initiate the API calls from the player directive to centralize the
  logic.
This commit is contained in:
Carey Metcalfe 2017-11-10 11:57:38 -05:00
parent 8c279093df
commit ee7d11d8d7
5 changed files with 55 additions and 53 deletions

View file

@ -258,35 +258,6 @@ angular.module('JamStash')
});
};
$rootScope.addToJukebox = function (id) {
if (globals.settings.Debug) { console.log("LOAD JUKEBOX"); }
$.ajax({
url: globals.BaseURL() + '/jukeboxControl.view?' + globals.BaseParams() + '&action=set&id=' + id,
method: 'GET',
dataType: globals.settings.Protocol,
timeout: globals.settings.Timeout,
success: function (data) {
/*
if (data["subsonic-response"].podcasts.channel !== undefined) {
}
deferred.resolve(podcasts);
*/
$.get(globals.BaseURL() + '/jukeboxControl.view?' + globals.BaseParams() + '&action=start');
}
});
};
$rootScope.sendToJukebox = function (action) {
if (globals.settings.Debug) { console.log("SEND JUKEBOX " + action); }
$.ajax({
url: globals.BaseURL() + '/jukeboxControl.view?' + globals.BaseParams() + '&action=' + action,
method: 'GET',
dataType: globals.settings.Protocol,
timeout: globals.settings.Timeout,
success: function (data) {
}
});
};
$scope.toggleStar = function (item) {
subsonic.toggleStar(item).then(function (newStarred) {
item.starred = newStarred;

View file

@ -123,7 +123,7 @@ angular.module('jamstash.player.directive', ['jamstash.player.service', 'jamstas
$player.jPlayer('setMedia', media);
if (globals.settings.Jukebox) {
$player.jPlayer('mute', true);
scope.addToJukebox(newSong.id);
subsonic.addToJukebox(newSong);
}
if (playerService.loadSong === true || globals.settings.Jukebox) {
// Do not play, only load
@ -152,10 +152,9 @@ angular.module('jamstash.player.directive', ['jamstash.player.service', 'jamstas
scope.$watch(function () {
return playerService.pauseSong;
}, function (newVal) {
if(newVal === true) {
$player.jPlayer('pause');
} else {
$player.jPlayer('play');
$player.jPlayer(newVal ? 'pause' : 'play');
if(globals.settings.Jukebox){
subsonic.sendToJukebox(newVal ? 'stop' : 'start');
}
});

View file

@ -76,11 +76,12 @@ describe("jplayer directive", function () {
it("if the global setting Jukebox is true, it mutes jPlayer and adds the song to subsonic's Jukebox", function () {
mockGlobals.settings.Jukebox = true;
scope.addToJukebox = jasmine.createSpy("addToJukebox");
spyOn(subsonic, "addToJukebox")
scope.$apply();
expect($player.jPlayer).toHaveBeenCalledWith('mute', true);
expect(scope.addToJukebox).toHaveBeenCalledWith(playingSong.id);
expect(subsonic.addToJukebox).toHaveBeenCalledWith(
jasmine.objectContaining({ id: playingSong.id}))
});
it("if the player service's loadSong flag is true, it does not play the song, it displays the player controls and sets the player to the song's supplied position", function () {
@ -142,6 +143,7 @@ describe("jplayer directive", function () {
describe("", function () {
beforeEach(function () {
$.fn.jPlayer.and.stub();
spyOn(subsonic, "sendToJukebox")
});
it("When the player service's restartSong flag is true, it restarts the current song, resets the restart flag to false and resets the scrobbled flag to false", function () {
@ -170,6 +172,25 @@ describe("jplayer directive", function () {
expect($player.jPlayer).toHaveBeenCalledWith('play');
});
it("When the player service's pauseSong is true and jukebox is enabled, 'stop' is sent to the jukebox", function () {
mockGlobals.settings.Jukebox = true;
playerService.pauseSong = true;
scope.$apply();
expect(subsonic.sendToJukebox).toHaveBeenCalledWith('stop');
});
it("Given that the current song is paused and jukebox is enabled, 'start' is sent to the jukebox when it's unpaused", function () {
mockGlobals.settings.Jukebox = true;
playerService.pauseSong = true;
scope.$apply();
playerService.pauseSong = false;
scope.$apply();
expect(subsonic.sendToJukebox).toHaveBeenCalledWith('start');
});
it("When the player service's volume changes, it sets jPlayer's volume", function () {
playerService.getVolume.and.returnValue(0.2034);
scope.$apply();

View file

@ -15,22 +15,8 @@ angular.module('jamstash.player.controller', ['jamstash.player.service', 'jamsta
$scope.settings = globals.settings;
$scope.playerSettings = player.settings;
$scope.play = function () {
if (globals.settings.Jukebox) {
$scope.sendToJukebox('start');
} else {
player.togglePause();
}
};
$scope.pause = function () {
if (globals.settings.Jukebox) {
$scope.sendToJukebox('stop');
} else {
player.togglePause();
}
};
$scope.play = player.togglePause;
$scope.pause = player.togglePause;
$scope.previousTrack = player.previousTrack;
$scope.nextTrack = player.nextTrack;
}]);

View file

@ -53,7 +53,9 @@ function subsonicService(
scrobble : scrobble,
search : search,
subsonicRequest : subsonicRequest,
toggleStar : toggleStar
toggleStar : toggleStar,
addToJukebox : addToJukebox,
sendToJukebox : sendToJukebox
});
// TODO: Hyz: Remove when refactored
@ -570,4 +572,27 @@ function subsonicService(
});
return promise;
}
function addToJukebox(song) {
if (globals.settings.Debug) { console.log("Load Jukebox"); }
var promise = self.subsonicRequest('jukeboxControl.view', {
params: {
action: 'set',
id: song.id
}
}).then(function () {
self.sendToJukebox('start');
});
return promise;
}
function sendToJukebox(action) {
if (globals.settings.Debug) { console.log("Send Jukebox " + action); }
var promise = self.subsonicRequest('jukeboxControl.view', {
params: {
action: action
}
})
return promise;
}
}