From 1f2827680d77f940ed2f470d1e0e45f7df07f8af Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Tue, 26 Sep 2023 16:13:29 +0100 Subject: [PATCH] remove notification duplicate --- src/app/store/notification-holder.service.ts | 43 ++++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/app/store/notification-holder.service.ts b/src/app/store/notification-holder.service.ts index de7b9e922..81e0b3fca 100644 --- a/src/app/store/notification-holder.service.ts +++ b/src/app/store/notification-holder.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { StorageService } from '../services/storage.service'; - +import { v4 as uuidv4 } from 'uuid' @Injectable({ providedIn: 'root' }) @@ -8,9 +8,12 @@ export class NotificationHolderService { notificationList = [] + private _notificationList = [] + + constructor( private storageService: StorageService, - ) { + ) { try { this.restore() @@ -18,27 +21,34 @@ export class NotificationHolderService { } + + reverse() { + this.notificationList = this._notificationList.reverse() + } + restore() { this.storageService.get("Notifications").then((store) => { if(Array.isArray(store)) { - this.notificationList = store + this._notificationList = store + this.reverse() } }).catch((error) => {}) } save() { - this.storageService.store("Notifications", this.notificationList) + this.storageService.store("Notifications", this._notificationList) } addNotification(notification) { const element = notification - const i = this.notificationList.length + 1 - + const i = this._notificationList.length + 1 + let notificationObject; if (element.notification) { notificationObject = { + id: notification?.id || uuidv4(), index: i, title: element.notification.title, Service: element.data.Service, @@ -57,6 +67,7 @@ export class NotificationHolderService { } else if (element.data) { notificationObject = { + id: notification?.id || uuidv4(), index: i, title: element.title, Service: element.data.Service, @@ -76,6 +87,7 @@ export class NotificationHolderService { } else { { notificationObject = { + id: notification?.id || uuidv4(), FolderId: element.FolderId, IdObject: element.IdObject, Location: element.Location, @@ -94,8 +106,19 @@ export class NotificationHolderService { } } - this.notificationList.push(notificationObject) - this.save() + if(this.notificationExist(notificationObject)) { + this._notificationList.push(notificationObject) + this.reverse() + this.save() + } + + } + + + notificationExist(notificationObject) { + return this._notificationList.find(item => { + return item.id === notificationObject.id; + }); } @@ -109,9 +132,11 @@ export class NotificationHolderService { } removeNotification(notification) { - this.notificationList = this.notificationList.filter( (e) => { + this._notificationList = this._notificationList.filter( (e) => { return e.index != notification.index }) + + this.reverse() this.save() }