-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleDelegate.swift
57 lines (44 loc) · 2.25 KB
/
AppleDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
let toDoList = ToDoList()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
func applicationWillTerminate(_ application: UIApplication) {
// Save the to-do list data here
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Present the notification here
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Handle the user's response to the notification here
}
func scheduleReminder(for item: ToDoItem) {
let content = UNMutableNotificationContent()
content.title = "Reminder: \(item.title)"
content.body = "Due today: \(item.dueDate)"
content.sound = .default
let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: item.dueDate), repeats: false)
let requestIdentifier = "toDoItem-\(item.title)"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Error scheduling reminder for \(item.title): \(error.localizedDescription)")
}
}
}
func addItem(title: String, dueDate: Date) {
toDoList.addItem(title: title, dueDate: dueDate)
scheduleReminder(for: toDoList.items.last!)
}
func markItemCompleted(at index: Int) {
toDoList.markItemCompleted(at: index)
}
func getItemsWithinDays(days: Int) -> [ToDoItem] {
return toDoList.getItemsWithinDays(days: days)
}
}