Files
ReminderOrganizer/Services/NotificationService.cs
Michi 33459bdec7 Snooze notifications, morning summary, ICS import, data folder sync
- Windows toast notifications with snooze (10 min / 1 h / tomorrow) and
  done buttons; balloon fallback; works when Windows starts the app
- Recurring entries snooze via SnoozedUntil without moving their date;
  lead reminders keep the real date
- Morning summary once a day from a chosen hour (pure DailySummary)
- iCalendar import incl. Google birthday calendar, folded lines, UTC,
  alarms, own export round trip, duplicates skipped by UID, undo
- Data folder selectable (location.json pointer), file watcher reloads
  changes from another PC (own writes ignored)
- Themed date picker calendar, WPF tray menu, arrow/Home/End/Space keys
- 90 tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:37:20 +01:00

118 lines
4.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#nullable enable
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using Microsoft.Toolkit.Uwp.Notifications;
namespace ReminderOrganizer.Services
{
/// <summary>
/// Windows toast notifications (Action Center) with snooze buttons. Falls back to the tray balloon if toasts
/// are not available (e.g. notifications turned off or an old Windows build).
/// </summary>
public class NotificationService : INotificationService
{
private readonly ILocalizationService _loc;
private string? _logoPath;
public static NotifyIcon? SharedTrayIcon { get; set; }
public NotificationService(ILocalizationService localizationService)
{
_loc = localizationService;
}
public void ShowNotification(string title, string message)
{
try
{
Builder(title, message).AddArgument("action", "open").Show();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Toast] {ex.Message}");
ShowBalloon(title, message);
}
}
public void ShowReminder(string title, string message, string taskId, bool isLead)
{
try
{
string lead = isLead ? "1" : "0";
ToastButton Snooze(string label, string minutes) => new ToastButton()
.SetContent(label)
.AddArgument("action", "snooze").AddArgument("min", minutes)
.AddArgument("id", taskId).AddArgument("lead", lead)
.SetBackgroundActivation();
Builder(title, message)
.AddArgument("action", "open").AddArgument("id", taskId)
.AddButton(Snooze(_loc.GetString("Snooze10"), "10"))
.AddButton(Snooze(_loc.GetString("Snooze60"), "60"))
.AddButton(Snooze(_loc.GetString("SnoozeTomorrow"), "tomorrow"))
.AddButton(new ToastButton()
.SetContent(_loc.GetString("ToastDone"))
.AddArgument("action", "done").AddArgument("id", taskId).AddArgument("lead", lead)
.SetBackgroundActivation())
.Show();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Toast] {ex.Message}");
ShowBalloon(title, message);
}
}
private ToastContentBuilder Builder(string title, string message)
{
var b = new ToastContentBuilder()
.AddText(string.IsNullOrWhiteSpace(title) ? "Reminder Organizer" : title);
if (!string.IsNullOrWhiteSpace(message)) b.AddText(message);
if (LogoPath() is { } logo) b.AddAppLogoOverride(new Uri(logo), ToastGenericAppLogoCrop.Default);
return b;
}
/// <summary>Toasts need the logo as a file – extract it once to the app folder.</summary>
private string? LogoPath()
{
if (_logoPath != null) return _logoPath;
try
{
var path = Path.Combine(DataService.AppDirectory, "toast-logo.png");
if (!File.Exists(path))
{
var res = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/Assets/app.png"));
if (res == null) return null;
Directory.CreateDirectory(DataService.AppDirectory);
using var fs = File.Create(path);
res.Stream.CopyTo(fs);
}
return _logoPath = path;
}
catch
{
return null;
}
}
private static void ShowBalloon(string title, string message)
{
try
{
if (SharedTrayIcon == null) return;
// ShowBalloonTip throws on empty text – tasks without description would never notify.
SharedTrayIcon.BalloonTipTitle = string.IsNullOrWhiteSpace(title) ? "Reminder Organizer" : title;
SharedTrayIcon.BalloonTipText = string.IsNullOrWhiteSpace(message) ? " " : message;
SharedTrayIcon.BalloonTipIcon = ToolTipIcon.Info;
SharedTrayIcon.ShowBalloonTip(5000);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Balloon] {ex.Message}");
}
}
}
}