diff --git a/MTMR/Widgets/UpNextBarItem.swift b/MTMR/Widgets/UpNextBarItem.swift index 943dcee..d0d3e8d 100644 --- a/MTMR/Widgets/UpNextBarItem.swift +++ b/MTMR/Widgets/UpNextBarItem.swift @@ -12,7 +12,6 @@ import EventKit class UpNextBarItem: CustomButtonTouchBarItem { private let activity: NSBackgroundActivityScheduler // Update scheduler - private let eventStore = EKEventStore() // private let df = DateFormatter() private let buttonTemplate = "🗓 %@ - %@ " @@ -22,8 +21,7 @@ class UpNextBarItem: CustomButtonTouchBarItem { private var nthEvent: Int // State - private var hasPermission: Bool = false - + private var eventSources : [IUpNextSource] = [] /// <#Description#> /// - Parameters: @@ -33,45 +31,22 @@ class UpNextBarItem: CustomButtonTouchBarItem { /// - to: Relative to current time, how far forward we search for events in hours /// - nthEvent: Which event to show (1 is first, 2 is second, and so on) init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: Double, to: Double, nthEvent: Int) { + // Initialise member properties activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updateCheck") - activity.interval = interval - self.pastSearchCutoff = from - self.futureSearchCutoff = to + pastSearchCutoff = from * 360 + futureSearchCutoff = to * 360 self.nthEvent = nthEvent - self.df.dateFormat = "HH:mm" - + df.dateFormat = "HH:mm" + // Error handling if (nthEvent <= 0) { fatalError("Error on UpNext bar item. nthEvent property must be greater than 0.") } - + // Init super super.init(identifier: identifier, title: " ") - let authorizationStatus = EKEventStore.authorizationStatus(for: EKEntityType.event) - switch authorizationStatus { - case .notDetermined: - print("notDetermined") - case .restricted: - print("restricted") - case .denied: - print("denied") - case .authorized: - print("authorizded") - default: - print("Unkown EKEventStore authorization status") - } - eventStore.requestAccess(to: .event){ granted, error in - self.hasPermission = granted; - if(!granted) { - NSLog("Error: MTMR UpNextBarWidget not given calendar access.") - return - } - self.updateView() - } - - - tapClosure = { [weak self] in self?.gotoAppleCalendar() } - - + // Add event sources + self.eventSources.append(UpNextCalenderSource()) // Start activity to update view + activity.interval = interval activity.repeats = true activity.qualityOfService = .utility activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in @@ -79,7 +54,6 @@ class UpNextBarItem: CustomButtonTouchBarItem { completion(NSBackgroundActivityScheduler.Result.finished) } updateView() - } required init?(coder _: NSCoder) { @@ -87,10 +61,6 @@ class UpNextBarItem: CustomButtonTouchBarItem { } func updateView() { - if (!self.hasPermission) { - self.title = "🗓 No permissions" - return - } var upcomingEvents = self.getUpcomingEvents() upcomingEvents.sort(by: {$0.startDate.compare($1.startDate) == .orderedAscending}) for event in upcomingEvents { @@ -102,34 +72,71 @@ class UpNextBarItem: CustomButtonTouchBarItem { let startDateString = self.df.string(for: event.startDate) print("TITLE: " + title + " STARTDATE: " + (startDateString ?? "nil")) - DispatchQueue.main.async { - print("SHOW") - self.image = nil - self.title = String(format: self.buttonTemplate, title, startDateString ?? "No time") - self.view.isHidden = false - } + print("SHOW") + self.image = nil + self.title = String(format: self.buttonTemplate, title, startDateString ?? "No time") +// self.view.widthAnchor.constraint(lessThanOrEqualToConstant: 1000 as CGFloat).isActive = true } else { // Do not display any event - DispatchQueue.main.async { - print("HIDE " + String(upcomingEvents.count) + " " + String(self.nthEvent) + " " + String(upcomingEvents.count > self.nthEvent)) - self.image = nil - self.title = "" - self.view.isHidden = true - } + print("HIDE " + String(upcomingEvents.count) + " " + String(self.nthEvent) + " " + String(upcomingEvents.count > self.nthEvent)) + self.image = nil + self.title = "" +// self.setWidth(value: 0) } } func getUpcomingEvents() -> [UpNextEventModel] { var upcomingEvents: [UpNextEventModel] = [] - NSLog("Getting calendar events...") // Calculate the range we're going to search for events in - let dateLowerBounds = Date(timeIntervalSinceNow: self.pastSearchCutoff * 360) - let dateUpperBounds = Date(timeIntervalSinceNow: self.futureSearchCutoff * 360) + let dateLowerBounds = Date(timeIntervalSinceNow: self.pastSearchCutoff) + let dateUpperBounds = Date(timeIntervalSinceNow: self.futureSearchCutoff) + // Get all events from all sources + for eventSource in self.eventSources { + if (eventSource.hasPermission) { + let events = eventSource.getUpcomingEvents(dateLowerBounds: dateLowerBounds, dateUpperBounds: dateUpperBounds) + upcomingEvents.append(contentsOf: events) + } + } + + return upcomingEvents + } + + func gotoAppleCalendar() { + print("CLICK") + NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Calendar.app")) + } +} +// Model for events to be displayed in dock +struct UpNextEventModel { + var title: String + var startDate: Date +} +// Interface for any event source +protocol IUpNextSource { + var hasPermission : Bool { get } + init() + func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] +} + +class UpNextCalenderSource : IUpNextSource { + public var hasPermission: Bool = false + private let eventStore = EKEventStore() // + required init() { + eventStore.requestAccess(to: .event){ granted, error in + self.hasPermission = granted; + if(!granted) { + NSLog("Error: MTMR UpNextBarWidget not given calendar access.") + return + } + } + } + public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] { + NSLog("Getting calendar events...") + var upcomingEvents: [UpNextEventModel] = [] let calendars = self.eventStore.calendars(for: .event) - for calendar in calendars { let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: [calendar]) @@ -143,13 +150,4 @@ class UpNextBarItem: CustomButtonTouchBarItem { print("Found " + String(upcomingEvents.count) + " events.") return upcomingEvents } - - func gotoAppleCalendar() { - print("CLICK") - NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Photos.app")) - }} - -struct UpNextEventModel { - var title: String - var startDate: Date }