Skip to content

Commit

Permalink
front-end music controller & service
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Nov 12, 2016
1 parent 4549a55 commit 8a18be6
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
130 changes: 130 additions & 0 deletions assets/js/app/music/music.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Gladys Project
* http://gladysproject.com
* Software under licence Creative Commons 3.0 France
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/
* You may not use this software for commercial purposes.
* @author :: Pierre-Gilles Leymarie
*/

(function () {
'use strict';

angular
.module('gladys')
.controller('MusicCtrl', MusicCtrl);

MusicCtrl.$inject = ['musicService', 'roomService'];

function MusicCtrl(musicService, roomService) {
/* jshint validthis: true */
var vm = this;
vm.roomId = null;
vm.displayAskRoomForm = false;

vm.selectRoomId = selectRoomId;

vm.play = play;
vm.pause = pause;
vm.stop = stop;
vm.previous = previous;
vm.next = next;
vm.getCurrentTrack = getCurrentTrack;
vm.getQueue = getQueue;

activate();

function activate(){
getRoomId();
if(vm.displayAskRoomForm) getRooms();
else refreshMusicData();
}


function getRoomId() {
vm.roomId = musicService.getSavedRoomBox(vm.boxId);
if(vm.roomId) vm.displayAskRoomForm = false;
else vm.displayAskRoomForm = true;
}

function getRooms(){
roomService.get()
.then(function(data){
vm.rooms = data.data;
});
}

function selectRoomId(roomId){
musicService.saveRoomBox(vm.boxId, roomId);
vm.displayAskRoomForm = false;
vm.roomId = roomId;
refreshMusicData();
}

function refreshMusicData(){
getPlaying();
getCurrentTrack();
getQueue();
}

function play(){
return musicService.play({room: vm.roomId})
.then(function(){
vm.playing = true;
});
}

function pause(){
return musicService.pause({room: vm.roomId})
.then(function(){
vm.playing = false;
});
}

function stop(){
return musicService.stop({room: vm.roomId})
.then(function(){
vm.playing = false;
refreshMusicData();
});
}

function previous(){
return musicService.previous({room: vm.roomId})
.then(function(){
getCurrentTrack();
getQueue();
});
}

function next(){
return musicService.next({room: vm.roomId})
.then(function(){
getCurrentTrack();
getQueue();
});
}

function getPlaying(){
return musicService.getPlaying({room: vm.roomId})
.then(function(data){
vm.playing = data.data.playing;
});
}

function getCurrentTrack(){
return musicService.getCurrentTrack({room: vm.roomId})
.then(function(data){
vm.currentTrack = data.data;
});
}

function getQueue() {
return musicService.getQueue({room: vm.roomId})
.then(function(data){
vm.queue = data.data;
});
}

}
})();
80 changes: 80 additions & 0 deletions assets/js/app/music/music.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Gladys Project
* http://gladysproject.com
* Software under licence Creative Commons 3.0 France
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/
* You may not use this software for commercial purposes.
* @author :: Pierre-Gilles Leymarie
*/

(function () {
'use strict';

angular
.module('gladys')
.factory('musicService', musicService);

musicService.$inject = ['$http', 'cacheService'];

function musicService($http, cacheService) {

var service = {
play: play,
pause: pause,
stop: stop,
previous: previous,
next: next,
getPlaying: getPlaying,
getCurrentTrack: getCurrentTrack,
getQueue: getQueue,
getSavedRoomBox: getSavedRoomBox,
saveRoomBox: saveRoomBox
};

var EXPIRATION = 10*365*24*3600*1000;

return service;

function play(params) {
return $http({method: 'POST', url: '/music/play', data: params});
}

function pause(params) {
return $http({method: 'POST', url: '/music/pause', data: params});
}

function stop(params) {
return $http({method: 'POST', url: '/music/stop', data: params});
}

function previous(params) {
return $http({method: 'POST', url: '/music/previous', data: params});
}

function next(params) {
return $http({method: 'POST', url: '/music/next', data: params});
}

function getPlaying(params) {
return $http({method: 'GET', url: '/music/playing', params: params});
}

function getCurrentTrack(params) {
return $http({method: 'GET', url: '/music/currenttrack', params: params});
}

function getQueue(params) {
return $http({method: 'GET', url: '/music/queue', params: params});
}

// get RoomId from localStorage
function getSavedRoomBox(boxId){
return cacheService.get('MUSIC_BOX_ID_' + boxId);
}

// saveRoomId of this box to localStorage
function saveRoomBox(boxId, roomId){
return cacheService.set('MUSIC_BOX_ID_' + boxId, roomId, EXPIRATION);
}
}
})();

0 comments on commit 8a18be6

Please sign in to comment.