diff options
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r-- | src/stores/SettingsStore.js | 81 |
1 files changed, 67 insertions, 14 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index f1b067115..a5c2c8b52 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js | |||
@@ -1,4 +1,4 @@ | |||
1 | import { remote } from 'electron'; | 1 | import { remote, ipcRenderer } from 'electron'; |
2 | import { action, computed, observable } from 'mobx'; | 2 | import { action, computed, observable } from 'mobx'; |
3 | import localStorage from 'mobx-localstorage'; | 3 | import localStorage from 'mobx-localstorage'; |
4 | 4 | ||
@@ -7,6 +7,8 @@ import SettingsModel from '../models/Settings'; | |||
7 | import Request from './lib/Request'; | 7 | import Request from './lib/Request'; |
8 | import CachedRequest from './lib/CachedRequest'; | 8 | import CachedRequest from './lib/CachedRequest'; |
9 | 9 | ||
10 | import { DEFAULT_APP_SETTINGS, FILE_SYSTEM_SETTINGS_TYPES } from '../config'; | ||
11 | |||
10 | const { systemPreferences } = remote; | 12 | const { systemPreferences } = remote; |
11 | const debug = require('debug')('Franz:SettingsStore'); | 13 | const debug = require('debug')('Franz:SettingsStore'); |
12 | 14 | ||
@@ -14,12 +16,35 @@ export default class SettingsStore extends Store { | |||
14 | @observable appSettingsRequest = new CachedRequest(this.api.local, 'getAppSettings'); | 16 | @observable appSettingsRequest = new CachedRequest(this.api.local, 'getAppSettings'); |
15 | @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings'); | 17 | @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings'); |
16 | 18 | ||
19 | @observable fileSystemSettingsRequests = []; | ||
20 | |||
21 | fileSystemSettingsTypes = FILE_SYSTEM_SETTINGS_TYPES; | ||
22 | @observable _fileSystemSettingsCache = { | ||
23 | app: DEFAULT_APP_SETTINGS, | ||
24 | proxy: {}, | ||
25 | }; | ||
26 | |||
17 | constructor(...args) { | 27 | constructor(...args) { |
18 | super(...args); | 28 | super(...args); |
19 | 29 | ||
20 | // Register action handlers | 30 | // Register action handlers |
21 | this.actions.settings.update.listen(this._update.bind(this)); | 31 | this.actions.settings.update.listen(this._update.bind(this)); |
22 | this.actions.settings.remove.listen(this._remove.bind(this)); | 32 | this.actions.settings.remove.listen(this._remove.bind(this)); |
33 | |||
34 | this.fileSystemSettingsTypes.forEach((type) => { | ||
35 | this.fileSystemSettingsRequests[type] = new CachedRequest(this.api.local, 'getAppSettings'); | ||
36 | }); | ||
37 | |||
38 | ipcRenderer.on('appSettings', (event, resp) => { | ||
39 | debug('Get appSettings resolves', resp, resp.type, resp.data); | ||
40 | |||
41 | this._fileSystemSettingsCache[resp.type] = resp.data; | ||
42 | }); | ||
43 | |||
44 | this.fileSystemSettingsTypes.forEach((type) => { | ||
45 | console.log(type); | ||
46 | ipcRenderer.send('getAppSettings', type); | ||
47 | }); | ||
23 | } | 48 | } |
24 | 49 | ||
25 | async setup() { | 50 | async setup() { |
@@ -28,29 +53,53 @@ export default class SettingsStore extends Store { | |||
28 | await this._migrate(); | 53 | await this._migrate(); |
29 | } | 54 | } |
30 | 55 | ||
56 | @computed get app() { | ||
57 | return this._fileSystemSettingsCache.app || DEFAULT_APP_SETTINGS; | ||
58 | } | ||
59 | |||
60 | @computed get proxy() { | ||
61 | return this._fileSystemSettingsCache.proxy || {}; | ||
62 | } | ||
63 | |||
64 | @computed get service() { | ||
65 | return localStorage.getItem('service') || { | ||
66 | activeService: '', | ||
67 | }; | ||
68 | } | ||
69 | |||
70 | @computed get stats() { | ||
71 | return localStorage.getItem('stats') || { | ||
72 | activeService: '', | ||
73 | }; | ||
74 | } | ||
75 | |||
76 | @computed get migration() { | ||
77 | return localStorage.getItem('migration') || {}; | ||
78 | } | ||
79 | |||
31 | @computed get all() { | 80 | @computed get all() { |
32 | return new SettingsModel({ | 81 | return { |
33 | app: this.appSettingsRequest.execute().result || {}, | 82 | app: this.app, |
34 | service: localStorage.getItem('service') || {}, | 83 | proxy: this.proxy, |
35 | group: localStorage.getItem('group') || {}, | 84 | service: this.service, |
36 | stats: localStorage.getItem('stats') || {}, | 85 | stats: this.stats, |
37 | migration: localStorage.getItem('migration') || {}, | 86 | migration: this.migration, |
38 | }); | 87 | }; |
39 | } | 88 | } |
40 | 89 | ||
41 | @action async _update({ type, data }) { | 90 | @action async _update({ type, data }) { |
42 | const appSettings = this.all; | 91 | const appSettings = this.all; |
43 | if (type !== 'app') { | 92 | if (!this.fileSystemSettingsTypes.includes(type)) { |
44 | debug('Update settings', type, data, this.all); | 93 | debug('Update settings', type, data, this.all); |
45 | localStorage.setItem(type, Object.assign(appSettings[type], data)); | 94 | localStorage.setItem(type, Object.assign(appSettings[type], data)); |
46 | } else { | 95 | } else { |
47 | debug('Update settings on file system', type, data); | 96 | debug('Update settings on file system', type, data); |
48 | this.updateAppSettingsRequest.execute(data); | 97 | ipcRenderer.send('updateAppSettings', { |
49 | 98 | type, | |
50 | this.appSettingsRequest.patch((result) => { | 99 | data, |
51 | if (!result) return; | ||
52 | Object.assign(result, data); | ||
53 | }); | 100 | }); |
101 | |||
102 | Object.assign(this._fileSystemSettingsCache[type], data); | ||
54 | } | 103 | } |
55 | } | 104 | } |
56 | 105 | ||
@@ -128,4 +177,8 @@ export default class SettingsStore extends Store { | |||
128 | debug('Set up dark mode'); | 177 | debug('Set up dark mode'); |
129 | } | 178 | } |
130 | } | 179 | } |
180 | |||
181 | _getFileBasedSettings(type) { | ||
182 | ipcRenderer.send('getAppSettings', type); | ||
183 | } | ||
131 | } | 184 | } |