using Plugin.LocalNotification; namespace WguApp.Services; public static class LocalNotificationService { private static readonly Random Rng = new(); public static async Task ScheduleNotification(string title, string description, DateTime time) { var permCheck = await PermissionsService.CheckNotificationPermissions(); if (!permCheck) { LoggerService.LogToFile("Notification Service: Notification Permission Not Granted"); return null; } var newId = Rng.Next(); var notif = new NotificationRequest { NotificationId = newId, Title = title, Description = description, Schedule = { NotifyTime = time, RepeatType = NotificationRepeat.No } }; try { await LocalNotificationCenter.Current.Show(notif); LoggerService.LogToFile($"Notification Service: {notif.NotificationId}:{notif.Title} set"); } catch (Exception e) { LoggerService.LogToFile($"Notification Service: {e.Message}"); throw; } return notif.NotificationId; } public static async Task ScheduleNotification(NotificationRequest notif) { try { await LocalNotificationCenter.Current.Show(notif); } catch (Exception e) { LoggerService.LogToFile(e.Message); throw; } return notif.NotificationId; } public static async Task UpdateNotification(int notificationId, string? title = null, string? description = null, DateTime? time = null) { var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList(); var notif = pendingNotifs.FirstOrDefault(n => n.NotificationId == notificationId); if (notif is null) return false; await CancelNotification(notif); notif.Title = title ?? notif.Title; notif.Description = description ?? notif.Description; notif.Schedule = new NotificationRequestSchedule() { NotifyTime = time ?? notif.Schedule.NotifyTime, RepeatType = NotificationRepeat.No }; await ScheduleNotification(notif); return true; } public static async Task CancelNotification(int notificationId) { var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList(); var match = pendingNotifs.FirstOrDefault(n => n.NotificationId == notificationId); if (match is null) return false; LocalNotificationCenter.Current.Cancel(notificationId); return true; } public static async Task CancelNotification(NotificationRequest notif) { var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList(); if (pendingNotifs.Contains(notif)) { notif.Cancel(); } return true; } public static async Task DoesNotificationAlreadyExist(int notificationId) { var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList(); var match = pendingNotifs.FirstOrDefault(n => n.NotificationId == notificationId); return match is not null; } }