F: Re-worked the toast service

This commit is contained in:
X4Lo committed 2025-04-13 03:53:29 +01:00
1 parent 2a43e9108e
commit d01913bb6d
1 file changed
+65 -29
@@ -1,5 +1,11 @@
import { Injectable } from '@angular/core';
import { ProgressAnimationType, ToastrService } from 'ngx-toastr';
import {
IndividualConfig,
ProgressAnimationType,
ToastrService,
} from 'ngx-toastr';
type ToastType = 'success' | 'error' | 'warning' | 'info';
@Injectable({
providedIn: 'root',
@@ -7,37 +13,67 @@ import { ProgressAnimationType, ToastrService } from 'ngx-toastr';
export class ToastService {
constructor(private toastr: ToastrService) {}
public show(title: string, message: string, type: string = 'success'): void {
const options = {
tapToDismiss: true,
progressBar: true,
positionClass: 'top-right',
progressAnimation: 'increasing' as ProgressAnimationType,
};
if (type == 'success') this.toastr.show(title, message, options);
else if (type == 'error') this.toastr.error(title, message, options);
else if (type == 'warning') this.toastr.warning(title, message, options);
else if (type == 'info') this.toastr.info(title, message, options);
}
public showWithOptions(
/**
* Displays a toast notification with custom options.
* @param title - The title of the toast.
* @param message - The message content of the toast.
* @param type - The type of the toast (default: 'success').
* @param extraOptions - Additional options to customize the toast.
*/
public show(
title: string,
message: string,
type: string = 'success',
extraOptions: any
type: ToastType = 'success',
extraOptions: any = {}
): void {
const options = {
tapToDismiss: true,
progressBar: true,
positionClass: 'top-right',
progressAnimation: 'increasing' as ProgressAnimationType,
...extraOptions,
};
const options = { ...this.getDefaultOptions(), ...extraOptions };
this.displayToast(type, title, message, options);
}
if (type == 'success') this.toastr.success(title, message, options);
else if (type == 'error') this.toastr.error(title, message, options);
else if (type == 'warning') this.toastr.warning(title, message, options);
else if (type == 'info') this.toastr.info(title, message, options);
/**
* Generates the default options for a toast notification.
* @returns Default options object.
*/
private getDefaultOptions(): any {
return {
tapToDismiss: true,
progressBar: false,
positionClass: 'toast-bottom-right',
progressAnimation: 'increasing' as ProgressAnimationType,
maxOpened: 7,
autoDismiss: true,
};
}
/**
* Displays a toast notification based on the type.
* @param type - The type of the toast.
* @param title - The title of the toast.
* @param message - The message content of the toast.
* @param options - The options for the toast.
*/
private displayToast(
type: ToastType,
title: string,
message: string,
options: IndividualConfig
): void {
switch (type) {
case 'success':
this.toastr.success(message, title, options);
break;
case 'error':
this.toastr.error(message, title, options);
break;
case 'warning':
this.toastr.warning(message, title, options);
break;
case 'info':
this.toastr.info(message, title, options);
break;
default:
console.warn(`Invalid toast type: ${type}. Defaulting to 'success'.`);
this.toastr.success(message, title, options);
}
}
}