aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/SettingsStore.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r--src/stores/SettingsStore.js116
1 files changed, 98 insertions, 18 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js
index d8519c609..9ea14a911 100644
--- a/src/stores/SettingsStore.js
+++ b/src/stores/SettingsStore.js
@@ -1,54 +1,105 @@
1import { remote, ipcRenderer } from 'electron';
1import { action, computed, observable } from 'mobx'; 2import { action, computed, observable } from 'mobx';
2import localStorage from 'mobx-localstorage'; 3import localStorage from 'mobx-localstorage';
3 4
4import Store from './lib/Store'; 5import Store from './lib/Store';
5import SettingsModel from '../models/Settings';
6import Request from './lib/Request'; 6import Request from './lib/Request';
7import CachedRequest from './lib/CachedRequest'; 7import CachedRequest from './lib/CachedRequest';
8import { getLocale } from '../helpers/i18n-helpers';
8 9
9const debug = require('debug')('SettingsStore'); 10import { DEFAULT_APP_SETTINGS, FILE_SYSTEM_SETTINGS_TYPES } from '../config';
11import { SPELLCHECKER_LOCALES } from '../i18n/languages';
12
13const { systemPreferences } = remote;
14const debug = require('debug')('Franz:SettingsStore');
10 15
11export default class SettingsStore extends Store { 16export default class SettingsStore extends Store {
12 @observable appSettingsRequest = new CachedRequest(this.api.local, 'getAppSettings'); 17 @observable appSettingsRequest = new CachedRequest(this.api.local, 'getAppSettings');
13 @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings'); 18 @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings');
14 19
20 @observable fileSystemSettingsRequests = [];
21
22 fileSystemSettingsTypes = FILE_SYSTEM_SETTINGS_TYPES;
23 @observable _fileSystemSettingsCache = {
24 app: DEFAULT_APP_SETTINGS,
25 proxy: {},
26 };
27
15 constructor(...args) { 28 constructor(...args) {
16 super(...args); 29 super(...args);
17 30
18 // Register action handlers 31 // Register action handlers
19 this.actions.settings.update.listen(this._update.bind(this)); 32 this.actions.settings.update.listen(this._update.bind(this));
20 this.actions.settings.remove.listen(this._remove.bind(this)); 33 this.actions.settings.remove.listen(this._remove.bind(this));
34
35 this.fileSystemSettingsTypes.forEach((type) => {
36 this.fileSystemSettingsRequests[type] = new CachedRequest(this.api.local, 'getAppSettings');
37 });
38
39 ipcRenderer.on('appSettings', (event, resp) => {
40 debug('Get appSettings resolves', resp, resp.type, resp.data);
41
42 this._fileSystemSettingsCache[resp.type] = resp.data;
43 });
44
45 this.fileSystemSettingsTypes.forEach((type) => {
46 ipcRenderer.send('getAppSettings', type);
47 });
21 } 48 }
22 49
23 async setup() { 50 async setup() {
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 51 // 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; 52 await this.appSettingsRequest._promise;
26 this._migrate(); 53 await this._migrate();
54 }
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') || {};
27 } 78 }
28 79
29 @computed get all() { 80 @computed get all() {
30 return new SettingsModel({ 81 return {
31 app: this.appSettingsRequest.execute().result || {}, 82 app: this.app,
32 service: localStorage.getItem('service') || {}, 83 proxy: this.proxy,
33 group: localStorage.getItem('group') || {}, 84 service: this.service,
34 stats: localStorage.getItem('stats') || {}, 85 stats: this.stats,
35 migration: localStorage.getItem('migration') || {}, 86 migration: this.migration,
36 }); 87 };
37 } 88 }
38 89
39 @action async _update({ type, data }) { 90 @action async _update({ type, data }) {
40 const appSettings = this.all; 91 const appSettings = this.all;
41 if (type !== 'app') { 92 if (!this.fileSystemSettingsTypes.includes(type)) {
42 debug('Update settings', type, data, this.all); 93 debug('Update settings', type, data, this.all);
43 localStorage.setItem(type, Object.assign(appSettings[type], data)); 94 localStorage.setItem(type, Object.assign(appSettings[type], data));
44 } else { 95 } else {
45 debug('Update settings on file system', type, data); 96 debug('Update settings on file system', type, data);
46 this.updateAppSettingsRequest.execute(data); 97 ipcRenderer.send('updateAppSettings', {
47 98 type,
48 this.appSettingsRequest.patch((result) => { 99 data,
49 if (!result) return;
50 Object.assign(result, data);
51 }); 100 });
101
102 Object.assign(this._fileSystemSettingsCache[type], data);
52 } 103 }
53 } 104 }
54 105
@@ -67,8 +118,8 @@ export default class SettingsStore extends Store {
67 } 118 }
68 119
69 // Helper 120 // Helper
70 _migrate() { 121 async _migrate() {
71 const legacySettings = localStorage.getItem('app'); 122 const legacySettings = localStorage.getItem('app') || {};
72 123
73 if (!this.all.migration['5.0.0-beta.17-settings']) { 124 if (!this.all.migration['5.0.0-beta.17-settings']) {
74 this.actions.settings.update({ 125 this.actions.settings.update({
@@ -104,5 +155,34 @@ export default class SettingsStore extends Store {
104 155
105 debug('Migrated settings to split stores'); 156 debug('Migrated settings to split stores');
106 } 157 }
158
159 // Enable dark mode once
160 if (!this.all.migration['5.0.0-beta.19-settings']) {
161 const spellcheckerLanguage = getLocale({
162 locale: this.stores.settings.app.locale,
163 locales: SPELLCHECKER_LOCALES,
164 defaultLocale: DEFAULT_APP_SETTINGS.spellcheckerLanguage,
165 fallbackLocale: DEFAULT_APP_SETTINGS.spellcheckerLanguage,
166 });
167
168 this.actions.settings.update({
169 type: 'app',
170 data: {
171 darkMode: systemPreferences.isDarkMode(),
172 spellcheckerLanguage,
173 },
174 });
175
176 this.actions.settings.update({
177 type: 'migration',
178 data: {
179 '5.0.0-beta.19-settings': true,
180 },
181 });
182 }
183 }
184
185 _getFileBasedSettings(type) {
186 ipcRenderer.send('getAppSettings', type);
107 } 187 }
108} 188}