From be3f1e8f1536313513a04d4e96a3e57c9bf0121e Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 19 Sep 2023 10:40:39 +0100 Subject: [PATCH] separate notification responsability --- ...ts => notification-holder.service.spec.ts} | 8 +- .../notification-holder.service.ts | 119 ++++++++++++++++++ .../notifications-store.service.ts | 9 -- src/app/shared/header/header.page.ts | 13 +- 4 files changed, 132 insertions(+), 17 deletions(-) rename src/app/services/notification/{notifications-store.service.spec.ts => notification-holder.service.spec.ts} (50%) create mode 100644 src/app/services/notification/notification-holder.service.ts delete mode 100644 src/app/services/notification/notifications-store.service.ts diff --git a/src/app/services/notification/notifications-store.service.spec.ts b/src/app/services/notification/notification-holder.service.spec.ts similarity index 50% rename from src/app/services/notification/notifications-store.service.spec.ts rename to src/app/services/notification/notification-holder.service.spec.ts index 89c9f0edc..191e1e1a4 100644 --- a/src/app/services/notification/notifications-store.service.spec.ts +++ b/src/app/services/notification/notification-holder.service.spec.ts @@ -1,13 +1,13 @@ import { TestBed } from '@angular/core/testing'; -import { NotificationsStoreService } from './notifications-store.service'; +import { NotificationHolderService } from './notification-holder.service'; -describe('NotificationsStoreService', () => { - let service: NotificationsStoreService; +describe('NotificationHolderService', () => { + let service: NotificationHolderService; beforeEach(() => { TestBed.configureTestingModule({}); - service = TestBed.inject(NotificationsStoreService); + service = TestBed.inject(NotificationHolderService); }); it('should be created', () => { diff --git a/src/app/services/notification/notification-holder.service.ts b/src/app/services/notification/notification-holder.service.ts new file mode 100644 index 000000000..1e950eded --- /dev/null +++ b/src/app/services/notification/notification-holder.service.ts @@ -0,0 +1,119 @@ +import { Injectable } from '@angular/core'; +import { StorageService } from '../storage.service'; + + +@Injectable({ + providedIn: 'root' +}) +export class NotificationHolderService { + + notificationList = [] + + constructor( + private storageService: StorageService, + ) { + + try { + this.restore() + } catch(error) {} + + } + + restore() { + this.storageService.get("Notifications").then((store) => { + if(Array.isArray(store)) { + this.notificationList = store + } + }).catch((error) => {}) + } + + save() { + this.storageService.store("Notifications", this.notificationList) + } + + addNotification(notification) { + + const element = notification + const i = this.notificationList.length + 1 + + let notificationObject; + if (element.notification) { + + notificationObject = { + index: i, + title: element.notification.title, + Service: element.data.Service, + Object: element.data.Object, + IdObject: element.data.IdObject, + FolderId: element.data.FolderId, + body: element.notification.body, + dateInit: this.getFormatedTime(element.data.dateInit), + dateEnd: this.getFormatedTime(element.data.dateEnd), + Location: element.data.Location, + TypeAgenda: element.data.TypeAgenda, + Role: element.data.Role, + Status: element.data.Status, + read: false, + } + + } else if (element.data) { + notificationObject = { + index: i, + title: element.title, + Service: element.data.Service, + Object: element.data.Object, + IdObject: element.data.IdObject, + FolderId: element.data.FolderId, + body: element.body, + dateInit: this.getFormatedTime(element.data.dateInit), + dateEnd: this.getFormatedTime(element.data.dateEnd), + Location: element.data.Location, + TypeAgenda: element.data.TypeAgenda, + Role: element.data.Role, + Status: element.data.Status, + read: false, + } + + } else { + { + notificationObject = { + FolderId: element.FolderId, + IdObject: element.IdObject, + Location: element.Location, + Object: element.Object, + Role: element.Role, + Service: element.Service, + Status: element.Status, + TypeAgenda: element.TypeAgenda, + body: element.body, + dateEnd: element.dateEnd, + dateInit: element.dateInit, + index: element.index, + title: element.title, + read: false, + } + } + } + + this.notificationList.push(notificationObject) + this.save() + } + + + getFormatedTime(dateString) { + var date = new Date(dateString); + var hours = date.getHours() /* > 12 ? date.getHours() - 12 : date.getHours() */; + var am_pm = date.getHours() >= 12 ? "pm" : "am"; + var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); + let time = hours + ":" + minutes /* + " " + am_pm */; + return time; + } + + removeNotification(notification) { + this.notificationList = this.notificationList.filter( (e) => { + return e.index != notification.index + }) + this.save() + } + +} diff --git a/src/app/services/notification/notifications-store.service.ts b/src/app/services/notification/notifications-store.service.ts deleted file mode 100644 index b6914e848..000000000 --- a/src/app/services/notification/notifications-store.service.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Injectable } from '@angular/core'; - -@Injectable({ - providedIn: 'root' -}) -export class NotificationsStoreService { - - constructor() { } -} diff --git a/src/app/shared/header/header.page.ts b/src/app/shared/header/header.page.ts index 9c8af9e94..7cfd9bd36 100644 --- a/src/app/shared/header/header.page.ts +++ b/src/app/shared/header/header.page.ts @@ -149,14 +149,19 @@ export class HeaderPage implements OnInit { console.log('Get notification data',) - this.notificationLength = value.length; - console.log('Call notification data', this.notificationLength) + this.zone.run(() => { + this.notificationLength = value.length; + console.log('Call notification data', this.notificationLength) + }); + }).catch((error) => { if (!error) { - console.error('header storage get notification', error) - this.notificationLength = 0; + this.zone.run(() => { + console.error('header storage get notification', error) + this.notificationLength = 0; + }); } else { console.error('header storage get notification', error) }