-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IRI 3104 - notification manager (#117)
* feat: notification manager implementation
- v10.0.0
- v9.0.1
- v9.0.0
- v8.0.3
- v8.0.2
- v8.0.1
- v8.0.0
- v7.0.1
- v7.0.0
- v6.0.0
- v5.2.0
- v5.1.0
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v4.0.1
- v4.0.0
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-beta.0
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.1
- v1.1.0
- v1.0.0
- v0.4.46
- v0.4.45
- v0.4.44
- v0.4.43
- v0.4.42
- v0.4.41
- v0.4.40
- v0.4.39
- v0.4.38
- v0.4.37
- v0.4.36
- v0.4.35
- v0.4.34
Showing
12 changed files
with
231 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { debounce, noop } from 'lodash'; | ||
import { | ||
NotificationConfig, | ||
PopupNotificationConfig, | ||
AudioNotificationConfig, | ||
INotificationManager | ||
} from '../../types/notification'; | ||
import defaultAudio from '../../assets/notification.mp3'; | ||
import defaultIcon from '../../assets/carbonio-logo.png'; | ||
|
||
const PopupNotificationDefaultConfig = { | ||
title: 'Carbonio client', | ||
icon: defaultIcon, | ||
vibrate: [200, 100, 200] | ||
}; | ||
|
||
const AudioNotificationDefaultConfig = { | ||
sound: defaultAudio | ||
}; | ||
|
||
const NotificationDefaultConfig: NotificationConfig = { | ||
...PopupNotificationDefaultConfig, | ||
...AudioNotificationDefaultConfig, | ||
showPopup: true, | ||
playSound: false | ||
}; | ||
|
||
/** | ||
* The main goals of the NotificationManager are: | ||
* - to provide a single and rich implementation for all the Carbonio modules, | ||
* reducing the boilerplate code needed to send a notification to the user | ||
* - to optimize the audio notifications avoiding to spam the same sound file | ||
* in a short period of time | ||
* - to act as a collector for all the notification (for possible future | ||
* implementations) | ||
* | ||
* In order to reduce the effort needed to send a notification the class | ||
* provided a set of default values/assets (e.g. icon, sound, title, ...) | ||
* | ||
* The class is provided as a singleton | ||
*/ | ||
export class NotificationManager implements INotificationManager { | ||
private static instance: NotificationManager; | ||
|
||
/** | ||
* Minimum time (ms) to wait before the same audio file will be played | ||
* @private | ||
*/ | ||
private static DEBOUNCE_TIME = 1000; | ||
|
||
/** | ||
* Map of functions to play a specific audio file | ||
* @private | ||
*/ | ||
private functions = new Map<string, () => void>(); | ||
|
||
/** | ||
* Gets or creates the (debounced) function to play the audio file | ||
* @param sound - relative path to the audio file to play | ||
*/ | ||
private getAudioFileFunction = (sound: string): (() => void) => { | ||
if (!this.functions.has(sound)) { | ||
this.functions.set( | ||
sound, | ||
debounce(() => { | ||
new Audio(sound).play().then(); | ||
this.functions.delete(sound); | ||
}, NotificationManager.DEBOUNCE_TIME) | ||
); | ||
} | ||
const result = this.functions.get(sound); | ||
return result ?? noop; | ||
}; | ||
|
||
/** | ||
* Executes the debounced function to play the audio file | ||
* @param config - Configuration for the audio notification. In case of | ||
* missing properties default values are used | ||
*/ | ||
public playSound = (config: AudioNotificationConfig): void => { | ||
const defConfig = { | ||
...AudioNotificationDefaultConfig, | ||
...config | ||
}; | ||
if (!defConfig.sound) { | ||
return; | ||
} | ||
|
||
this.getAudioFileFunction(defConfig.sound)(); | ||
}; | ||
|
||
/** | ||
* Shows a popup notification | ||
* @param config - Configuration for the popup notification. In case of | ||
* missing properties default values are used | ||
*/ | ||
public showPopup = (config: PopupNotificationConfig): void => { | ||
const defConfig = { | ||
...PopupNotificationDefaultConfig, | ||
...config | ||
}; | ||
|
||
const n = new Notification(defConfig.title, { | ||
body: defConfig.message, | ||
vibrate: defConfig.vibrate, | ||
icon: defConfig.icon, | ||
tag: defConfig?.tag | ||
}); | ||
|
||
if (defConfig.onClick) { | ||
n.addEventListener('click', defConfig.onClick); | ||
} | ||
}; | ||
|
||
/** | ||
* Sends a popup/audio notification to the user | ||
* @param config - Configuration for the notification. In case of | ||
* missing properties default values are used | ||
*/ | ||
public notify = (config: NotificationConfig): void => { | ||
const defConfig = { | ||
...NotificationDefaultConfig, | ||
...config | ||
}; | ||
|
||
if (defConfig?.showPopup) { | ||
this.showPopup(defConfig); | ||
} | ||
|
||
if (defConfig?.playSound) { | ||
this.playSound(defConfig); | ||
} | ||
}; | ||
|
||
/** | ||
* Sends multiple notifications | ||
* @param config - Array of configurations for the notifications. In case of | ||
* missing properties default values are used | ||
*/ | ||
public multipleNotify = (config: NotificationConfig[]): void => { | ||
config.forEach((conf) => this.notify(conf)); | ||
}; | ||
|
||
/** | ||
* Return the singleton instance | ||
*/ | ||
public static getInstance(): NotificationManager { | ||
if (!NotificationManager.instance) { | ||
NotificationManager.instance = new NotificationManager(); | ||
} | ||
|
||
return NotificationManager.instance; | ||
} | ||
} | ||
|
||
export const getNotificationManager = (): INotificationManager => NotificationManager.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import React, { FC, ReactElement, useEffect } from 'react'; | ||
|
||
export const NotificationPermissionChecker: FC = (): ReactElement => { | ||
useEffect(() => { | ||
if (!('Notification' in window)) { | ||
// eslint-disable-next-line no-console | ||
console.warn('This browser does not support desktop notifications'); | ||
} else { | ||
Notification.requestPermission(); | ||
} | ||
}, []); | ||
|
||
return <></>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export type PopupNotificationConfig = { | ||
title?: string; | ||
message?: string; | ||
icon?: string; | ||
vibrate?: Array<number>; | ||
tag?: string; | ||
onClick?: (event: Event) => void; | ||
}; | ||
|
||
export type AudioNotificationConfig = { | ||
sound?: string; | ||
}; | ||
|
||
export type NotificationConfig = { | ||
showPopup?: boolean; | ||
playSound?: boolean; | ||
} & PopupNotificationConfig & | ||
AudioNotificationConfig; | ||
|
||
export interface INotificationManager { | ||
playSound: (config: AudioNotificationConfig) => void; | ||
showPopup: (config: PopupNotificationConfig) => void; | ||
notify: (config: NotificationConfig) => void; | ||
multipleNotify: (config: NotificationConfig[]) => void; | ||
} |