Sources/OrgApp/ReminderLists.swift
75 lines · 3783 bytes
6 symbols in this file
1import CoreGraphics
2import EventKit
3import Foundation
4import OrgPresentation
5
6/// What the agenda reads from a reminder: `EKReminder`, or a value made in tests.
7public protocol ReminderRecord {
8 var recordID: String { get }
9 var recordTitle: String { get }
10 var recordDue: DateComponents? { get }
11 var recordListID: String { get }
12 var recordListTitle: String { get }
13 var recordListColor: CGColor? { get }
14}
15
16extension EKReminder: ReminderRecord {
17 public var recordID: String { calendarItemIdentifier }
18 public var recordTitle: String { title ?? "" }
19 public var recordDue: DateComponents? { dueDateComponents }
20 public var recordListID: String { calendar?.calendarIdentifier ?? "" }
21 public var recordListTitle: String { calendar?.title ?? "" }
22 public var recordListColor: CGColor? { calendar?.cgColor }
23}
24
25/// Read-only incomplete reminders with a due date from Reminders for the agenda
26/// (`agenda-reminders` in config.toml), from the lists `agenda-reminder-lists` names.
27public final class ReminderLists: @unchecked Sendable {
28 private let store = EKEventStore()
29
30 public init() {}
31
32 public static var isAuthorized: Bool { EKEventStore.authorizationStatus(for: .reminder) == .fullAccess }
33
34 /// Asks for access once; later calls answer with what the person chose.
35 public func requestAccess() async -> Bool {
36 if Self.isAuthorized { return true }
37 return (try? await store.requestFullAccessToReminders()) ?? false
38 }
39
40 /// The reminder lists, for choosing among them.
41 public func lists() -> [(id: String, title: String, color: RGBA?)] {
42 guard Self.isAuthorized else { return [] }
43 return store.calendars(for: .reminder).map { ($0.calendarIdentifier, $0.title, $0.cgColor.flatMap(CalendarEvents.rgba)) }
44 }
45
46 /// Incomplete reminders due before `end` in the chosen lists (all when `chosen` is empty),
47 /// by due date. Earlier ones are overdue and show today. None without access.
48 public func reminders(before end: Date, chosen: [String], calendar: Calendar = .current) async -> [AgendaEvent] {
49 guard Self.isAuthorized else { return [] }
50 let lists = store.calendars(for: .reminder).filter { CalendarEvents.chooses(id: $0.calendarIdentifier, title: $0.title, chosen: chosen) }
51 guard !lists.isEmpty else { return [] }
52 let predicate = store.predicateForIncompleteReminders(withDueDateStarting: nil, ending: end, calendars: lists)
53 let store = store
54 let found: [AgendaEvent] = await withCheckedContinuation { continuation in
55 store.fetchReminders(matching: predicate) { reminders in
56 continuation.resume(returning: (reminders ?? []).compactMap { Self.event($0, calendar: calendar) })
57 }
58 }
59 return found.sorted { $0.start < $1.start }
60 }
61
62 /// Nil without a due date.
63 public static func event(_ record: some ReminderRecord, calendar: Calendar = .current) -> AgendaEvent? {
64 guard let due = record.recordDue, due.year != nil, due.month != nil, due.day != nil else { return nil }
65 let timed = due.hour != nil
66 var parts = DateComponents(year: due.year, month: due.month, day: due.day, hour: due.hour ?? 0, minute: due.minute ?? 0)
67 parts.timeZone = due.timeZone
68 guard let date = (due.timeZone.map { zone -> Calendar in var c = calendar; c.timeZone = zone; return c } ?? calendar).date(from: parts)
69 else { return nil }
70 return AgendaEvent(
71 id: record.recordID, title: record.recordTitle, calendarID: record.recordListID, calendarTitle: record.recordListTitle,
72 color: record.recordListColor.flatMap(CalendarEvents.rgba), start: date, end: date, isAllDay: !timed, isReminder: true
73 )
74 }
75}