wip: optimizations
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { STORAGE_KEY_NOTIFICATION_SETTINGS } from '../../../../core/constants';
|
||||
import { jsonStorage } from '../../../../infrastructure/persistence/json-storage.service';
|
||||
import { createDefaultNotificationSettings, type NotificationsSettings } from '../../domain/models/notification.model';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class NotificationSettingsStorageService {
|
||||
private readonly storage = jsonStorage;
|
||||
|
||||
load(): NotificationsSettings {
|
||||
const fallback = createDefaultNotificationSettings();
|
||||
const raw = localStorage.getItem(STORAGE_KEY_NOTIFICATION_SETTINGS);
|
||||
const parsed = this.storage.read<Partial<NotificationsSettings> | null>(
|
||||
STORAGE_KEY_NOTIFICATION_SETTINGS,
|
||||
null
|
||||
);
|
||||
|
||||
if (!raw) {
|
||||
if (!parsed) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Partial<NotificationsSettings>;
|
||||
|
||||
return {
|
||||
enabled: parsed.enabled ?? fallback.enabled,
|
||||
showPreview: parsed.showPreview ?? fallback.showPreview,
|
||||
respectBusyStatus: parsed.respectBusyStatus ?? fallback.respectBusyStatus,
|
||||
mutedRooms: normaliseBooleanRecord(parsed.mutedRooms),
|
||||
mutedChannels: normaliseNestedBooleanRecord(parsed.mutedChannels),
|
||||
roomBaselines: normaliseNumberRecord(parsed.roomBaselines),
|
||||
lastReadByChannel: normaliseNestedNumberRecord(parsed.lastReadByChannel)
|
||||
};
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
return {
|
||||
enabled: parsed.enabled ?? fallback.enabled,
|
||||
showPreview: parsed.showPreview ?? fallback.showPreview,
|
||||
respectBusyStatus: parsed.respectBusyStatus ?? fallback.respectBusyStatus,
|
||||
mutedRooms: normaliseBooleanRecord(parsed.mutedRooms),
|
||||
mutedChannels: normaliseNestedBooleanRecord(parsed.mutedChannels),
|
||||
roomBaselines: normaliseNumberRecord(parsed.roomBaselines),
|
||||
lastReadByChannel: normaliseNestedNumberRecord(parsed.lastReadByChannel)
|
||||
};
|
||||
}
|
||||
|
||||
save(settings: NotificationsSettings): void {
|
||||
localStorage.setItem(STORAGE_KEY_NOTIFICATION_SETTINGS, JSON.stringify(settings));
|
||||
this.storage.write(STORAGE_KEY_NOTIFICATION_SETTINGS, settings);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user