diff options
author | Stefan Malzner <stefan@adlk.io> | 2018-11-22 14:14:25 +0100 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2018-11-22 14:14:25 +0100 |
commit | 46b8c8c4b3a5b80e0187b284abc84566a7e784db (patch) | |
tree | 7fd378bcdd18e78c42dfeb61a15f89fd10106046 /src/stores/SettingsStore.js | |
parent | ADD features loading spinner (diff) | |
parent | feat(App): Add option to enable dark mode for supported services (diff) | |
download | ferdium-app-46b8c8c4b3a5b80e0187b284abc84566a7e784db.tar.gz ferdium-app-46b8c8c4b3a5b80e0187b284abc84566a7e784db.tar.zst ferdium-app-46b8c8c4b3a5b80e0187b284abc84566a7e784db.zip |
Merge branch 'develop' into feature/features-api
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r-- | src/stores/SettingsStore.js | 121 |
1 files changed, 101 insertions, 20 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index b7d803398..f1b067115 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js | |||
@@ -1,12 +1,19 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { remote } from 'electron'; |
2 | import { action, computed } 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 { gaEvent } from '../lib/analytics'; | ||
7 | import SettingsModel from '../models/Settings'; | 6 | import SettingsModel from '../models/Settings'; |
7 | import Request from './lib/Request'; | ||
8 | import CachedRequest from './lib/CachedRequest'; | ||
9 | |||
10 | const { systemPreferences } = remote; | ||
11 | const debug = require('debug')('Franz:SettingsStore'); | ||
8 | 12 | ||
9 | export default class SettingsStore extends Store { | 13 | export default class SettingsStore extends Store { |
14 | @observable appSettingsRequest = new CachedRequest(this.api.local, 'getAppSettings'); | ||
15 | @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings'); | ||
16 | |||
10 | constructor(...args) { | 17 | constructor(...args) { |
11 | super(...args); | 18 | super(...args); |
12 | 19 | ||
@@ -15,36 +22,110 @@ export default class SettingsStore extends Store { | |||
15 | this.actions.settings.remove.listen(this._remove.bind(this)); | 22 | this.actions.settings.remove.listen(this._remove.bind(this)); |
16 | } | 23 | } |
17 | 24 | ||
18 | setup() { | 25 | async setup() { |
19 | this._shareSettingsWithMainProcess(); | 26 | // 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 |
27 | await this.appSettingsRequest._promise; | ||
28 | await this._migrate(); | ||
20 | } | 29 | } |
21 | 30 | ||
22 | @computed get all() { | 31 | @computed get all() { |
23 | return new SettingsModel(localStorage.getItem('app') || {}); | 32 | return new SettingsModel({ |
33 | app: this.appSettingsRequest.execute().result || {}, | ||
34 | service: localStorage.getItem('service') || {}, | ||
35 | group: localStorage.getItem('group') || {}, | ||
36 | stats: localStorage.getItem('stats') || {}, | ||
37 | migration: localStorage.getItem('migration') || {}, | ||
38 | }); | ||
24 | } | 39 | } |
25 | 40 | ||
26 | @action async _update({ settings }) { | 41 | @action async _update({ type, data }) { |
27 | const appSettings = this.all; | 42 | const appSettings = this.all; |
28 | localStorage.setItem('app', Object.assign(appSettings, settings)); | 43 | if (type !== 'app') { |
29 | 44 | debug('Update settings', type, data, this.all); | |
30 | // We need a little hack to wait until everything is patched | 45 | localStorage.setItem(type, Object.assign(appSettings[type], data)); |
31 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); | 46 | } else { |
47 | debug('Update settings on file system', type, data); | ||
48 | this.updateAppSettingsRequest.execute(data); | ||
32 | 49 | ||
33 | gaEvent('Settings', 'update'); | 50 | this.appSettingsRequest.patch((result) => { |
51 | if (!result) return; | ||
52 | Object.assign(result, data); | ||
53 | }); | ||
54 | } | ||
34 | } | 55 | } |
35 | 56 | ||
36 | @action async _remove({ key }) { | 57 | @action async _remove({ type, key }) { |
37 | const appSettings = this.all; | 58 | if (type === 'app') return; // app keys can't be deleted |
59 | |||
60 | const appSettings = this.all[type]; | ||
38 | if (Object.hasOwnProperty.call(appSettings, key)) { | 61 | if (Object.hasOwnProperty.call(appSettings, key)) { |
39 | delete appSettings[key]; | 62 | delete appSettings[key]; |
40 | localStorage.setItem('app', appSettings); | ||
41 | } | ||
42 | 63 | ||
43 | this._shareSettingsWithMainProcess(); | 64 | this.actions.settings.update({ |
65 | type, | ||
66 | data: appSettings, | ||
67 | }); | ||
68 | } | ||
44 | } | 69 | } |
45 | 70 | ||
46 | // Reactions | 71 | // Helper |
47 | _shareSettingsWithMainProcess() { | 72 | async _migrate() { |
48 | ipcRenderer.send('settings', this.all); | 73 | const legacySettings = localStorage.getItem('app') || {}; |
74 | |||
75 | if (!this.all.migration['5.0.0-beta.17-settings']) { | ||
76 | this.actions.settings.update({ | ||
77 | type: 'app', | ||
78 | data: { | ||
79 | autoLaunchInBackground: legacySettings.autoLaunchInBackground, | ||
80 | runInBackground: legacySettings.runInBackground, | ||
81 | enableSystemTray: legacySettings.enableSystemTray, | ||
82 | minimizeToSystemTray: legacySettings.minimizeToSystemTray, | ||
83 | isAppMuted: legacySettings.isAppMuted, | ||
84 | enableGPUAcceleration: legacySettings.enableGPUAcceleration, | ||
85 | showMessageBadgeWhenMuted: legacySettings.showMessageBadgeWhenMuted, | ||
86 | showDisabledServices: legacySettings.showDisabledServices, | ||
87 | enableSpellchecking: legacySettings.enableSpellchecking, | ||
88 | }, | ||
89 | }); | ||
90 | |||
91 | this.actions.settings.update({ | ||
92 | type: 'service', | ||
93 | data: { | ||
94 | activeService: legacySettings.activeService, | ||
95 | }, | ||
96 | }); | ||
97 | |||
98 | this.actions.settings.update({ | ||
99 | type: 'migration', | ||
100 | data: { | ||
101 | '5.0.0-beta.17-settings': true, | ||
102 | }, | ||
103 | }); | ||
104 | |||
105 | localStorage.removeItem('app'); | ||
106 | |||
107 | debug('Migrated settings to split stores'); | ||
108 | } | ||
109 | |||
110 | // Enable dark mode once | ||
111 | if (!this.all.migration['5.0.0-beta.19-settings']) { | ||
112 | this.actions.settings.update({ | ||
113 | type: 'app', | ||
114 | data: { | ||
115 | darkMode: systemPreferences.isDarkMode(), | ||
116 | }, | ||
117 | }); | ||
118 | |||
119 | this.actions.settings.update({ | ||
120 | type: 'migration', | ||
121 | data: { | ||
122 | '5.0.0-beta.19-settings': true, | ||
123 | }, | ||
124 | }); | ||
125 | |||
126 | localStorage.removeItem('app'); | ||
127 | |||
128 | debug('Set up dark mode'); | ||
129 | } | ||
49 | } | 130 | } |
50 | } | 131 | } |