Files
doneit-web/src/app/store/notification-holder.service.ts
T

208 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-09-28 09:02:15 +01:00
import { Injectable, NgZone } from '@angular/core';
2023-08-31 16:37:11 +01:00
import { StorageService } from '../services/storage.service';
2023-09-26 16:13:29 +01:00
import { v4 as uuidv4 } from 'uuid'
2023-10-19 15:03:12 +01:00
import { SHA1 } from 'crypto-js';
2023-08-31 16:37:11 +01:00
@Injectable({
providedIn: 'root'
})
export class NotificationHolderService {
notificationList = []
2023-09-26 16:13:29 +01:00
private _notificationList = []
2023-08-31 16:37:11 +01:00
constructor(
private storageService: StorageService,
2023-09-28 09:02:15 +01:00
private zone: NgZone,
2023-09-26 16:13:29 +01:00
) {
2023-08-31 16:37:11 +01:00
try {
this.restore()
2023-10-11 16:20:42 +01:00
} catch (error) { }
2023-08-31 16:37:11 +01:00
}
2023-09-26 16:13:29 +01:00
reverse() {
this.notificationList = this._notificationList.reverse()
}
2023-08-31 16:37:11 +01:00
restore() {
this.storageService.get("Notifications").then((store) => {
2023-10-11 16:20:42 +01:00
if (Array.isArray(store)) {
2023-09-26 16:13:29 +01:00
this._notificationList = store
this.reverse()
2023-08-31 16:37:11 +01:00
}
2023-10-11 16:20:42 +01:00
}).catch((error) => { })
2023-08-31 16:37:11 +01:00
}
save() {
2023-09-26 16:13:29 +01:00
this.storageService.store("Notifications", this._notificationList)
2023-08-31 16:37:11 +01:00
}
2023-10-19 15:03:12 +01:00
notificationObject(notification) {
2023-08-31 16:37:11 +01:00
const element = notification
let notificationObject;
if (element.notification) {
notificationObject = {
2023-10-19 15:03:12 +01:00
id: notification?.id,
2023-08-31 16:37:11 +01:00
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 = {
2023-10-19 15:03:12 +01:00
id: notification?.id,
2023-08-31 16:37:11 +01:00
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 = {
2023-10-19 15:03:12 +01:00
id: notification?.id,
title: element.title,
Service: element.Service,
Object: element.Object,
2023-08-31 16:37:11 +01:00
IdObject: element.IdObject,
2023-10-19 15:03:12 +01:00
FolderId: element.FolderId,
body: element.body,
dateInit: element.dateInit,
dateEnd: element.dateEnd,
2023-08-31 16:37:11 +01:00
Location: element.Location,
2023-10-19 15:03:12 +01:00
TypeAgenda: element.TypeAgenda,
2023-08-31 16:37:11 +01:00
Role: element.Role,
Status: element.Status,
read: false,
}
}
}
2023-10-19 15:03:12 +01:00
notificationObject.hashCode = (SHA1(notification)).toString()
return notificationObject
}
addNotification(notification) {
let notificationObject = this.notificationObject(notification)
//alert("add notification")
//alert(JSON.stringify(notificationObject))
2023-10-11 16:20:42 +01:00
if (!this.notificationExist(notificationObject)) {
2023-09-26 16:13:29 +01:00
this._notificationList.push(notificationObject)
2023-09-28 09:02:15 +01:00
2023-10-11 16:20:42 +01:00
this.zone.run(() => {
2023-09-28 09:02:15 +01:00
this.reverse()
this.save()
})
} else {
console.log('duplicate', notification, this._notificationList)
2023-09-26 16:13:29 +01:00
}
}
notificationExist(notificationObject) {
2023-10-11 16:20:42 +01:00
const notification = this._notificationList.find(item => {
2023-09-26 16:13:29 +01:00
return item.id === notificationObject.id;
});
2023-09-28 09:02:15 +01:00
2023-10-11 16:20:42 +01:00
if (notification?.id) {
2023-09-28 09:02:15 +01:00
return true
} else {
return false
}
2023-08-31 16:37:11 +01:00
}
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) {
2023-10-11 16:20:42 +01:00
this._notificationList = this._notificationList.filter((e) => {
2023-08-31 16:37:11 +01:00
return e.index != notification.index
})
2023-09-26 16:13:29 +01:00
2023-09-28 09:02:15 +01:00
2023-10-11 16:20:42 +01:00
this.zone.run(() => {
this.save()
this.reverse()
})
}
clear() {
this._notificationList = []
this.zone.run(() => {
2023-09-28 09:02:15 +01:00
this.save()
this.reverse()
})
2023-08-31 16:37:11 +01:00
}
2023-10-19 15:03:12 +01:00
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 })
}
//}
}
2023-08-31 16:37:11 +01:00
}