2015-12-05 14:57:43 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const Launcher = require('./launcher');
|
|
|
|
const StringUtil = require('../util/string-util');
|
|
|
|
const Logger = require('../util/logger');
|
2015-12-12 09:53:50 +01:00
|
|
|
|
2017-01-31 07:50:28 +01:00
|
|
|
const logger = new Logger('settings');
|
2015-12-05 14:57:43 +01:00
|
|
|
|
2017-02-03 00:48:20 +01:00
|
|
|
// TODO async
|
2017-01-31 07:50:28 +01:00
|
|
|
const SettingsStore = {
|
2017-02-01 02:37:43 +01:00
|
|
|
|
|
|
|
useFileStore: function() {
|
|
|
|
return Launcher && Launcher.platform() !== 'android';
|
|
|
|
},
|
|
|
|
|
2015-12-05 14:57:43 +01:00
|
|
|
fileName: function(key) {
|
|
|
|
return key + '.json';
|
|
|
|
},
|
|
|
|
|
|
|
|
load: function(key) {
|
|
|
|
try {
|
2017-02-01 02:37:43 +01:00
|
|
|
if (this.useFileStore()) {
|
2017-01-31 07:50:28 +01:00
|
|
|
const settingsFile = Launcher.getUserDataPath(this.fileName(key));
|
2015-12-05 14:57:43 +01:00
|
|
|
if (Launcher.fileExists(settingsFile)) {
|
2017-02-01 02:37:43 +01:00
|
|
|
return JSON.parse(Launcher.readFile(settingsFile));
|
2015-12-05 14:57:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-31 07:50:28 +01:00
|
|
|
const data = localStorage[StringUtil.camelCase(key)];
|
2015-12-06 21:32:41 +01:00
|
|
|
return data ? JSON.parse(data) : undefined;
|
2015-12-05 14:57:43 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.error('Error loading ' + key, e);
|
2015-12-05 14:57:43 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
save: function(key, data) {
|
|
|
|
try {
|
2017-02-01 02:37:43 +01:00
|
|
|
if (this.useFileStore()) {
|
2015-12-05 14:57:43 +01:00
|
|
|
Launcher.writeFile(Launcher.getUserDataPath(this.fileName(key)), JSON.stringify(data));
|
|
|
|
} else if (typeof localStorage !== 'undefined') {
|
|
|
|
localStorage[StringUtil.camelCase(key)] = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2015-12-12 09:53:50 +01:00
|
|
|
logger.error('Error saving ' + key, e);
|
2015-12-05 14:57:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SettingsStore;
|