Initial Commit

This commit is contained in:
aminecmi
2024-07-11 12:38:11 +02:00
parent dc63e0c91e
commit 7aa8fab7f5
20 changed files with 1127 additions and 0 deletions

View File

@ -0,0 +1,44 @@
//
// NotificationDelegate.swift
// Screaming Reminder
//
// Created by Amine Bou on 12/07/2024.
//
import Foundation
import SwiftUI
import SwiftData
class NotificationDelegate: NSObject , UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler:
@escaping () -> Void) {
let title = response.notification.request.content.title
switch response.actionIdentifier {
case "DONE_ACTION":
let content = UNMutableNotificationContent()
content.title = "Bravo !"
content.subtitle = "Tu as fais ce que tu devais faire ! (Il faudra ignorer les autres notifications 😅)"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
// choose a random identifier
let request = UNNotificationRequest(identifier: "\(title)-done", content: content, trigger: trigger)
// add our reminder request
UNUserNotificationCenter.current().add(request)
break
default:
exit(0)
break
}
// Always call the completion handler when done.
completionHandler()
}
}

View File

@ -0,0 +1,35 @@
//
// NotificationsUtils.swift
// Screaming Reminder
//
// Created by Amine Bou on 12/07/2024.
//
import Foundation
import SwiftUI
import SwiftData
func scheduleNotifications(reminder: Reminder) {
let notification = Notification(reminder: reminder)
notification.triggers.forEach {
let content = UNMutableNotificationContent()
content.title = notification.reminder.label
content.sound = UNNotificationSound.defaultCritical
content.categoryIdentifier = "CAT"
let request = UNNotificationRequest(identifier: getIdentifier(reminder: reminder, trigger: $0), content: content, trigger: $0)
// add our reminder request
UNUserNotificationCenter.current().add(request)
}
}
func cancelNotifications(reminder: Reminder) {
let identifiers = getTriggersForReminder(reminder: reminder).map {
getIdentifier(reminder: reminder, trigger: $0)
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
}