From df80ff953375fdd00825c3b1bb415a1dfc51f4fc Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 7 Jun 2022 18:46:06 +0200 Subject: [PATCH] Put sound notifications in SMAudio --- dev/App/User.js | 5 +++-- dev/Common/Audio.js | 34 +++++++++++++++++++++------------ dev/Settings/User/General.js | 11 ++++++----- dev/Stores/User/Messagelist.js | 7 ++++--- dev/Stores/User/Notification.js | 23 +++++----------------- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/dev/App/User.js b/dev/App/User.js index a653f469b..58f65c737 100644 --- a/dev/App/User.js +++ b/dev/App/User.js @@ -1,5 +1,6 @@ import 'External/User/ko'; +import { SMAudio } from 'Common/Audio'; import { isArray, pString, changeTheme } from 'Common/Utils'; import { mailToHelper, setLayoutResizer, dropdownsDetectVisibility } from 'Common/UtilsUser'; @@ -289,8 +290,8 @@ export class AppUser extends AbstractApp { if (SettingsGet('Auth')) { rl.setWindowTitle(i18n('GLOBAL/LOADING')); - NotificationUserStore.enableSoundNotification(!!SettingsGet('SoundNotification')); - NotificationUserStore.enableDesktopNotification(!!SettingsGet('DesktopNotifications')); + SMAudio.notifications(!!SettingsGet('SoundNotification')); + NotificationUserStore.enabled(!!SettingsGet('DesktopNotifications')); AccountUserStore.email(SettingsGet('Email')); diff --git a/dev/Common/Audio.js b/dev/Common/Audio.js index a77efbcd5..befd17491 100644 --- a/dev/Common/Audio.js +++ b/dev/Common/Audio.js @@ -1,5 +1,6 @@ import * as Links from 'Common/Links'; import { doc, SettingsGet, fireEvent, addEventsListener } from 'Common/Globals'; +import { addObservablesTo } from 'External/ko'; let notificator = null, player = null, @@ -50,7 +51,7 @@ let notificator = null, audioCtx.resume(); } unlockEvents.forEach(type => doc.removeEventListener(type, unlock, true)); -// setTimeout(()=>Audio.playNotification(1),1); +// setTimeout(()=>SMAudio.playNotification(0,1),1); }; if (audioCtx) { @@ -76,6 +77,10 @@ export const SMAudio = new class { addEventsListener(player, ['ended','error'], stopFn); addEventListener('audio.api.stop', stopFn); } + + addObservablesTo(this, { + notifications: false + }); } paused() { @@ -103,18 +108,23 @@ export const SMAudio = new class { this.supportedWav && play(url, name); } - playNotification(silent) { - if ('running' == audioCtx.state && (this.supportedMp3 || this.supportedOgg)) { - notificator = notificator || createNewObject(); - if (notificator) { - notificator.src = Links.staticLink('sounds/' - + SettingsGet('NotificationSound') - + (this.supportedMp3 ? '.mp3' : '.ogg')); - notificator.volume = silent ? 0.01 : 1; - notificator.play(); + /** + * Used with SoundNotification setting + */ + playNotification(force, silent) { + if (force || this.notifications()) { + if ('running' == audioCtx.state && (this.supportedMp3 || this.supportedOgg)) { + notificator = notificator || createNewObject(); + if (notificator) { + notificator.src = Links.staticLink('sounds/' + + SettingsGet('NotificationSound') + + (this.supportedMp3 ? '.mp3' : '.ogg')); + notificator.volume = silent ? 0.01 : 1; + notificator.play(); + } + } else { + console.log('No audio: ' + audioCtx.state); } - } else { - console.log('No audio: ' + audioCtx.state); } } }; diff --git a/dev/Settings/User/General.js b/dev/Settings/User/General.js index caedc7301..e6114fdbf 100644 --- a/dev/Settings/User/General.js +++ b/dev/Settings/User/General.js @@ -1,5 +1,6 @@ import ko from 'ko'; +import { SMAudio } from 'Common/Audio'; import { SaveSettingsStep } from 'Common/Enums'; import { EditorDefaultType, Layout } from 'Common/EnumsUser'; import { Settings, SettingsGet } from 'Common/Globals'; @@ -35,12 +36,12 @@ export class UserSettingsGeneral extends AbstractViewSettings { this.editorDefaultType = SettingsUserStore.editorDefaultType; this.layout = SettingsUserStore.layout; - this.soundNotification = NotificationUserStore.enableSoundNotification; + this.soundNotification = SMAudio.notifications; this.notificationSound = ko.observable(SettingsGet('NotificationSound')); this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds')); - this.desktopNotification = NotificationUserStore.enableDesktopNotification; - this.isDesktopNotificationAllowed = NotificationUserStore.isDesktopNotificationAllowed; + this.desktopNotification = NotificationUserStore.enabled; + this.isDesktopNotificationAllowed = NotificationUserStore.allowed; this.viewHTML = SettingsUserStore.viewHTML; this.showImages = SettingsUserStore.showImages; @@ -133,11 +134,11 @@ export class UserSettingsGeneral extends AbstractViewSettings { } testSoundNotification() { - NotificationUserStore.playSoundNotification(true); + SMAudio.playNotification(true); } testSystemNotification() { - NotificationUserStore.displayDesktopNotification('SnappyMail', 'Test notification'); + NotificationUserStore.display('SnappyMail', 'Test notification'); } selectLanguage() { diff --git a/dev/Stores/User/Messagelist.js b/dev/Stores/User/Messagelist.js index 68c535360..b58b29454 100644 --- a/dev/Stores/User/Messagelist.js +++ b/dev/Stores/User/Messagelist.js @@ -1,5 +1,6 @@ import { koComputable } from 'External/ko'; +import { SMAudio } from 'Common/Audio'; import { Notification } from 'Common/Enums'; import { MessageSetAction } from 'Common/EnumsUser'; import { $htmlCL } from 'Common/Globals'; @@ -114,11 +115,11 @@ MessagelistUserStore.hasCheckedOrSelected = koComputable(() => MessagelistUserStore.notifyNewMessages = (folder, newMessages) => { if (getFolderInboxName() === folder && arrayLength(newMessages)) { - NotificationUserStore.playSoundNotification(); + SMAudio.playNotification(); const len = newMessages.length; if (3 < len) { - NotificationUserStore.displayDesktopNotification( + NotificationUserStore.display( AccountUserStore.email(), i18n('MESSAGE_LIST/NEW_MESSAGE_NOTIFICATION', { COUNT: len @@ -127,7 +128,7 @@ MessagelistUserStore.notifyNewMessages = (folder, newMessages) => { ); } else { newMessages.forEach(item => { - NotificationUserStore.displayDesktopNotification( + NotificationUserStore.display( EmailCollectionModel.reviveFromJson(item.From).toString(), item.subject, { Folder: item.Folder, Uid: item.Uid } diff --git a/dev/Stores/User/Notification.js b/dev/Stores/User/Notification.js index d84bf4b97..403460caa 100644 --- a/dev/Stores/User/Notification.js +++ b/dev/Stores/User/Notification.js @@ -1,4 +1,3 @@ -import { SMAudio } from 'Common/Audio'; import * as Links from 'Common/Links'; import { addObservablesTo } from 'External/ko'; import { fireEvent } from 'Common/Globals'; @@ -37,36 +36,24 @@ if (WorkerNotifications && ServiceWorkerRegistration && ServiceWorkerRegistratio export const NotificationUserStore = new class { constructor() { addObservablesTo(this, { - enableSoundNotification: false, - - enableDesktopNotification: false,/*.extend({ notify: 'always' })*/ - - isDesktopNotificationAllowed: !NotificationsDenied() + enabled: false,/*.extend({ notify: 'always' })*/ + allowed: !NotificationsDenied() }); - this.enableDesktopNotification.subscribe(value => { + this.enabled.subscribe(value => { DesktopNotifications = !!value; if (value && HTML5Notification && !NotificationsGranted()) { HTML5Notification.requestPermission(() => - this.isDesktopNotificationAllowed(!NotificationsDenied()) + this.allowed(!NotificationsDenied()) ); } }); } - /** - * Used with SoundNotification setting - */ - playSoundNotification(skipSetting) { - if (skipSetting ? true : this.enableSoundNotification()) { - SMAudio.playNotification(); - } - } - /** * Used with DesktopNotifications setting */ - displayDesktopNotification(title, text, messageData, imageSrc) { + display(title, text, messageData, imageSrc) { if (DesktopNotifications && NotificationsGranted()) { const options = { body: text,