2016-04-05 09:19:20 +00:00
|
|
|
import { extend, has, each } from 'lodash';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
import userStore from './user';
|
|
|
|
import ls from '../services/ls';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
storeKey: '',
|
|
|
|
|
|
|
|
state: {
|
|
|
|
volume: 7,
|
|
|
|
notify: true,
|
|
|
|
repeatMode: 'NO_REPEAT',
|
|
|
|
showExtraPanel: true,
|
2016-01-12 15:39:50 +00:00
|
|
|
confirmClosing: false,
|
2016-01-19 11:00:23 +00:00
|
|
|
equalizer: {
|
|
|
|
preamp: 0,
|
|
|
|
gains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
2016-03-27 06:13:17 +00:00
|
|
|
},
|
|
|
|
artistsViewMode: null,
|
|
|
|
albumsViewMode: null,
|
2016-04-13 06:00:38 +00:00
|
|
|
selectedPreset: -1,
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-22 17:46:54 +00:00
|
|
|
* Init the store.
|
2016-03-13 17:00:32 +00:00
|
|
|
*
|
2015-12-22 17:46:54 +00:00
|
|
|
* @param {Object} user The user whose preferences we are managing.
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
init(user = null) {
|
|
|
|
if (!user) {
|
2016-03-18 04:45:12 +00:00
|
|
|
user = userStore.current;
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.storeKey = `preferences_${user.id}`;
|
2016-03-31 07:44:36 +00:00
|
|
|
extend(this.state, ls.get(this.storeKey, this.state));
|
2016-04-05 09:19:20 +00:00
|
|
|
this.setupProxy();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy the state properties, so that each can be directly accessed using the key.
|
|
|
|
*/
|
|
|
|
setupProxy() {
|
|
|
|
each(Object.keys(this.state), key => {
|
|
|
|
Object.defineProperty(this, key, {
|
|
|
|
get: () => this.state[key],
|
|
|
|
set: (value) => {
|
|
|
|
this.state[key] = value;
|
|
|
|
this.save();
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
});
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set(key, val) {
|
|
|
|
this.state[key] = val;
|
|
|
|
this.save();
|
|
|
|
},
|
|
|
|
|
|
|
|
get(key) {
|
2016-03-31 07:44:36 +00:00
|
|
|
return has(this.state, key) ? this.state[key] : null;
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
|
|
|
ls.set(this.storeKey, this.state);
|
|
|
|
},
|
|
|
|
};
|