60 lines
1023 B
C#
60 lines
1023 B
C#
using WebAssembly.Data;
|
|
|
|
namespace WebAssembly.Services;
|
|
|
|
public class ToastService : IToastService
|
|
{
|
|
private readonly List<ToastModel> _toasts = [];
|
|
|
|
public void Subscribe(Action action)
|
|
{
|
|
OnChange += action;
|
|
}
|
|
|
|
public void Unsubscribe(Action action)
|
|
{
|
|
OnChange += action;
|
|
}
|
|
|
|
public void AddToast(ToastModel toast)
|
|
{
|
|
_toasts.Insert(0, toast);
|
|
|
|
NotifyDataChanged();
|
|
}
|
|
|
|
public void RemoveToast(ToastModel toast)
|
|
{
|
|
_toasts.Remove(toast);
|
|
}
|
|
|
|
public bool HasToasts()
|
|
{
|
|
return _toasts.Count > 0;
|
|
}
|
|
|
|
public List<ToastModel> GetToasts()
|
|
{
|
|
return _toasts;
|
|
}
|
|
|
|
public void AgeToasts()
|
|
{
|
|
foreach (var toast in _toasts) toast.Age++;
|
|
|
|
NotifyDataChanged();
|
|
}
|
|
|
|
public void ClearAllToasts()
|
|
{
|
|
_toasts.Clear();
|
|
NotifyDataChanged();
|
|
}
|
|
|
|
private event Action OnChange = null!;
|
|
|
|
private void NotifyDataChanged()
|
|
{
|
|
OnChange();
|
|
}
|
|
} |