You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
57 lines
1.5 KiB
// Copyright © 2015 Abhishek Banthia |
|
|
|
import Cocoa |
|
import CoreLoggerKit |
|
import EventKit |
|
|
|
class EventCenter: NSObject { |
|
private static var shared = EventCenter() |
|
|
|
var eventStore: EKEventStore! |
|
|
|
var calendar: EKCalendar? |
|
|
|
var autoupdatingCalendar = NSCalendar.autoupdatingCurrent |
|
|
|
var eventsForDate: [Date: [EventInfo]] = [:] |
|
|
|
var filteredEvents: [Date: [EventInfo]] = [:] |
|
|
|
private let fetchQueue = DispatchQueue(label: "com.abhishek.fetch") |
|
|
|
@discardableResult class func sharedCenter() -> EventCenter { |
|
return shared |
|
} |
|
|
|
override init() { |
|
super.init() |
|
refetchAll() |
|
NotificationCenter.default.addObserver(self, |
|
selector: #selector(EventCenter.eventStoreDidChange(_:)), |
|
name: .EKEventStoreChanged, |
|
object: nil) |
|
} |
|
|
|
@objc func eventStoreDidChange(_: Any) { |
|
refetchAll() |
|
} |
|
|
|
deinit { |
|
// Just to be super safe |
|
NotificationCenter.default.removeObserver(self) |
|
} |
|
|
|
private func refetchAll() { |
|
Logger.info("Refetching events from the store") |
|
|
|
eventsForDate = [:] |
|
filteredEvents = [:] |
|
autoreleasepool { |
|
fetchQueue.async { |
|
// We get events for a 120 day period. |
|
// If the user uses a calendar often, this will be called frequently |
|
self.fetchEvents(-40, 80) |
|
} |
|
} |
|
} |
|
}
|
|
|