import { Injectable, NgZone } from '@angular/core'; import { StorageService } from '../services/storage.service'; import { v4 as uuidv4 } from 'uuid' import { SHA1 } from 'crypto-js'; @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) } notificationObject(notification) { const element = notification let notificationObject; if (element.notification) { notificationObject = { id: notification?.id, 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, 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, title: element.title, Service: element.Service, Object: element.Object, IdObject: element.IdObject, FolderId: element.FolderId, body: element.body, dateInit: element.dateInit, dateEnd: element.dateEnd, Location: element.Location, TypeAgenda: element.TypeAgenda, Role: element.Role, Status: element.Status, read: false, } } } notificationObject.hashCode = (SHA1(notification)).toString() return notificationObject } addNotification(notification) { let notificationObject = this.notificationObject(notification) //alert("add notification") //alert(JSON.stringify(notificationObject)) 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() }) } clear() { this._notificationList = [] this.zone.run(() => { this.save() this.reverse() }) } PhoneClickNotification(ClickedNotificationObject) { //alert("PhoneClickNotification") //alert(JSON.stringify(ClickedNotificationObject)) const { hashCode, id } = this.notificationObject(ClickedNotificationObject) //alert(id) //alert(JSON.stringify(this._notificationList)) // let foundNotification = this._notificationList.find( (e) => { // return e.hashCode == hashCode // }) //if(foundNotification) { // this.removeNotification(foundNotification) //} else { console.log({ ClickedNotificationObject, list: this._notificationList }) let foundNotification = this._notificationList.find((e) => { return e.id == id }) if (foundNotification) { this.removeNotification(foundNotification) } else { console.log({ ClickedNotificationObject, list: this._notificationList }) } //} } }