Put sound notifications in SMAudio

This commit is contained in:
the-djmaze 2022-06-07 18:46:06 +02:00
parent 78a3b1a5be
commit df80ff9533
5 changed files with 40 additions and 40 deletions

View file

@ -1,5 +1,6 @@
import 'External/User/ko'; import 'External/User/ko';
import { SMAudio } from 'Common/Audio';
import { isArray, pString, changeTheme } from 'Common/Utils'; import { isArray, pString, changeTheme } from 'Common/Utils';
import { mailToHelper, setLayoutResizer, dropdownsDetectVisibility } from 'Common/UtilsUser'; import { mailToHelper, setLayoutResizer, dropdownsDetectVisibility } from 'Common/UtilsUser';
@ -289,8 +290,8 @@ export class AppUser extends AbstractApp {
if (SettingsGet('Auth')) { if (SettingsGet('Auth')) {
rl.setWindowTitle(i18n('GLOBAL/LOADING')); rl.setWindowTitle(i18n('GLOBAL/LOADING'));
NotificationUserStore.enableSoundNotification(!!SettingsGet('SoundNotification')); SMAudio.notifications(!!SettingsGet('SoundNotification'));
NotificationUserStore.enableDesktopNotification(!!SettingsGet('DesktopNotifications')); NotificationUserStore.enabled(!!SettingsGet('DesktopNotifications'));
AccountUserStore.email(SettingsGet('Email')); AccountUserStore.email(SettingsGet('Email'));

View file

@ -1,5 +1,6 @@
import * as Links from 'Common/Links'; import * as Links from 'Common/Links';
import { doc, SettingsGet, fireEvent, addEventsListener } from 'Common/Globals'; import { doc, SettingsGet, fireEvent, addEventsListener } from 'Common/Globals';
import { addObservablesTo } from 'External/ko';
let notificator = null, let notificator = null,
player = null, player = null,
@ -50,7 +51,7 @@ let notificator = null,
audioCtx.resume(); audioCtx.resume();
} }
unlockEvents.forEach(type => doc.removeEventListener(type, unlock, true)); unlockEvents.forEach(type => doc.removeEventListener(type, unlock, true));
// setTimeout(()=>Audio.playNotification(1),1); // setTimeout(()=>SMAudio.playNotification(0,1),1);
}; };
if (audioCtx) { if (audioCtx) {
@ -76,6 +77,10 @@ export const SMAudio = new class {
addEventsListener(player, ['ended','error'], stopFn); addEventsListener(player, ['ended','error'], stopFn);
addEventListener('audio.api.stop', stopFn); addEventListener('audio.api.stop', stopFn);
} }
addObservablesTo(this, {
notifications: false
});
} }
paused() { paused() {
@ -103,7 +108,11 @@ export const SMAudio = new class {
this.supportedWav && play(url, name); this.supportedWav && play(url, name);
} }
playNotification(silent) { /**
* Used with SoundNotification setting
*/
playNotification(force, silent) {
if (force || this.notifications()) {
if ('running' == audioCtx.state && (this.supportedMp3 || this.supportedOgg)) { if ('running' == audioCtx.state && (this.supportedMp3 || this.supportedOgg)) {
notificator = notificator || createNewObject(); notificator = notificator || createNewObject();
if (notificator) { if (notificator) {
@ -117,4 +126,5 @@ export const SMAudio = new class {
console.log('No audio: ' + audioCtx.state); console.log('No audio: ' + audioCtx.state);
} }
} }
}
}; };

View file

@ -1,5 +1,6 @@
import ko from 'ko'; import ko from 'ko';
import { SMAudio } from 'Common/Audio';
import { SaveSettingsStep } from 'Common/Enums'; import { SaveSettingsStep } from 'Common/Enums';
import { EditorDefaultType, Layout } from 'Common/EnumsUser'; import { EditorDefaultType, Layout } from 'Common/EnumsUser';
import { Settings, SettingsGet } from 'Common/Globals'; import { Settings, SettingsGet } from 'Common/Globals';
@ -35,12 +36,12 @@ export class UserSettingsGeneral extends AbstractViewSettings {
this.editorDefaultType = SettingsUserStore.editorDefaultType; this.editorDefaultType = SettingsUserStore.editorDefaultType;
this.layout = SettingsUserStore.layout; this.layout = SettingsUserStore.layout;
this.soundNotification = NotificationUserStore.enableSoundNotification; this.soundNotification = SMAudio.notifications;
this.notificationSound = ko.observable(SettingsGet('NotificationSound')); this.notificationSound = ko.observable(SettingsGet('NotificationSound'));
this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds')); this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds'));
this.desktopNotification = NotificationUserStore.enableDesktopNotification; this.desktopNotification = NotificationUserStore.enabled;
this.isDesktopNotificationAllowed = NotificationUserStore.isDesktopNotificationAllowed; this.isDesktopNotificationAllowed = NotificationUserStore.allowed;
this.viewHTML = SettingsUserStore.viewHTML; this.viewHTML = SettingsUserStore.viewHTML;
this.showImages = SettingsUserStore.showImages; this.showImages = SettingsUserStore.showImages;
@ -133,11 +134,11 @@ export class UserSettingsGeneral extends AbstractViewSettings {
} }
testSoundNotification() { testSoundNotification() {
NotificationUserStore.playSoundNotification(true); SMAudio.playNotification(true);
} }
testSystemNotification() { testSystemNotification() {
NotificationUserStore.displayDesktopNotification('SnappyMail', 'Test notification'); NotificationUserStore.display('SnappyMail', 'Test notification');
} }
selectLanguage() { selectLanguage() {

View file

@ -1,5 +1,6 @@
import { koComputable } from 'External/ko'; import { koComputable } from 'External/ko';
import { SMAudio } from 'Common/Audio';
import { Notification } from 'Common/Enums'; import { Notification } from 'Common/Enums';
import { MessageSetAction } from 'Common/EnumsUser'; import { MessageSetAction } from 'Common/EnumsUser';
import { $htmlCL } from 'Common/Globals'; import { $htmlCL } from 'Common/Globals';
@ -114,11 +115,11 @@ MessagelistUserStore.hasCheckedOrSelected = koComputable(() =>
MessagelistUserStore.notifyNewMessages = (folder, newMessages) => { MessagelistUserStore.notifyNewMessages = (folder, newMessages) => {
if (getFolderInboxName() === folder && arrayLength(newMessages)) { if (getFolderInboxName() === folder && arrayLength(newMessages)) {
NotificationUserStore.playSoundNotification(); SMAudio.playNotification();
const len = newMessages.length; const len = newMessages.length;
if (3 < len) { if (3 < len) {
NotificationUserStore.displayDesktopNotification( NotificationUserStore.display(
AccountUserStore.email(), AccountUserStore.email(),
i18n('MESSAGE_LIST/NEW_MESSAGE_NOTIFICATION', { i18n('MESSAGE_LIST/NEW_MESSAGE_NOTIFICATION', {
COUNT: len COUNT: len
@ -127,7 +128,7 @@ MessagelistUserStore.notifyNewMessages = (folder, newMessages) => {
); );
} else { } else {
newMessages.forEach(item => { newMessages.forEach(item => {
NotificationUserStore.displayDesktopNotification( NotificationUserStore.display(
EmailCollectionModel.reviveFromJson(item.From).toString(), EmailCollectionModel.reviveFromJson(item.From).toString(),
item.subject, item.subject,
{ Folder: item.Folder, Uid: item.Uid } { Folder: item.Folder, Uid: item.Uid }

View file

@ -1,4 +1,3 @@
import { SMAudio } from 'Common/Audio';
import * as Links from 'Common/Links'; import * as Links from 'Common/Links';
import { addObservablesTo } from 'External/ko'; import { addObservablesTo } from 'External/ko';
import { fireEvent } from 'Common/Globals'; import { fireEvent } from 'Common/Globals';
@ -37,36 +36,24 @@ if (WorkerNotifications && ServiceWorkerRegistration && ServiceWorkerRegistratio
export const NotificationUserStore = new class { export const NotificationUserStore = new class {
constructor() { constructor() {
addObservablesTo(this, { addObservablesTo(this, {
enableSoundNotification: false, enabled: false,/*.extend({ notify: 'always' })*/
allowed: !NotificationsDenied()
enableDesktopNotification: false,/*.extend({ notify: 'always' })*/
isDesktopNotificationAllowed: !NotificationsDenied()
}); });
this.enableDesktopNotification.subscribe(value => { this.enabled.subscribe(value => {
DesktopNotifications = !!value; DesktopNotifications = !!value;
if (value && HTML5Notification && !NotificationsGranted()) { if (value && HTML5Notification && !NotificationsGranted()) {
HTML5Notification.requestPermission(() => 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 * Used with DesktopNotifications setting
*/ */
displayDesktopNotification(title, text, messageData, imageSrc) { display(title, text, messageData, imageSrc) {
if (DesktopNotifications && NotificationsGranted()) { if (DesktopNotifications && NotificationsGranted()) {
const options = { const options = {
body: text, body: text,