diff options
author | Stefan Malzner <stefan@adlk.io> | 2018-02-16 15:03:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-16 15:03:36 +0100 |
commit | d6662400e5f72822ce462f6c17f081db4ea4ac5f (patch) | |
tree | 59067a6cd261724607bc46ad1ac56210b23d1d5f /src | |
parent | Merge branch 'fix/copy-paste' into develop (diff) | |
parent | fix sideeffect of app not launching itself after first launch (diff) | |
download | ferdium-app-d6662400e5f72822ce462f6c17f081db4ea4ac5f.tar.gz ferdium-app-d6662400e5f72822ce462f6c17f081db4ea4ac5f.tar.zst ferdium-app-d6662400e5f72822ce462f6c17f081db4ea4ac5f.zip |
fix(App): Fix issues with disabling launch app on startup
Refactor settingsStore to use mobx-localstorage
Diffstat (limited to 'src')
-rw-r--r-- | src/api/LocalApi.js | 12 | ||||
-rw-r--r-- | src/api/server/LocalApi.js | 32 | ||||
-rw-r--r-- | src/api/server/ServerApi.js | 1 | ||||
-rw-r--r-- | src/models/Settings.js | 1 | ||||
-rw-r--r-- | src/stores/AppStore.js | 10 | ||||
-rw-r--r-- | src/stores/ServicesStore.js | 1 | ||||
-rw-r--r-- | src/stores/SettingsStore.js | 26 | ||||
-rw-r--r-- | src/stores/UserStore.js | 3 |
8 files changed, 15 insertions, 71 deletions
diff --git a/src/api/LocalApi.js b/src/api/LocalApi.js index 3f84f8a0b..59d7d8fa2 100644 --- a/src/api/LocalApi.js +++ b/src/api/LocalApi.js | |||
@@ -4,18 +4,6 @@ export default class LocalApi { | |||
4 | this.local = local; | 4 | this.local = local; |
5 | } | 5 | } |
6 | 6 | ||
7 | getSettings() { | ||
8 | return this.local.getAppSettings(); | ||
9 | } | ||
10 | |||
11 | updateSettings(data) { | ||
12 | return this.local.updateAppSettings(data); | ||
13 | } | ||
14 | |||
15 | removeKey(key) { | ||
16 | return this.local.removeKey(key); | ||
17 | } | ||
18 | |||
19 | getAppCacheSize() { | 7 | getAppCacheSize() { |
20 | return this.local.getAppCacheSize(); | 8 | return this.local.getAppCacheSize(); |
21 | } | 9 | } |
diff --git a/src/api/server/LocalApi.js b/src/api/server/LocalApi.js index e95d750ac..aa3a7d655 100644 --- a/src/api/server/LocalApi.js +++ b/src/api/server/LocalApi.js | |||
@@ -6,38 +6,6 @@ import { getServicePartitionsDirectory } from '../../helpers/service-helpers.js' | |||
6 | const { session } = remote; | 6 | const { session } = remote; |
7 | 7 | ||
8 | export default class LocalApi { | 8 | export default class LocalApi { |
9 | // App | ||
10 | async updateAppSettings(data) { | ||
11 | const currentSettings = await this.getAppSettings(); | ||
12 | const settings = Object.assign(currentSettings, data); | ||
13 | |||
14 | localStorage.setItem('app', JSON.stringify(settings)); | ||
15 | console.debug('LocalApi::updateAppSettings resolves', settings); | ||
16 | |||
17 | return settings; | ||
18 | } | ||
19 | |||
20 | async getAppSettings() { | ||
21 | const settingsString = localStorage.getItem('app'); | ||
22 | try { | ||
23 | const settings = JSON.parse(settingsString) || {}; | ||
24 | console.debug('LocalApi::getAppSettings resolves', settings); | ||
25 | |||
26 | return settings; | ||
27 | } catch (err) { | ||
28 | return {}; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | async removeKey(key) { | ||
33 | const settings = await this.getAppSettings(); | ||
34 | |||
35 | if (Object.hasOwnProperty.call(settings, key)) { | ||
36 | delete settings[key]; | ||
37 | localStorage.setItem('app', JSON.stringify(settings)); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | // Services | 9 | // Services |
42 | async getAppCacheSize() { | 10 | async getAppCacheSize() { |
43 | const partitionsDir = getServicePartitionsDirectory(); | 11 | const partitionsDir = getServicePartitionsDirectory(); |
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index d37ff51f8..8f3297d13 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -3,6 +3,7 @@ import path from 'path'; | |||
3 | import tar from 'tar'; | 3 | import tar from 'tar'; |
4 | import fs from 'fs-extra'; | 4 | import fs from 'fs-extra'; |
5 | import { remote } from 'electron'; | 5 | import { remote } from 'electron'; |
6 | import localStorage from 'mobx-localstorage'; | ||
6 | 7 | ||
7 | import ServiceModel from '../../models/Service'; | 8 | import ServiceModel from '../../models/Service'; |
8 | import RecipePreviewModel from '../../models/RecipePreview'; | 9 | import RecipePreviewModel from '../../models/RecipePreview'; |
diff --git a/src/models/Settings.js b/src/models/Settings.js index ca44da258..e39b63087 100644 --- a/src/models/Settings.js +++ b/src/models/Settings.js | |||
@@ -2,7 +2,6 @@ import { observable, extendObservable } from 'mobx'; | |||
2 | import { DEFAULT_APP_SETTINGS } from '../config'; | 2 | import { DEFAULT_APP_SETTINGS } from '../config'; |
3 | 3 | ||
4 | export default class Settings { | 4 | export default class Settings { |
5 | @observable autoLaunchOnStart = DEFAULT_APP_SETTINGS.autoLaunchOnStart; | ||
6 | @observable autoLaunchInBackground = DEFAULT_APP_SETTINGS.autoLaunchInBackground; | 5 | @observable autoLaunchInBackground = DEFAULT_APP_SETTINGS.autoLaunchInBackground; |
7 | @observable runInBackground = DEFAULT_APP_SETTINGS.runInBackground; | 6 | @observable runInBackground = DEFAULT_APP_SETTINGS.runInBackground; |
8 | @observable enableSystemTray = DEFAULT_APP_SETTINGS.enableSystemTray; | 7 | @observable enableSystemTray = DEFAULT_APP_SETTINGS.enableSystemTray; |
diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index dc5a04c46..9b59f01d2 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js | |||
@@ -331,10 +331,7 @@ export default class AppStore extends Store { | |||
331 | } | 331 | } |
332 | 332 | ||
333 | // Helpers | 333 | // Helpers |
334 | async _appStartsCounter() { | 334 | _appStartsCounter() { |
335 | // we need to wait until the settings request is resolved | ||
336 | await this.stores.settings.allSettingsRequest; | ||
337 | |||
338 | this.actions.settings.update({ | 335 | this.actions.settings.update({ |
339 | settings: { | 336 | settings: { |
340 | appStarts: (this.stores.settings.all.appStarts || 0) + 1, | 337 | appStarts: (this.stores.settings.all.appStarts || 0) + 1, |
@@ -345,10 +342,7 @@ export default class AppStore extends Store { | |||
345 | async _autoStart() { | 342 | async _autoStart() { |
346 | this.autoLaunchOnStart = await this._checkAutoStart(); | 343 | this.autoLaunchOnStart = await this._checkAutoStart(); |
347 | 344 | ||
348 | // we need to wait until the settings request is resolved | 345 | if (this.stores.settings.all.appStarts === 1) { |
349 | await this.stores.settings.allSettingsRequest; | ||
350 | |||
351 | if (!this.stores.settings.all.appStarts) { | ||
352 | this.actions.app.launchOnStartup({ | 346 | this.actions.app.launchOnStartup({ |
353 | enable: true, | 347 | enable: true, |
354 | }); | 348 | }); |
diff --git a/src/stores/ServicesStore.js b/src/stores/ServicesStore.js index 99ffe5439..c38d0d9ee 100644 --- a/src/stores/ServicesStore.js +++ b/src/stores/ServicesStore.js | |||
@@ -536,7 +536,6 @@ export default class ServicesStore extends Store { | |||
536 | 536 | ||
537 | // We can't just block this earlier, otherwise the mobx reaction won't be aware of the vars to watch in some cases | 537 | // We can't just block this earlier, otherwise the mobx reaction won't be aware of the vars to watch in some cases |
538 | if (showMessageBadgesEvenWhenMuted) { | 538 | if (showMessageBadgesEvenWhenMuted) { |
539 | console.log('set badge', unreadDirectMessageCount, unreadIndirectMessageCount); | ||
540 | this.actions.app.setBadge({ | 539 | this.actions.app.setBadge({ |
541 | unreadDirectMessageCount, | 540 | unreadDirectMessageCount, |
542 | unreadIndirectMessageCount, | 541 | unreadIndirectMessageCount, |
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index da99a720f..b7d803398 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js | |||
@@ -1,17 +1,12 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { ipcRenderer } from 'electron'; |
2 | import { action, computed, observable, extendObservable } from 'mobx'; | 2 | import { action, computed } from 'mobx'; |
3 | import localStorage from 'mobx-localstorage'; | ||
3 | 4 | ||
4 | import Store from './lib/Store'; | 5 | import Store from './lib/Store'; |
5 | import Request from './lib/Request'; | ||
6 | import CachedRequest from './lib/CachedRequest'; | ||
7 | import { gaEvent } from '../lib/analytics'; | 6 | import { gaEvent } from '../lib/analytics'; |
8 | import SettingsModel from '../models/Settings'; | 7 | import SettingsModel from '../models/Settings'; |
9 | 8 | ||
10 | export default class SettingsStore extends Store { | 9 | export default class SettingsStore extends Store { |
11 | @observable allSettingsRequest = new CachedRequest(this.api.local, 'getSettings'); | ||
12 | @observable updateSettingsRequest = new Request(this.api.local, 'updateSettings'); | ||
13 | @observable removeSettingsKeyRequest = new Request(this.api.local, 'removeKey'); | ||
14 | |||
15 | constructor(...args) { | 10 | constructor(...args) { |
16 | super(...args); | 11 | super(...args); |
17 | 12 | ||
@@ -21,20 +16,16 @@ export default class SettingsStore extends Store { | |||
21 | } | 16 | } |
22 | 17 | ||
23 | setup() { | 18 | setup() { |
24 | this.allSettingsRequest.execute(); | ||
25 | this._shareSettingsWithMainProcess(); | 19 | this._shareSettingsWithMainProcess(); |
26 | } | 20 | } |
27 | 21 | ||
28 | @computed get all() { | 22 | @computed get all() { |
29 | return new SettingsModel(this.allSettingsRequest.result); | 23 | return new SettingsModel(localStorage.getItem('app') || {}); |
30 | } | 24 | } |
31 | 25 | ||
32 | @action async _update({ settings }) { | 26 | @action async _update({ settings }) { |
33 | await this.updateSettingsRequest.execute(settings)._promise; | 27 | const appSettings = this.all; |
34 | await this.allSettingsRequest.patch((result) => { | 28 | localStorage.setItem('app', Object.assign(appSettings, settings)); |
35 | if (!result) return; | ||
36 | extendObservable(result, settings); | ||
37 | }); | ||
38 | 29 | ||
39 | // We need a little hack to wait until everything is patched | 30 | // We need a little hack to wait until everything is patched |
40 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); | 31 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); |
@@ -43,8 +34,11 @@ export default class SettingsStore extends Store { | |||
43 | } | 34 | } |
44 | 35 | ||
45 | @action async _remove({ key }) { | 36 | @action async _remove({ key }) { |
46 | await this.removeSettingsKeyRequest.execute(key); | 37 | const appSettings = this.all; |
47 | await this.allSettingsRequest.invalidate({ immediately: true }); | 38 | if (Object.hasOwnProperty.call(appSettings, key)) { |
39 | delete appSettings[key]; | ||
40 | localStorage.setItem('app', appSettings); | ||
41 | } | ||
48 | 42 | ||
49 | this._shareSettingsWithMainProcess(); | 43 | this._shareSettingsWithMainProcess(); |
50 | } | 44 | } |
diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index abec4df5d..7dbbd955b 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js | |||
@@ -1,6 +1,7 @@ | |||
1 | import { observable, computed, action } from 'mobx'; | 1 | import { observable, computed, action } from 'mobx'; |
2 | import moment from 'moment'; | 2 | import moment from 'moment'; |
3 | import jwt from 'jsonwebtoken'; | 3 | import jwt from 'jsonwebtoken'; |
4 | import localStorage from 'mobx-localstorage'; | ||
4 | 5 | ||
5 | import { isDevMode } from '../environment'; | 6 | import { isDevMode } from '../environment'; |
6 | import Store from './lib/Store'; | 7 | import Store from './lib/Store'; |
@@ -99,7 +100,7 @@ export default class UserStore extends Store { | |||
99 | 100 | ||
100 | // Data | 101 | // Data |
101 | @computed get isLoggedIn() { | 102 | @computed get isLoggedIn() { |
102 | return this.authToken !== null && this.authToken !== undefined; | 103 | return Boolean(localStorage.getItem('authToken')); |
103 | } | 104 | } |
104 | 105 | ||
105 | // @computed get isTokenValid() { | 106 | // @computed get isTokenValid() { |