From 2a35710d796f425a52f70889fe5cf1d814ba77f7 Mon Sep 17 00:00:00 2001 From: connorgmeehan Date: Sun, 26 Jul 2020 20:06:05 +1000 Subject: [PATCH] Added EKEventStore listener to reduce perfomance impact --- MTMR/ItemsParsing.swift | 1 + MTMR/TouchBarController.swift | 2 +- MTMR/Widgets/UpNextScrubberTouchBarItem.swift | 38 +++++++++++-------- README.md | 2 +- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 49dc220..a16beef 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -387,6 +387,7 @@ enum ItemType: Decodable { let to = try container.decodeIfPresent(Double.self, forKey: .to) ?? 12 // Upper bounds of period of time in hours to search for events let maxToShow = try container.decodeIfPresent(Int.self, forKey: .maxToShow) ?? 3 // 1 indexed array. Get the 1st, 2nd, 3rd event to display multiple notifications let autoResize = try container.decodeIfPresent(Bool.self, forKey: .autoResize) ?? false + let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 60.0 self = .upnext(from: from, to: to, maxToShow: maxToShow, autoResize: autoResize) } } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 53ec8a7..e3a5898 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -304,7 +304,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { case let .swipe(direction: direction, fingers: fingers, minOffset: minOffset, sourceApple: sourceApple, sourceBash: sourceBash): barItem = SwipeItem(identifier: identifier, direction: direction, fingers: fingers, minOffset: minOffset, sourceApple: sourceApple, sourceBash: sourceBash) case let .upnext(from: from, to: to, maxToShow: maxToShow, autoResize: autoResize): - barItem = UpNextScrubberTouchBarItem(identifier: identifier, interval: 2, from: from, to: to, maxToShow: maxToShow, autoResize: autoResize) + barItem = UpNextScrubberTouchBarItem(identifier: identifier, interval: 60, from: from, to: to, maxToShow: maxToShow, autoResize: autoResize) } if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem { diff --git a/MTMR/Widgets/UpNextScrubberTouchBarItem.swift b/MTMR/Widgets/UpNextScrubberTouchBarItem.swift index 15a189b..e79a19b 100644 --- a/MTMR/Widgets/UpNextScrubberTouchBarItem.swift +++ b/MTMR/Widgets/UpNextScrubberTouchBarItem.swift @@ -47,15 +47,17 @@ class UpNextScrubberTouchBarItem: NSCustomTouchBarItem { super.init(identifier: identifier) view = scrollView // Add event sources - self.eventSources.append(UpNextCalenderSource()) - // Start activity to update view + self.eventSources.append(UpNextCalenderSource(updateCallback: self.updateView)) + // Add reactivity through interval updates + on calendar change handler activity.interval = interval activity.repeats = true activity.qualityOfService = .utility activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in + NSLog("---- INTERVAL ----") self.updateView() completion(NSBackgroundActivityScheduler.Result.finished) } + updateView() let upperBoundsDate = Date(timeIntervalSinceNow: futureSearchCutoff) @@ -66,7 +68,7 @@ class UpNextScrubberTouchBarItem: NSCustomTouchBarItem { fatalError("init(coder:) has not been implemented") } - private func updateView() { + private func updateView() -> Void { items = [] var upcomingEvents = self.getUpcomingEvents() NSLog("Found \(upcomingEvents.count) events") @@ -206,7 +208,9 @@ struct UpNextEventModel { protocol IUpNextSource { static var bundleIdentifier: String { get } var hasPermission : Bool { get } - init() + var updateCallback : () -> Void { get set } + + init(updateCallback: @escaping () -> Void) func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] } @@ -215,31 +219,35 @@ class UpNextCalenderSource : IUpNextSource { public var hasPermission: Bool = false private var eventStore = EKEventStore() + internal var updateCallback: () -> Void - required init() { + required init(updateCallback: @escaping () -> Void = {}) { + self.updateCallback = updateCallback + NotificationCenter.default.addObserver(forName: .EKEventStoreChanged, object: eventStore, queue: nil, using: handleUpdate) + eventStore.requestAccess(to: .event){ granted, error in self.hasPermission = granted; + self.handleUpdate(note: Notification(name: Notification.Name("refresh view"))) if(!granted) { NSLog("Error: MTMR UpNextBarWidget not given calendar access.") return } } } + + public func handleUpdate(note: Notification) { + self.updateCallback() + } + public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] { NSLog("Getting calendar events...") - eventStore = EKEventStore() 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]) - - let events = self.eventStore.events(matching: predicate) - for event in events { - upcomingEvents.append(UpNextEventModel(title: event.title, startDate: event.startDate, sourceType: UpNextSourceType.iCalendar)) - } + let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: calendars) + let events = self.eventStore.events(matching: predicate) + for event in events { + upcomingEvents.append(UpNextEventModel(title: event.title, startDate: event.startDate, sourceType: UpNextSourceType.iCalendar)) } - print("Found " + String(upcomingEvents.count) + " events.") return upcomingEvents } diff --git a/README.md b/README.md index 432ea13..e9babc9 100644 --- a/README.md +++ b/README.md @@ -347,7 +347,7 @@ To close a group, use the button: #### `upnext` > Calender next event plugin -Displays upcoming events from MacOS Calendar. Does not display current event +Displays upcoming events from MacOS Calendar. Does not display current event. ```js {