1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 00:58:37 +00:00

Added EKEventStore listener to reduce perfomance impact

This commit is contained in:
connorgmeehan 2020-07-26 20:06:05 +10:00
parent 59867febfb
commit 2a35710d79
4 changed files with 26 additions and 17 deletions

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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
{