Skip to content

Commit d0ff4dd

Browse files
committed
Due & issue notification issues solved
1 parent 5cd602c commit d0ff4dd

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

lib/model/data.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,20 @@ class Data {
154154

155155
if (task.status == 'pending' && task.due != null) {
156156
int notificationid = notificationService.calculateNotificationId(
157-
task.due!, task.description, task.id);
157+
task.due!, task.description, false, task.entry);
158158
notificationService.cancelNotification(notificationid);
159159
notificationService.sendNotification(
160-
task.due!, task.description, task.id);
160+
task.due!, task.description, false, task.entry);
161+
if (task.wait != null) {
162+
int waitNotificationId = notificationService.calculateNotificationId(
163+
task.wait!, task.description, true, task.entry);
164+
notificationService.cancelNotification(waitNotificationId);
165+
notificationService.sendNotification(
166+
task.wait!, task.description, true, task.entry);
167+
}
161168
} else if (task.due != null) {
162169
int notificationid = notificationService.calculateNotificationId(
163-
task.due!, task.description, task.id);
170+
task.due!, task.description, false, task.entry);
164171

165172
notificationService.cancelNotification(notificationid);
166173
}

lib/services/notification_services.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,26 @@ class NotificationService {
3535
}
3636

3737
// Function to create a unique notification ID
38-
int calculateNotificationId(
39-
DateTime scheduledTime, String taskname, int? taskid) {
40-
String combinedString = '${scheduledTime.toIso8601String()}$taskname';
38+
int calculateNotificationId(DateTime scheduledTime, String taskname,
39+
bool isWait, DateTime entryTime) {
40+
String combinedString =
41+
'${entryTime.toIso8601String().substring(0, 19)}$taskname';
4142

4243
// Calculate SHA-256 hash
4344
var sha2561 = sha256.convert(utf8.encode(combinedString));
4445

4546
// Convert the first 8 characters of the hash to an integer
4647
int notificationId =
4748
int.parse(sha2561.toString().substring(0, 8), radix: 16) % 2147483647;
48-
if (taskid != null) {
49-
notificationId = (notificationId + taskid) % 2147483647;
49+
if (isWait) {
50+
notificationId = (notificationId + 2) % 2147483647;
5051
}
5152

5253
return notificationId;
5354
}
5455

55-
void sendNotification(DateTime dtb, String taskname, int? taskid) async {
56+
void sendNotification(
57+
DateTime dtb, String taskname, bool isWait, DateTime entryTime) async {
5658
DateTime dateTime = DateTime.now();
5759
tz.initializeTimeZones();
5860
if (kDebugMode) {
@@ -90,13 +92,16 @@ class NotificationService {
9092
);
9193

9294
// Generate a unique notification ID based on the scheduled time and task name
93-
int notificationId = calculateNotificationId(dtb, taskname, taskid);
95+
int notificationId =
96+
calculateNotificationId(dtb, taskname, isWait, entryTime);
9497

9598
await _flutterLocalNotificationsPlugin
9699
.zonedSchedule(
97100
notificationId,
98101
'Task Warrior Reminder',
99-
'Hey! Your task of $taskname is still pending',
102+
isWait
103+
? "Hey! Don't forget your task of $taskname"
104+
: 'Hey! Your task of $taskname is still pending',
100105
scheduledAt,
101106
notificationDetails,
102107
uiLocalNotificationDateInterpretation:

lib/widgets/buildTasks.dart

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,8 @@ class _TasksBuilderState extends State<TasksBuilder> {
192192
DateTime? dtb = task.due;
193193
dtb =
194194
dtb!.add(const Duration(minutes: 1));
195-
NotificationService notificationService =
196-
NotificationService();
197-
//Task ID is set to null when creating the notification id.
198-
int notificationId = notificationService
199-
.calculateNotificationId(task.due!,
200-
task.description, null);
201-
notificationService
202-
.cancelNotification(notificationId);
195+
196+
cancelNotification(task);
203197

204198
if (kDebugMode) {
205199
print("Task due is $dtb");
@@ -229,14 +223,7 @@ class _TasksBuilderState extends State<TasksBuilder> {
229223
dtb!.add(const Duration(minutes: 1));
230224

231225
//Task ID is set to null when creating the notification id.
232-
NotificationService notificationService =
233-
NotificationService();
234-
235-
int notificationId = notificationService
236-
.calculateNotificationId(task.due!,
237-
task.description, null);
238-
notificationService
239-
.cancelNotification(notificationId);
226+
cancelNotification(task);
240227

241228
if (kDebugMode) {
242229
print("Task due is $dtb");
@@ -301,4 +288,19 @@ class _TasksBuilderState extends State<TasksBuilder> {
301288
],
302289
));
303290
}
291+
292+
void cancelNotification(Task task) {
293+
//Task ID is set to null when creating the notification id.
294+
NotificationService notificationService = NotificationService();
295+
296+
int notificationId = notificationService.calculateNotificationId(
297+
task.due!, task.description, false, task.entry);
298+
notificationService.cancelNotification(notificationId);
299+
300+
if (task.wait != null) {
301+
notificationId = notificationService.calculateNotificationId(
302+
task.wait!, task.description, true, task.entry);
303+
notificationService.cancelNotification(notificationId);
304+
}
305+
}
304306
}

0 commit comments

Comments
 (0)