-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReminderService.cs
More file actions
103 lines (87 loc) · 2.72 KB
/
ReminderService.cs
File metadata and controls
103 lines (87 loc) · 2.72 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
using Microsoft.UI.Xaml;
namespace NoteUI;
internal sealed class ReminderService : IDisposable
{
private readonly NotesManager _notes;
private readonly DispatcherTimer _timer;
private readonly HashSet<string> _firedReminders = [];
public event Action? ReminderFired;
public ReminderService(NotesManager notes)
{
_notes = notes;
_timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(15) };
_timer.Tick += CheckReminders;
_timer.Start();
}
private void CheckReminders(object? sender, object e)
{
var now = DateTime.Now;
var changed = false;
foreach (var note in _notes.Notes)
{
// Note-level reminder (regular notes)
if (note.ReminderAt != null && note.ReminderAt <= now && !_firedReminders.Contains(note.Id))
{
_firedReminders.Add(note.Id);
ShowToast(note.Title, "");
note.ReminderAt = null;
_notes.UpdateNoteReminder(note.Id, null);
changed = true;
}
// Task-level reminders (task lists)
if (note.NoteType != "tasklist") continue;
foreach (var task in note.Tasks)
{
if (task.ReminderAt == null) continue;
if (task.ReminderAt > now) continue;
if (_firedReminders.Contains(task.Id)) continue;
_firedReminders.Add(task.Id);
ShowToast(note.Title, task.Text);
task.ReminderAt = null;
changed = true;
}
}
if (changed)
{
_notes.Save();
ReminderFired?.Invoke();
}
}
private static void ShowToast(string noteTitle, string taskText)
{
try
{
var title = string.IsNullOrWhiteSpace(taskText) ? noteTitle : taskText;
if (title.Length > 100)
title = title[..100] + "\u2026";
var builder = new AppNotificationBuilder()
.AddText($"\ud83d\udd14 {title}")
.AddText(noteTitle)
.SetScenario(AppNotificationScenario.Reminder);
AppNotificationManager.Default.Show(builder.BuildNotification());
}
catch { }
}
public static void Initialize()
{
try
{
AppNotificationManager.Default.Register();
}
catch { }
}
public static void Shutdown()
{
try
{
AppNotificationManager.Default.Unregister();
}
catch { }
}
public void Dispose()
{
_timer.Stop();
}
}