SPB Git

Repository Public

ReminderScheduler.swift 2.8 KB 59 lines swift

1// SP Notes iOS2// File: Core/ReminderScheduler.swift3// Description: Local notifications for note reminders. Permission is requested only when the first reminder is set. The reminder4//              itself lives in the store (and syncs as a task); this type only mirrors it into the notification centre.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPNotesCore10import UserNotifications1112@MainActor13final class ReminderScheduler {14    private var center: UNUserNotificationCenter { .current() }15    static let noteIdKey = "noteId"1617    /// Asks for permission if it was never asked. Returns false when notifications are refused (the reminder is still saved).18    func ensurePermission() async -> Bool {19        let settings = await center.notificationSettings()20        switch settings.authorizationStatus {21        case .authorized, .provisional, .ephemeral: return true22        case .denied: return false23        case .notDetermined: return (try? await center.requestAuthorization(options: [.alert, .sound, .badge])) ?? false24        @unknown default: return false25        }26    }2728    func schedule(_ reminder: ReminderRecord, noteTitle: String) async {29        cancel(reminderId: reminder.id)30        guard let due = reminder.dueAt, !reminder.done, !reminder.deleted, due > Date() else { return }31        let content = UNMutableNotificationContent()32        content.title = reminder.title.isEmpty ? noteTitle : reminder.title33        if !reminder.title.isEmpty, reminder.title != noteTitle { content.subtitle = noteTitle }34        content.sound = .default35        if let pageId = reminder.pageId { content.userInfo = [Self.noteIdKey: pageId]; content.threadIdentifier = pageId }36        let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: due)37        let request = UNNotificationRequest(identifier: Self.identifier(reminder.id), content: content, trigger: UNCalendarNotificationTrigger(dateMatching: components, repeats: false))38        try? await center.add(request)39    }4041    func cancel(reminderId: String) {42        center.removePendingNotificationRequests(withIdentifiers: [Self.identifier(reminderId)])43        center.removeDeliveredNotifications(withIdentifiers: [Self.identifier(reminderId)])44    }4546    /// Removes the pending notifications of a note that went to the trash.47    func cancel(forNote noteId: String) {48        Task {49            let requests = await center.pendingNotificationRequests()50            let ids = requests.filter { ($0.content.userInfo[Self.noteIdKey] as? String) == noteId }.map(\.identifier)51            center.removePendingNotificationRequests(withIdentifiers: ids)52        }53    }5455    func cancelAll() { center.removeAllPendingNotificationRequests() }5657    static func identifier(_ reminderId: String) -> String { "reminder-" + reminderId }58}59
Files main
Menu

Theme