From 97d5f8762dce6ebf8289b23eeccc23f6869a9565 Mon Sep 17 00:00:00 2001 From: djmaze Date: Thu, 18 Mar 2021 16:12:24 +0100 Subject: [PATCH] Improve SettingsUserStore handling --- dev/App/User.js | 10 ++-- dev/Remote/User/Fetch.js | 14 +++-- dev/Screen/User/MailBox.js | 2 - dev/Settings/User/General.js | 52 ++++++------------- dev/Settings/User/OpenPgp.js | 8 +-- dev/Settings/User/Security.js | 20 +++---- dev/Stores/User/Settings.js | 39 ++++---------- .../Views/User/SettingsSecurity.html | 2 +- 8 files changed, 50 insertions(+), 97 deletions(-) diff --git a/dev/App/User.js b/dev/App/User.js index 282b98fa4d..6c0371a38d 100644 --- a/dev/App/User.js +++ b/dev/App/User.js @@ -130,10 +130,6 @@ class AppUser extends AbstractApp { const fn = (ev=>$htmlCL.toggle('rl-ctrl-key-pressed', ev.ctrlKey)).debounce(500); ['keydown','keyup'].forEach(t => doc.addEventListener(t, fn)); - ['touchstart','mousedown','mousemove','keydown'].forEach( - t => doc.addEventListener(t, SettingsUserStore.delayLogout, {passive:true}) - ); - shortcuts.add('escape,enter', '', Scope.All, () => rl.Dropdowns.detectVisibility()); } @@ -884,7 +880,6 @@ class AppUser extends AbstractApp { addEventListener('resize', () => leftPanelDisabled(ThemeStore.isMobile() || 1000 > innerWidth)); - SettingsUserStore.populate(); NotificationUserStore.populate(); AccountUserStore.populate(); ContactUserStore.populate(); @@ -1011,6 +1006,11 @@ class AppUser extends AbstractApp { }, 500); } + ['touchstart','mousedown','mousemove','keydown'].forEach( + t => doc.addEventListener(t, SettingsUserStore.delayLogout, {passive:true}) + ); + SettingsUserStore.delayLogout(); + setTimeout(() => this.initVerticalLayoutResizer(), 1); } else { this.logout(); diff --git a/dev/Remote/User/Fetch.js b/dev/Remote/User/Fetch.js index d50441084a..8217f83b25 100644 --- a/dev/Remote/User/Fetch.js +++ b/dev/Remote/User/Fetch.js @@ -583,15 +583,13 @@ class RemoteUserFetch extends AbstractFetchRemote { /** * @param {string} key - * @param {?Function} valueFn - * @param {?Function} fn + * @param {?scalar} value + * @param {?Function} fCallback */ - saveSettingsHelper(key, valueFn, fn) { - return (value) => { - this.saveSettings(fn || null, { - [key]: valueFn ? valueFn(value) : value - }); - }; + saveSetting(key, value, fCallback) { + this.saveSettings(fCallback, { + [key]: value + }); } /** diff --git a/dev/Screen/User/MailBox.js b/dev/Screen/User/MailBox.js index cf67ddf287..83e7c3e28f 100644 --- a/dev/Screen/User/MailBox.js +++ b/dev/Screen/User/MailBox.js @@ -6,7 +6,6 @@ import { i18n } from 'Common/Translator'; import { AppUserStore } from 'Stores/User/App'; import { AccountUserStore } from 'Stores/User/Account'; -import { SettingsUserStore } from 'Stores/User/Settings'; import { FolderUserStore } from 'Stores/User/Folder'; import { MessageUserStore } from 'Stores/User/Message'; import { ThemeStore } from 'Stores/Theme'; @@ -89,7 +88,6 @@ export class MailBoxUserScreen extends AbstractScreen { onStart() { if (!this.__started) { super.onStart(); - setTimeout(() => SettingsUserStore.layout.valueHasMutated(), 50); setTimeout(() => warmUpScreenPopup(ComposePopupView), 500); addEventListener('mailbox.inbox-unread-count', e => { diff --git a/dev/Settings/User/General.js b/dev/Settings/User/General.js index 0a669d9286..15aecfc2f7 100644 --- a/dev/Settings/User/General.js +++ b/dev/Settings/User/General.js @@ -84,7 +84,6 @@ export class GeneralUserSettings { ]; }); - const fReloadLanguageHelper = (saveSettingsStep) => () => { this.languageTrigger(saveSettingsStep); setTimeout(() => this.languageTrigger(SaveSettingsStep.Idle), 1000); @@ -93,56 +92,39 @@ export class GeneralUserSettings { language: value => { this.languageTrigger(SaveSettingsStep.Animate); translatorReload(false, value) - .then(fReloadLanguageHelper(SaveSettingsStep.TrueResult), fReloadLanguageHelper(SaveSettingsStep.FalseResult)) - .then(() => { - Remote.saveSettings(null, { - 'Language': value - }); - }); + .then(fReloadLanguageHelper(SaveSettingsStep.TrueResult), + fReloadLanguageHelper(SaveSettingsStep.FalseResult)) + .then(() => Remote.saveSetting('Language', value)); }, - editorDefaultType: - Remote.saveSettingsHelper('EditorDefaultType', null, - settingsSaveHelperSimpleFunction(this.editorDefaultTypeTrigger, this)), + editorDefaultType: value => Remote.saveSetting('EditorDefaultType', value, + settingsSaveHelperSimpleFunction(this.editorDefaultTypeTrigger, this)), - messagesPerPage: Remote.saveSettingsHelper('MPP', null, settingsSaveHelperSimpleFunction(this.mppTrigger, this)), + messagesPerPage: value => Remote.saveSetting('MPP', value, + settingsSaveHelperSimpleFunction(this.mppTrigger, this)), - showImages: Remote.saveSettingsHelper('ShowImages', v=>v?'1':'0'), + showImages: value => Remote.saveSetting('ShowImages', value ? 1 : 0), - removeColors: Remote.saveSettingsHelper('RemoveColors', v=>v?'1':'0'), + removeColors: value => { + Remote.saveSetting('RemoveColors', value ? 1 : 0); + }, - useCheckboxesInList: Remote.saveSettingsHelper('UseCheckboxesInList', v=>v?'1':'0'), + useCheckboxesInList: value => Remote.saveSetting('UseCheckboxesInList', value ? 1 : 0), - enableDesktopNotification: (value => - Remote.saveSettings(null, { - 'DesktopNotifications': value ? 1 : 0 - }) - ).debounce(3000), + enableDesktopNotification: value => Remote.saveSetting('DesktopNotifications', value ? 1 : 0), - enableSoundNotification: (value => - Remote.saveSettings(null, { - 'SoundNotification': value ? 1 : 0 - }) - ).debounce(3000), + enableSoundNotification: value => Remote.saveSetting('SoundNotification', value ? 1 : 0), - replySameFolder: (value => - Remote.saveSettings(null, { - 'ReplySameFolder': value ? 1 : 0 - }) - ).debounce(3000), + replySameFolder: value => Remote.saveSetting('ReplySameFolder', value ? 1 : 0), useThreads: value => { MessageUserStore.list([]); - Remote.saveSettings(null, { - 'UseThreads': value ? 1 : 0 - }); + Remote.saveSetting('UseThreads', value ? 1 : 0); }, layout: value => { MessageUserStore.list([]); - Remote.saveSettings(settingsSaveHelperSimpleFunction(this.layoutTrigger, this), { - 'Layout': value - }); + Remote.saveSetting('Layout', value, settingsSaveHelperSimpleFunction(this.layoutTrigger, this)); } }); } diff --git a/dev/Settings/User/OpenPgp.js b/dev/Settings/User/OpenPgp.js index a6427ca062..7a50de7bfb 100644 --- a/dev/Settings/User/OpenPgp.js +++ b/dev/Settings/User/OpenPgp.js @@ -22,6 +22,8 @@ export class OpenPgpUserSettings { this.openPgpKeyForDeletion = ko.observable(null).deleteAccessHelper(); this.allowDraftAutosave = SettingsUserStore.allowDraftAutosave; + + this.allowDraftAutosave.subscribe(value => Remote.saveSetting('AllowDraftAutosave', value ? 1 : 0)) } addOpenPgpKey() { @@ -61,10 +63,4 @@ export class OpenPgpUserSettings { } } } - - onBuild() { - setTimeout(() => { - this.allowDraftAutosave.subscribe(Remote.saveSettingsHelper('AllowDraftAutosave', v=>v?'1':'0')); - }, 50); - } } diff --git a/dev/Settings/User/Security.js b/dev/Settings/User/Security.js index c2609bdd8b..1d51a3beb6 100644 --- a/dev/Settings/User/Security.js +++ b/dev/Settings/User/Security.js @@ -19,7 +19,7 @@ export class SecurityUserSettings { this.capaTwoFactor = Settings.capa(Capa.TwoFactor); this.autoLogout = SettingsUserStore.autoLogout; - this.autoLogout.trigger = ko.observable(SaveSettingsStep.Idle); + this.autoLogoutTrigger = ko.observable(SaveSettingsStep.Idle); let i18nLogout = (key, params) => i18n('SETTINGS_SECURITY/AUTOLOGIN_' + key, params); this.autoLogoutOptions = ko.computed(() => { @@ -35,20 +35,16 @@ export class SecurityUserSettings { { 'id': 60 * 10, 'name': i18nLogout('HOURS_OPTION_NAME', { 'HOURS': 10 }) } ]; }); + + if (this.capaAutoLogout) { + this.autoLogout.subscribe(value => Remote.saveSetting( + 'AutoLogout', pInt(value), + settingsSaveHelperSimpleFunction(this.autoLogoutTrigger, this) + )); + } } configureTwoFactor() { showScreenPopup(TwoFactorConfigurationPopupView); } - - onBuild() { - if (this.capaAutoLogout) { - setTimeout(() => - this.autoLogout.subscribe(Remote.saveSettingsHelper( - 'AutoLogout', pInt, - settingsSaveHelperSimpleFunction(this.autoLogout.trigger, this) - )) - ); - } - } } diff --git a/dev/Stores/User/Settings.js b/dev/Stores/User/Settings.js index 8b829243de..c8a09b11b3 100644 --- a/dev/Stores/User/Settings.js +++ b/dev/Stores/User/Settings.js @@ -9,10 +9,10 @@ import { ThemeStore } from 'Stores/Theme'; export const SettingsUserStore = new class { constructor() { this.layout = ko - .observable(Layout.SidePreview) + .observable(pInt(SettingsGet('Layout'))) .extend({ limitedList: [Layout.SidePreview, Layout.BottomPreview, Layout.NoPreview] }); - this.editorDefaultType = ko.observable(EditorDefaultType.Html).extend({ + this.editorDefaultType = ko.observable(SettingsGet('EditorDefaultType')).extend({ limitedList: [ EditorDefaultType.Html, EditorDefaultType.Plain, @@ -21,17 +21,17 @@ export const SettingsUserStore = new class { ] }); - this.messagesPerPage = ko.observable(20).extend({ limitedList: MESSAGES_PER_PAGE_VALUES }); + this.messagesPerPage = ko.observable(SettingsGet('MPP')).extend({ limitedList: MESSAGES_PER_PAGE_VALUES }); addObservablesTo(this, { - showImages: false, - removeColors: false, - useCheckboxesInList: true, - allowDraftAutosave: true, - useThreads: false, - replySameFolder: false, - - autoLogout: 30 + showImages: !!SettingsGet('ShowImages'), + removeColors: !!SettingsGet('RemoveColors'), + useCheckboxesInList: !!(ThemeStore.isMobile() || SettingsGet('UseCheckboxesInList')), + allowDraftAutosave: !!SettingsGet('AllowDraftAutosave'), + useThreads: !!SettingsGet('UseThreads'), + replySameFolder: !!SettingsGet('ReplySameFolder'), + + autoLogout: pInt(SettingsGet('AutoLogout')) }); this.usePreviewPane = ko.computed(() => Layout.NoPreview !== this.layout() && !ThemeStore.isMobile()); @@ -54,21 +54,4 @@ export const SettingsUserStore = new class { } }).throttle(5000); } - - populate() { - this.layout(pInt(SettingsGet('Layout'))); - this.editorDefaultType(SettingsGet('EditorDefaultType')); - - this.autoLogout(pInt(SettingsGet('AutoLogout'))); - this.messagesPerPage(SettingsGet('MPP')); - - this.showImages(!!SettingsGet('ShowImages')); - this.removeColors(!!SettingsGet('RemoveColors')); - this.useCheckboxesInList(!!(ThemeStore.isMobile() || SettingsGet('UseCheckboxesInList'))); - this.allowDraftAutosave(!!SettingsGet('AllowDraftAutosave')); - this.useThreads(!!SettingsGet('UseThreads')); - this.replySameFolder(!!SettingsGet('ReplySameFolder')); - - this.delayLogout(); - } }; diff --git a/snappymail/v/0.0.0/app/templates/Views/User/SettingsSecurity.html b/snappymail/v/0.0.0/app/templates/Views/User/SettingsSecurity.html index 18a9c1f982..e44fe8d190 100644 --- a/snappymail/v/0.0.0/app/templates/Views/User/SettingsSecurity.html +++ b/snappymail/v/0.0.0/app/templates/Views/User/SettingsSecurity.html @@ -14,7 +14,7 @@ inline: true, options: autoLogoutOptions, value: autoLogout, - trigger: autoLogout.trigger, + trigger: autoLogoutTrigger, optionsText: 'name', optionsValue: 'id' }