Skip to content

Commit

Permalink
Merge pull request #327 from Sabra-xD/dev
Browse files Browse the repository at this point in the history
Due & issue notification issues solved
  • Loading branch information
Pavel401 authored Mar 5, 2024
2 parents 5cd602c + d0ff4dd commit 3a78dd9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
13 changes: 10 additions & 3 deletions lib/model/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 13 additions & 8 deletions lib/services/notification_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ 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));

// 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) {
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 18 additions & 16 deletions lib/widgets/buildTasks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,8 @@ class _TasksBuilderState extends State<TasksBuilder> {
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");
Expand Down Expand Up @@ -229,14 +223,7 @@ class _TasksBuilderState extends State<TasksBuilder> {
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");
Expand Down Expand Up @@ -301,4 +288,19 @@ class _TasksBuilderState extends State<TasksBuilder> {
],
));
}

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);
}
}
}

0 comments on commit 3a78dd9

Please sign in to comment.