Jamstash/app/common/persistence-service.js
Hyzual ae703c1542 Refines the changesets mechanism.
- Adds an angular "value" that will contain an array of objects with version and change to be applied to the settings.
- Adds the possibility to run multiple changesets (e.g. upgrade from v1.0.0 to v1.0.2).
- Adds todos in the utils service for things related to version comparisons.
2015-03-24 18:36:54 +01:00

126 lines
4.2 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', ['angular-locker',
'jamstash.settings.service', 'jamstash.player.service', 'jamstash.notifications', 'jamstash.utils'])
.config(['lockerProvider', function (lockerProvider) {
lockerProvider.setDefaultDriver('local')
.setDefaultNamespace(false)
.setEventsEnabled(false);
}])
.service('persistence', ['globals', 'player', 'notifications', 'locker', 'jamstashVersion', 'jamstashVersionChangesets', 'utils',
function (globals, player, notifications, locker, jamstashVersion, jamstashVersionChangesets, utils) {
/* Manage current track */
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'); }
};
/* Manage playing queue */
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'); }
};
/* Manage player volume */
this.getVolume = function () {
return locker.get('Volume');
};
this.saveVolume = function (volume) {
locker.put('Volume', volume);
};
this.deleteVolume = function () {
locker.forget('Volume');
};
/* Manage user settings */
this.getSettings = function () {
//TODO: store as version object
if(this.getVersion() !== jamstashVersion) {
this.upgradeToVersion(jamstashVersion);
}
return locker.get('Settings');
};
this.saveSettings = function (settings) {
locker.put('Settings', settings);
};
this.deleteSettings = function () {
locker.forget('Settings');
};
/* Manage Jamstash Version */
this.getVersion = function () {
return locker.get('version');
};
this.upgradeToVersion = function (finalVersion) {
var currentVersion = utils.parseVersionString(this.getVersion());
var settings = locker.get('Settings');
// Apply all upgrades older than the final version
// TODO: Hyz: Do not apply upgrades already applied (start from current version)
var allUpgrades = _(jamstashVersionChangesets.versions).filter(function (toApply) {
//TODO: Hyz: have "checkVersion" do the conversion themselves
var versionToCheck = utils.parseVersionString(toApply.version);
var objFinalVersion = utils.parseVersionString(finalVersion);
return utils.checkVersion(objFinalVersion, versionToCheck);
});
_(allUpgrades).each(function (versionUpg) {
versionUpg.changeset(settings);
});
this.saveSettings(settings);
locker.put('version', finalVersion);
};
}])
.value('jamstashVersionChangesets', {
versions: [
{
version: '4.4.5',
changeset: function (settings) {
settings.DefaultSearchType = 0;
}
}
]
});