Options fix
clean up and fixes - Heute gefixt - Code Cleanup NotificationScheduleItemViewModel -fixed NotificationScheduleItemViewModel for Windows compat push implementation Push fixes Push benachrichtungen eingebaut - Status bei push - Push auch in background - Toggle für symtome angepasst push system change exporter update Optionen fix - Optionen aufgeteilt auf 3 tabs - Anpassungen haben jetzt Farbslider Optimierung fix
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace cfs_tracker.WinUI;
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
using cfs_tracker.Data;
|
||||
using cfs_tracker.Models;
|
||||
using cfs_tracker.Services;
|
||||
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 readonly NotificationScheduleRepository _notificationScheduleRepository;
|
||||
|
||||
public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository)
|
||||
{
|
||||
_notificationScheduleRepository = notificationScheduleRepository;
|
||||
}
|
||||
|
||||
public Task<bool> EnsurePermissionAsync()
|
||||
{
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user