import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class SharedService { isBtnDisabled: boolean = false; isAnyDialogOpen: boolean = false; isMobileNavbarOpen: boolean = false; isNewContactDialogOpen: boolean = false; isEditContactDialogOpen: boolean = false; isDeleteContactDialogOpen: boolean = false; isPageViewMedia: boolean = window.innerWidth <= 650; isContactViewMedia: boolean = window.innerWidth <= 800; currentUserId: string = ''; private resizeListener!: () => void; constructor() { this.initResizeListener(); } // CHECK VIEW WIDTH /** * Initializes the window resize listener. * * @remarks * This function will set up the window resize listener and bind the * onResize() method to it. */ private initResizeListener() { this.resizeListener = this.onResize.bind(this); window.addEventListener('resize', this.resizeListener); } /** * Removes the window resize listener. * * @remarks * This function will remove the window resize listener that was set up in * the constructor. This is useful when the service is being destroyed. */ private removeResizeListener() { window.removeEventListener('resize', this.resizeListener); } /** * Window resize event listener method. * * @remarks * This method will update the isPageViewMedia and isContactViewMedia * properties based on the current window width whenever the window is * resized. */ private onResize() { this.isPageViewMedia = window.innerWidth <= 650; this.isContactViewMedia = window.innerWidth <= 800; } /** * Cleans up the service by removing the window resize listener. * * @remarks * This function is useful when the service is being destroyed. */ cleanup() { this.removeResizeListener(); } // RANDOM COLOR /** * Generates a random color string. * * @remarks * The generated color is a hex string that is not a shade of gray. * The color is generated by randomly selecting a hex digit from the * string '0123456789ABCDEF' and appending it to the color string. The * loop continues until a non-gray color is generated. * * @returns A random color string. */ generateRandomColor(): string { const letters = '0123456789ABCDEF'; let color = '#'; let isGrayScale = true; while (isGrayScale) { for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } const rgb = this.hexToRgb(color); if (rgb.r !== rgb.g || rgb.g !== rgb.b) { isGrayScale = false; } else { color = '#'; } } return color; } /** * Converts a hex color string to an RGB object. * * @param hex A color string in hex format, e.g. '#FF0000'. * @returns An object with r, g, and b properties, each an integer between * 0 and 255, representing the red, green, and blue components of the color. */ hexToRgb(hex: string): { r: number; g: number; b: number } { const bigint = parseInt(hex.substring(1), 16); const r = (bigint >> 16) & 255; const g = (bigint >> 8) & 255; const b = bigint & 255; return { r, g, b }; } // SECURITY /** * Sanitizes the input string by replacing any potential cross-site scripting characters. * This ensures that the input string is safe to be stored in the Firebase Realtime Database. * @param input The input string to be sanitized. * @returns The sanitized string. */ replaceXSSChars(input: string) { return input.replace(//g, '>'); } }