diff options
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r-- | src/stores/SettingsStore.js | 96 |
1 files changed, 77 insertions, 19 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index b7d803398..82fc2a279 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js | |||
@@ -1,12 +1,17 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { action, computed, observable } from 'mobx'; |
2 | import { action, computed } from 'mobx'; | ||
3 | import localStorage from 'mobx-localstorage'; | 2 | import localStorage from 'mobx-localstorage'; |
4 | 3 | ||
5 | import Store from './lib/Store'; | 4 | import Store from './lib/Store'; |
6 | import { gaEvent } from '../lib/analytics'; | ||
7 | import SettingsModel from '../models/Settings'; | 5 | import SettingsModel from '../models/Settings'; |
6 | import Request from './lib/Request'; | ||
7 | import CachedRequest from './lib/CachedRequest'; | ||
8 | |||
9 | const debug = require('debug')('SettingsStore'); | ||
8 | 10 | ||
9 | export default class SettingsStore extends Store { | 11 | export default class SettingsStore extends Store { |
12 | @observable appSettingsRequest = new CachedRequest(this.api.local, 'getAppSettings'); | ||
13 | @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings'); | ||
14 | |||
10 | constructor(...args) { | 15 | constructor(...args) { |
11 | super(...args); | 16 | super(...args); |
12 | 17 | ||
@@ -16,35 +21,88 @@ export default class SettingsStore extends Store { | |||
16 | } | 21 | } |
17 | 22 | ||
18 | setup() { | 23 | setup() { |
19 | this._shareSettingsWithMainProcess(); | 24 | this._migrate(); |
20 | } | 25 | } |
21 | 26 | ||
22 | @computed get all() { | 27 | @computed get all() { |
23 | return new SettingsModel(localStorage.getItem('app') || {}); | 28 | return new SettingsModel({ |
29 | app: this.appSettingsRequest.execute().result || {}, | ||
30 | service: localStorage.getItem('service') || {}, | ||
31 | group: localStorage.getItem('group') || {}, | ||
32 | stats: localStorage.getItem('stats') || {}, | ||
33 | migration: localStorage.getItem('migration') || {}, | ||
34 | }); | ||
24 | } | 35 | } |
25 | 36 | ||
26 | @action async _update({ settings }) { | 37 | @action async _update({ type, data }) { |
27 | const appSettings = this.all; | 38 | const appSettings = this.all; |
28 | localStorage.setItem('app', Object.assign(appSettings, settings)); | 39 | if (type !== 'app') { |
40 | debug('Update settings', type, data, this.all); | ||
41 | localStorage.setItem(type, Object.assign(appSettings[type], data)); | ||
42 | } else { | ||
43 | debug('Update settings on file system', type, data); | ||
44 | this.updateAppSettingsRequest.execute(data); | ||
29 | 45 | ||
30 | // We need a little hack to wait until everything is patched | 46 | this.appSettingsRequest.patch((result) => { |
31 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); | 47 | if (!result) return; |
32 | 48 | Object.assign(result, data); | |
33 | gaEvent('Settings', 'update'); | 49 | }); |
50 | } | ||
34 | } | 51 | } |
35 | 52 | ||
36 | @action async _remove({ key }) { | 53 | @action async _remove({ type, key }) { |
37 | const appSettings = this.all; | 54 | if (type === 'app') return; // app keys can't be deleted |
55 | |||
56 | const appSettings = this.all[type]; | ||
38 | if (Object.hasOwnProperty.call(appSettings, key)) { | 57 | if (Object.hasOwnProperty.call(appSettings, key)) { |
39 | delete appSettings[key]; | 58 | delete appSettings[key]; |
40 | localStorage.setItem('app', appSettings); | ||
41 | } | ||
42 | 59 | ||
43 | this._shareSettingsWithMainProcess(); | 60 | this.actions.settings.update({ |
61 | type, | ||
62 | data: appSettings, | ||
63 | }); | ||
64 | } | ||
44 | } | 65 | } |
45 | 66 | ||
46 | // Reactions | 67 | // Helper |
47 | _shareSettingsWithMainProcess() { | 68 | _migrate() { |
48 | ipcRenderer.send('settings', this.all); | 69 | const legacySettings = localStorage.getItem('app'); |
70 | |||
71 | if (!this.all.migration['5.0.0-beta.17-settings']) { | ||
72 | this.actions.settings.update({ | ||
73 | type: 'app', | ||
74 | data: { | ||
75 | autoLaunchInBackground: legacySettings.autoLaunchInBackground, | ||
76 | runInBackground: legacySettings.runInBackground, | ||
77 | enableSystemTray: legacySettings.enableSystemTray, | ||
78 | minimizeToSystemTray: legacySettings.minimizeToSystemTray, | ||
79 | isAppMuted: legacySettings.isAppMuted, | ||
80 | enableGPUAcceleration: legacySettings.enableGPUAcceleration, | ||
81 | showMessageBadgeWhenMuted: legacySettings.showMessageBadgeWhenMuted, | ||
82 | showDisabledServices: legacySettings.showDisabledServices, | ||
83 | enableSpellchecking: legacySettings.enableSpellchecking, | ||
84 | locale: legacySettings.locale, | ||
85 | beta: legacySettings.beta, | ||
86 | }, | ||
87 | }); | ||
88 | |||
89 | this.actions.settings.update({ | ||
90 | type: 'service', | ||
91 | data: { | ||
92 | activeService: legacySettings.activeService, | ||
93 | }, | ||
94 | }); | ||
95 | |||
96 | this.actions.settings.update({ | ||
97 | type: 'migration', | ||
98 | data: { | ||
99 | '5.0.0-beta.17-settings': true, | ||
100 | }, | ||
101 | }); | ||
102 | |||
103 | localStorage.removeItem('app'); | ||
104 | |||
105 | debug('Migrated settings to split stores'); | ||
106 | } | ||
49 | } | 107 | } |
50 | } | 108 | } |