From d0ff4dd15826681103a714c675fb9c42f0e915e2 Mon Sep 17 00:00:00 2001 From: Goddemunchies <52329758+Goddemunchies@users.noreply.github.com> Date: Mon, 4 Mar 2024 05:38:28 +0200 Subject: [PATCH] Due & issue notification issues solved --- lib/model/data.dart | 13 +++++++--- lib/services/notification_services.dart | 21 +++++++++------ lib/widgets/buildTasks.dart | 34 +++++++++++++------------ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/model/data.dart b/lib/model/data.dart index 564fa88b..5abdadea 100644 --- a/lib/model/data.dart +++ b/lib/model/data.dart @@ -154,13 +154,20 @@ class Data { if (task.status == 'pending' && task.due != null) { int notificationid = notificationService.calculateNotificationId( - task.due!, task.description, task.id); + task.due!, task.description, false, task.entry); notificationService.cancelNotification(notificationid); notificationService.sendNotification( - task.due!, task.description, task.id); + task.due!, task.description, false, task.entry); + if (task.wait != null) { + int waitNotificationId = notificationService.calculateNotificationId( + task.wait!, task.description, true, task.entry); + notificationService.cancelNotification(waitNotificationId); + notificationService.sendNotification( + task.wait!, task.description, true, task.entry); + } } else if (task.due != null) { int notificationid = notificationService.calculateNotificationId( - task.due!, task.description, task.id); + task.due!, task.description, false, task.entry); notificationService.cancelNotification(notificationid); } diff --git a/lib/services/notification_services.dart b/lib/services/notification_services.dart index db399084..1a45b06b 100644 --- a/lib/services/notification_services.dart +++ b/lib/services/notification_services.dart @@ -35,9 +35,10 @@ class NotificationService { } // Function to create a unique notification ID - int calculateNotificationId( - DateTime scheduledTime, String taskname, int? taskid) { - String combinedString = '${scheduledTime.toIso8601String()}$taskname'; + int calculateNotificationId(DateTime scheduledTime, String taskname, + bool isWait, DateTime entryTime) { + String combinedString = + '${entryTime.toIso8601String().substring(0, 19)}$taskname'; // Calculate SHA-256 hash var sha2561 = sha256.convert(utf8.encode(combinedString)); @@ -45,14 +46,15 @@ class NotificationService { // Convert the first 8 characters of the hash to an integer int notificationId = int.parse(sha2561.toString().substring(0, 8), radix: 16) % 2147483647; - if (taskid != null) { - notificationId = (notificationId + taskid) % 2147483647; + if (isWait) { + notificationId = (notificationId + 2) % 2147483647; } return notificationId; } - void sendNotification(DateTime dtb, String taskname, int? taskid) async { + void sendNotification( + DateTime dtb, String taskname, bool isWait, DateTime entryTime) async { DateTime dateTime = DateTime.now(); tz.initializeTimeZones(); if (kDebugMode) { @@ -90,13 +92,16 @@ class NotificationService { ); // Generate a unique notification ID based on the scheduled time and task name - int notificationId = calculateNotificationId(dtb, taskname, taskid); + int notificationId = + calculateNotificationId(dtb, taskname, isWait, entryTime); await _flutterLocalNotificationsPlugin .zonedSchedule( notificationId, 'Task Warrior Reminder', - 'Hey! Your task of $taskname is still pending', + isWait + ? "Hey! Don't forget your task of $taskname" + : 'Hey! Your task of $taskname is still pending', scheduledAt, notificationDetails, uiLocalNotificationDateInterpretation: diff --git a/lib/widgets/buildTasks.dart b/lib/widgets/buildTasks.dart index 92bd41cb..a96d57f6 100644 --- a/lib/widgets/buildTasks.dart +++ b/lib/widgets/buildTasks.dart @@ -192,14 +192,8 @@ class _TasksBuilderState extends State { DateTime? dtb = task.due; dtb = dtb!.add(const Duration(minutes: 1)); - NotificationService notificationService = - NotificationService(); - //Task ID is set to null when creating the notification id. - int notificationId = notificationService - .calculateNotificationId(task.due!, - task.description, null); - notificationService - .cancelNotification(notificationId); + + cancelNotification(task); if (kDebugMode) { print("Task due is $dtb"); @@ -229,14 +223,7 @@ class _TasksBuilderState extends State { dtb!.add(const Duration(minutes: 1)); //Task ID is set to null when creating the notification id. - NotificationService notificationService = - NotificationService(); - - int notificationId = notificationService - .calculateNotificationId(task.due!, - task.description, null); - notificationService - .cancelNotification(notificationId); + cancelNotification(task); if (kDebugMode) { print("Task due is $dtb"); @@ -301,4 +288,19 @@ class _TasksBuilderState extends State { ], )); } + + void cancelNotification(Task task) { + //Task ID is set to null when creating the notification id. + NotificationService notificationService = NotificationService(); + + int notificationId = notificationService.calculateNotificationId( + task.due!, task.description, false, task.entry); + notificationService.cancelNotification(notificationId); + + if (task.wait != null) { + notificationId = notificationService.calculateNotificationId( + task.wait!, task.description, true, task.entry); + notificationService.cancelNotification(notificationId); + } + } }