126 lines
4.1 KiB
C#
126 lines
4.1 KiB
C#
using cfs_tracker.Data;
|
|
using cfs_tracker.Models;
|
|
using cfs_tracker.Services;
|
|
using Microsoft.Maui.Storage;
|
|
using Microsoft.Toolkit.Uwp.Notifications;
|
|
using Windows.UI.Notifications;
|
|
|
|
namespace cfs_tracker.Platforms.Windows;
|
|
|
|
public sealed class WindowsNotificationScheduler : INotificationScheduler
|
|
{
|
|
private const string GroupName = "cfs_tracker_weekly";
|
|
private const string PrefVibrationEnabled = "notification_vibration_enabled";
|
|
|
|
private readonly NotificationScheduleRepository _notificationScheduleRepository;
|
|
|
|
public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository)
|
|
{
|
|
_notificationScheduleRepository = notificationScheduleRepository;
|
|
}
|
|
|
|
public Task<bool> EnsurePermissionAsync()
|
|
{
|
|
return Task.FromResult(true);
|
|
}
|
|
|
|
public Task<bool> GetVibrationEnabledAsync()
|
|
{
|
|
return Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false));
|
|
}
|
|
|
|
public Task SetVibrationEnabledAsync(bool isEnabled)
|
|
{
|
|
Preferences.Default.Set(PrefVibrationEnabled, isEnabled);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task SyncAsync()
|
|
{
|
|
try
|
|
{
|
|
CancelExisting();
|
|
|
|
var schedules = await _notificationScheduleRepository.GetAllAsync();
|
|
var notifier = ToastNotificationManagerCompat.CreateToastNotifier();
|
|
|
|
foreach (var item in schedules.Where(x => x.IsEnabled && x.WeekdayMask != 0))
|
|
{
|
|
foreach (var day in GetScheduledDays(item.WeekdayMask))
|
|
{
|
|
var when = ComputeNextOccurrence(day, item.Hour, item.Minute);
|
|
|
|
var content = new ToastContentBuilder()
|
|
.AddArgument("route", "day")
|
|
.AddText("Wie geht es dir?")
|
|
.AddText("Tippe, um deinen Status einzutragen.")
|
|
.GetToastContent();
|
|
|
|
var scheduled = new ScheduledToastNotification(content.GetXml(), when)
|
|
{
|
|
Tag = BuildTag(item.Id, day),
|
|
Group = GroupName
|
|
};
|
|
|
|
notifier.AddToSchedule(scheduled);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Windows darf hier still ins Leere laufen.
|
|
}
|
|
}
|
|
|
|
private static string BuildTag(int id, DayOfWeek day) => $"schedule-{id}-{(int)day}";
|
|
|
|
private static DateTimeOffset ComputeNextOccurrence(DayOfWeek targetDay, int hour, int minute)
|
|
{
|
|
var now = DateTimeOffset.Now;
|
|
var todayAtTime = new DateTimeOffset(
|
|
now.Year,
|
|
now.Month,
|
|
now.Day,
|
|
hour,
|
|
minute,
|
|
0,
|
|
now.Offset);
|
|
|
|
var days = ((int)targetDay - (int)now.DayOfWeek + 7) % 7;
|
|
var target = todayAtTime.AddDays(days);
|
|
|
|
if (target <= now)
|
|
target = target.AddDays(7);
|
|
|
|
return target;
|
|
}
|
|
|
|
private static void CancelExisting()
|
|
{
|
|
try
|
|
{
|
|
var notifier = ToastNotificationManagerCompat.CreateToastNotifier();
|
|
var scheduled = notifier.GetScheduledToastNotifications();
|
|
|
|
foreach (var toast in scheduled.Where(x => x.Group == GroupName).ToList())
|
|
notifier.RemoveFromSchedule(toast);
|
|
}
|
|
catch
|
|
{
|
|
// bewusst no-op
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<DayOfWeek> GetScheduledDays(int mask)
|
|
{
|
|
var flags = (NotificationWeekdays)mask;
|
|
|
|
if (flags.HasFlag(NotificationWeekdays.Monday)) yield return DayOfWeek.Monday;
|
|
if (flags.HasFlag(NotificationWeekdays.Tuesday)) yield return DayOfWeek.Tuesday;
|
|
if (flags.HasFlag(NotificationWeekdays.Wednesday)) yield return DayOfWeek.Wednesday;
|
|
if (flags.HasFlag(NotificationWeekdays.Thursday)) yield return DayOfWeek.Thursday;
|
|
if (flags.HasFlag(NotificationWeekdays.Friday)) yield return DayOfWeek.Friday;
|
|
if (flags.HasFlag(NotificationWeekdays.Saturday)) yield return DayOfWeek.Saturday;
|
|
if (flags.HasFlag(NotificationWeekdays.Sunday)) yield return DayOfWeek.Sunday;
|
|
}
|
|
} |