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