diff options
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r-- | src/stores/SettingsStore.js | 98 |
1 files changed, 78 insertions, 20 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index b7d803398..d8519c609 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 | ||
@@ -15,36 +20,89 @@ export default class SettingsStore extends Store { | |||
15 | this.actions.settings.remove.listen(this._remove.bind(this)); | 20 | this.actions.settings.remove.listen(this._remove.bind(this)); |
16 | } | 21 | } |
17 | 22 | ||
18 | setup() { | 23 | async setup() { |
19 | this._shareSettingsWithMainProcess(); | 24 | // We need to wait until `appSettingsRequest` has been executed once, otherwise we can't patch the result. If we don't wait we'd run into an issue with mobx not reacting to changes of previously not existing keys |
25 | await this.appSettingsRequest._promise; | ||
26 | this._migrate(); | ||
20 | } | 27 | } |
21 | 28 | ||
22 | @computed get all() { | 29 | @computed get all() { |
23 | return new SettingsModel(localStorage.getItem('app') || {}); | 30 | return new SettingsModel({ |
31 | app: this.appSettingsRequest.execute().result || {}, | ||
32 | service: localStorage.getItem('service') || {}, | ||
33 | group: localStorage.getItem('group') || {}, | ||
34 | stats: localStorage.getItem('stats') || {}, | ||
35 | migration: localStorage.getItem('migration') || {}, | ||
36 | }); | ||
24 | } | 37 | } |
25 | 38 | ||
26 | @action async _update({ settings }) { | 39 | @action async _update({ type, data }) { |
27 | const appSettings = this.all; | 40 | const appSettings = this.all; |
28 | localStorage.setItem('app', Object.assign(appSettings, settings)); | 41 | if (type !== 'app') { |
42 | debug('Update settings', type, data, this.all); | ||
43 | localStorage.setItem(type, Object.assign(appSettings[type], data)); | ||
44 | } else { | ||
45 | debug('Update settings on file system', type, data); | ||
46 | this.updateAppSettingsRequest.execute(data); | ||
29 | 47 | ||
30 | // We need a little hack to wait until everything is patched | 48 | this.appSettingsRequest.patch((result) => { |
31 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); | 49 | if (!result) return; |
32 | 50 | Object.assign(result, data); | |
33 | gaEvent('Settings', 'update'); | 51 | }); |
52 | } | ||
34 | } | 53 | } |
35 | 54 | ||
36 | @action async _remove({ key }) { | 55 | @action async _remove({ type, key }) { |
37 | const appSettings = this.all; | 56 | if (type === 'app') return; // app keys can't be deleted |
57 | |||
58 | const appSettings = this.all[type]; | ||
38 | if (Object.hasOwnProperty.call(appSettings, key)) { | 59 | if (Object.hasOwnProperty.call(appSettings, key)) { |
39 | delete appSettings[key]; | 60 | delete appSettings[key]; |
40 | localStorage.setItem('app', appSettings); | ||
41 | } | ||
42 | 61 | ||
43 | this._shareSettingsWithMainProcess(); | 62 | this.actions.settings.update({ |
63 | type, | ||
64 | data: appSettings, | ||
65 | }); | ||
66 | } | ||
44 | } | 67 | } |
45 | 68 | ||
46 | // Reactions | 69 | // Helper |
47 | _shareSettingsWithMainProcess() { | 70 | _migrate() { |
48 | ipcRenderer.send('settings', this.all); | 71 | const legacySettings = localStorage.getItem('app'); |
72 | |||
73 | if (!this.all.migration['5.0.0-beta.17-settings']) { | ||
74 | this.actions.settings.update({ | ||
75 | type: 'app', | ||
76 | data: { | ||
77 | autoLaunchInBackground: legacySettings.autoLaunchInBackground, | ||
78 | runInBackground: legacySettings.runInBackground, | ||
79 | enableSystemTray: legacySettings.enableSystemTray, | ||
80 | minimizeToSystemTray: legacySettings.minimizeToSystemTray, | ||
81 | isAppMuted: legacySettings.isAppMuted, | ||
82 | enableGPUAcceleration: legacySettings.enableGPUAcceleration, | ||
83 | showMessageBadgeWhenMuted: legacySettings.showMessageBadgeWhenMuted, | ||
84 | showDisabledServices: legacySettings.showDisabledServices, | ||
85 | enableSpellchecking: legacySettings.enableSpellchecking, | ||
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 | } |