Jamstash/app/common/persistence-service.js
Hyzual 2e004a5f59 Adds deleting the queue and song from localStorage.
Adds a watch on the saveTrackPosition setting to start dynamically saving the position.
2015-01-17 19:15:07 +01:00

58 lines
2.1 KiB
JavaScript

'use strict';
/**
* jamstash.persistence Module
*
* Provides load, save and delete operations for the current song and queue.
* Data storage provided by HTML5 localStorage.
*/
angular.module('jamstash.persistence', ['jamstash.settings', 'jamstash.player.service', 'jamstash.notifications', 'angular-locker'])
.config(['lockerProvider', function (lockerProvider) {
lockerProvider.setDefaultDriver('local')
.setDefaultNamespace('jamstash')
.setEventsEnabled(false);
}])
.service('persistence', ['globals', 'player', 'notifications', 'locker', function(globals, player, notifications, locker){
this.loadTrackPosition = function () {
// Load Saved Song
var song = locker.get('CurrentSong');
if (song) {
player.load(song);
}
if (globals.settings.Debug) { console.log('Current Position Loaded from localStorage: ', song); }
};
this.saveTrackPosition = function (song) {
locker.put('CurrentSong', song);
if (globals.settings.Debug) { console.log('Saving Current Position: ', song); }
};
this.deleteTrackPosition = function () {
locker.forget('CurrentSong');
if (globals.settings.Debug) { console.log('Removing Current Position from localStorage'); }
};
this.loadQueue = function () {
// load Saved queue
var queue = locker.get('CurrentQueue');
if (queue) {
player.addSongs(queue);
if (player.queue.length > 0) {
notifications.updateMessage(player.queue.length + ' Saved Song(s)', true);
}
if (globals.settings.Debug) { console.log('Play Queue Loaded from localStorage: ' + player.queue.length + ' song(s)'); }
}
};
this.saveQueue = function () {
locker.put('CurrentQueue', player.queue);
if (globals.settings.Debug) { console.log('Saving Queue: ' + player.queue.length + ' songs'); }
};
this.deleteQueue = function () {
locker.forget('CurrentQueue');
if (globals.settings.Debug) { console.log('Removing Play Queue from localStorage'); }
};
}]);