-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationStore.cs
More file actions
59 lines (49 loc) · 1.57 KB
/
NotificationStore.cs
File metadata and controls
59 lines (49 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections.ObjectModel;
namespace ToastDesk;
public sealed class NotificationStore
{
public ObservableCollection<AppNotification> Notifications { get; } = [];
public event EventHandler<AppNotification>? NotificationAdded;
public event EventHandler<Guid>? NotificationDismissed;
public AppNotification Add(
string title,
string message,
NotificationOrigin origin,
string? sourceAppName = null,
string? sourceAppUserModelId = null)
{
var notification = new AppNotification(title, message, origin, sourceAppName, sourceAppUserModelId);
if (System.Windows.Application.Current.Dispatcher.CheckAccess())
{
AddOnUiThread(notification);
}
else
{
System.Windows.Application.Current.Dispatcher.Invoke(() => AddOnUiThread(notification));
}
return notification;
}
public void Dismiss(Guid id)
{
var existing = Notifications.FirstOrDefault(item => item.Id == id);
if (existing is not null)
{
Notifications.Remove(existing);
}
NotificationDismissed?.Invoke(this, id);
}
public void Clear()
{
var ids = Notifications.Select(item => item.Id).ToArray();
Notifications.Clear();
foreach (var id in ids)
{
NotificationDismissed?.Invoke(this, id);
}
}
private void AddOnUiThread(AppNotification notification)
{
Notifications.Insert(0, notification);
NotificationAdded?.Invoke(this, notification);
}
}