diff options
| author | Jonas Dellinger <jonas@dellinger.dev> | 2023-06-24 12:59:39 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-24 12:59:39 +0200 |
| commit | ef9afa8cbca29a3dd83454f44264a4682e968c59 (patch) | |
| tree | 5eb5033ba579ab6159d593cb01d8cf9b011f1942 /frontend/src/notification-service.tsx | |
| parent | 143461d59793665f9e54d05ba00b16c55dfec57f (diff) | |
| download | decky-loader-ef9afa8cbca29a3dd83454f44264a4682e968c59.tar.gz decky-loader-ef9afa8cbca29a3dd83454f44264a4682e968c59.zip | |
Add notification settings, which allows muting decky/plugin toast notifications (#479)
* Add notification settings, which allows muting decky/plugin toast notifications
* Fix typos
Diffstat (limited to 'frontend/src/notification-service.tsx')
| -rw-r--r-- | frontend/src/notification-service.tsx | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/frontend/src/notification-service.tsx b/frontend/src/notification-service.tsx new file mode 100644 index 00000000..188cebd3 --- /dev/null +++ b/frontend/src/notification-service.tsx @@ -0,0 +1,51 @@ +import { DeckyState } from './components/DeckyState'; +import { getSetting, setSetting } from './utils/settings'; + +export interface NotificationSettings { + deckyUpdates: boolean; + pluginUpdates: boolean; +} + +export const DEFAULT_NOTIFICATION_SETTINGS: NotificationSettings = { + deckyUpdates: true, + pluginUpdates: true, +}; + +/** + * A Service class for managing the notification settings + * + * It's mostly responsible for sending setting updates to the server and keeping the local state in sync. + */ +export class NotificationService { + constructor(private deckyState: DeckyState) {} + + async init() { + const notificationSettings = await getSetting<Partial<NotificationSettings>>('notificationSettings', {}); + + // Adding a fallback to the default settings to be backwards compatible if we ever add new notification settings + this.deckyState.setNotificationSettings({ + ...DEFAULT_NOTIFICATION_SETTINGS, + ...notificationSettings, + }); + } + + /** + * Sends the new notification settings to the server and persists it locally in the decky state + * + * @param notificationSettings The new notification settings + */ + async update(notificationSettings: NotificationSettings) { + await setSetting('notificationSettings', notificationSettings); + this.deckyState.setNotificationSettings(notificationSettings); + } + + /** + * For a specific event, returns true if a notification should be shown + * + * @param event The notification event that should be checked + * @returns true if the notification should be shown + */ + shouldNotify(event: keyof NotificationSettings) { + return this.deckyState.publicState().notificationSettings[event]; + } +} |
