import { Injectable, NgZone } from '@angular/core'; import { StorageService } from '../services/storage.service'; import { v4 as uuidv4 } from 'uuid' @Injectable({ providedIn: 'root' }) export class NotificationHolderService { notificationList = [] private _notificationList = [] constructor( private storageService: StorageService, private zone: NgZone, ) { try { this.restore() } catch(error) {} } reverse() { this.notificationList = this._notificationList.reverse() } restore() { this.storageService.get("Notifications").then((store) => { if(Array.isArray(store)) { this._notificationList = store this.reverse() } }).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 = { id: notification?.id || uuidv4(), 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 = { id: notification?.id || uuidv4(), 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 = { id: notification?.id || uuidv4(), 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, } } } if(!this.notificationExist(notificationObject)) { this._notificationList.push(notificationObject) this.zone.run(()=>{ this.reverse() this.save() }) } else { console.log('duplicate', notification, this._notificationList) } } notificationExist(notificationObject) { const notification = this._notificationList.find(item => { return item.id === notificationObject.id; }); if(notification?.id) { return true } else { return false } } 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.zone.run(()=>{ this.save() this.reverse() }) } }