From 7b9904e423a3dd801f0bd6ae125d8c85103f4664 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 18 Mar 2021 20:45:01 +0100 Subject: [PATCH] add UI in settings for push notifs status/enable/disable --- .../session/settings/SettingsViewModel.js | 27 ++++++++ .../web/ui/session/settings/SettingsView.js | 65 ++++++++++++++----- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/domain/session/settings/SettingsViewModel.js b/src/domain/session/settings/SettingsViewModel.js index 50aabd2c..ec171eea 100644 --- a/src/domain/session/settings/SettingsViewModel.js +++ b/src/domain/session/settings/SettingsViewModel.js @@ -17,6 +17,14 @@ limitations under the License. import {ViewModel} from "../../ViewModel.js"; import {SessionBackupViewModel} from "./SessionBackupViewModel.js"; +class PushNotificationStatus { + constructor() { + this.supported = null; + this.enabled = false; + this.updating = false; + } +} + function formatKey(key) { const partLength = 4; const partCount = Math.ceil(key.length / partLength); @@ -40,6 +48,7 @@ export class SettingsViewModel extends ViewModel { this.sentImageSizeLimit = null; this.minSentImageSizeLimit = 400; this.maxSentImageSizeLimit = 4000; + this.pushNotifications = new PushNotificationStatus(); } setSentImageSizeLimit(size) { @@ -56,6 +65,8 @@ export class SettingsViewModel extends ViewModel { async load() { this._estimate = await this.platform.estimateStorageUsage(); this.sentImageSizeLimit = await this.platform.settingsStorage.getInt("sentImageSizeLimit"); + this.pushNotifications.supported = await this.platform.notificationService.supportsPush(); + this.pushNotifications.enabled = await this._session.arePushNotificationsEnabled(); this.emitChange(""); } @@ -115,5 +126,21 @@ export class SettingsViewModel extends ViewModel { const logExport = await this.logger.export(); this.platform.saveFileAs(logExport.asBlob(), `hydrogen-logs-${this.platform.clock.now()}.json`); } + + async togglePushNotifications() { + this.pushNotifications.updating = true; + this.emitChange("pushNotifications.updating"); + try { + if (await this._session.enablePushNotifications(!this.pushNotifications.enabled)) { + this.pushNotifications.enabled = !this.pushNotifications.enabled; + if (this.pushNotifications.enabled) { + this.platform.notificationService.showNotification(this.i18n`Push notifications are now enabled`); + } + } + } finally { + this.pushNotifications.updating = false; + this.emitChange("pushNotifications.updating"); + } + } } diff --git a/src/platform/web/ui/session/settings/SettingsView.js b/src/platform/web/ui/session/settings/SettingsView.js index 02e57f5e..4d4b2aba 100644 --- a/src/platform/web/ui/session/settings/SettingsView.js +++ b/src/platform/web/ui/session/settings/SettingsView.js @@ -34,27 +34,60 @@ export class SettingsView extends TemplateView { ]); }; + const settingNodes = []; + + settingNodes.push( + t.h3("Session"), + row(vm.i18n`User ID`, vm.userId), + row(vm.i18n`Session ID`, vm.deviceId, "code"), + row(vm.i18n`Session key`, vm.fingerprintKey, "code") + ); + settingNodes.push( + t.h3("Session Backup"), + t.view(new SessionBackupSettingsView(vm.sessionBackupViewModel)) + ); + + settingNodes.push( + t.h3("Notifications"), + t.map(vm => vm.pushNotifications.supported, (supported, t) => { + if (supported === null) { + return t.p(vm.i18n`Loading…`); + } else if (supported) { + const label = vm => vm.pushNotifications.enabled ? + vm.i18n`Push notifications are enabled`: + vm.i18n`Push notifications are disabled`; + const buttonLabel = vm => vm.pushNotifications.enabled ? + vm.i18n`Disable`: + vm.i18n`Enable`; + return row(label, t.button({ + onClick: () => vm.togglePushNotifications(), + disabled: vm => vm.pushNotifications.updating + }, buttonLabel)); + } else { + return t.p(vm.i18n`Push notifications are not supported on this browser`); + } + }) + ); + + settingNodes.push( + t.h3("Preferences"), + row(vm.i18n`Scale down images when sending`, this._imageCompressionRange(t, vm)), + ); + settingNodes.push( + t.h3("Application"), + row(vm.i18n`Version`, version), + row(vm.i18n`Storage usage`, vm => `${vm.storageUsage} / ${vm.storageQuota}`), + row(vm.i18n`Debug logs`, t.button({onClick: () => vm.exportLogs()}, "Export")), + t.p(["Debug logs contain application usage data including your username, the IDs or aliases of the rooms or groups you have visited, the usernames of other users and the names of files you send. They do not contain messages. For more information, review our ", + t.a({href: "https://element.io/privacy", target: "_blank", rel: "noopener"}, "privacy policy"), "."]), + ); + return t.main({className: "Settings middle"}, [ t.div({className: "middle-header"}, [ t.a({className: "button-utility close-middle", href: vm.closeUrl, title: vm.i18n`Close settings`}), t.h2("Settings") ]), - t.div({className: "SettingsBody"}, [ - t.h3("Session"), - row(vm.i18n`User ID`, vm.userId), - row(vm.i18n`Session ID`, vm.deviceId, "code"), - row(vm.i18n`Session key`, vm.fingerprintKey, "code"), - t.h3("Session Backup"), - t.view(new SessionBackupSettingsView(vm.sessionBackupViewModel)), - t.h3("Preferences"), - row(vm.i18n`Scale down images when sending`, this._imageCompressionRange(t, vm)), - t.h3("Application"), - row(vm.i18n`Version`, version), - row(vm.i18n`Storage usage`, vm => `${vm.storageUsage} / ${vm.storageQuota}`), - row(vm.i18n`Debug logs`, t.button({onClick: () => vm.exportLogs()}, "Export")), - t.p(["Debug logs contain application usage data including your username, the IDs or aliases of the rooms or groups you have visited, the usernames of other users and the names of files you send. They do not contain messages. For more information, review our ", - t.a({href: "https://element.io/privacy", target: "_blank", rel: "noopener"}, "privacy policy"), "."]), - ]) + t.div({className: "SettingsBody"}, settingNodes) ]); }