mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
Added EKEventStore listener to reduce perfomance impact
This commit is contained in:
parent
59867febfb
commit
2a35710d79
@ -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 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 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 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)
|
self = .upnext(from: from, to: to, maxToShow: maxToShow, autoResize: autoResize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -304,7 +304,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case let .swipe(direction: direction, fingers: fingers, minOffset: minOffset, sourceApple: sourceApple, sourceBash: sourceBash):
|
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)
|
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):
|
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 {
|
if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
|
||||||
|
|||||||
@ -47,15 +47,17 @@ class UpNextScrubberTouchBarItem: NSCustomTouchBarItem {
|
|||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
view = scrollView
|
view = scrollView
|
||||||
// Add event sources
|
// Add event sources
|
||||||
self.eventSources.append(UpNextCalenderSource())
|
self.eventSources.append(UpNextCalenderSource(updateCallback: self.updateView))
|
||||||
// Start activity to update view
|
// Add reactivity through interval updates + on calendar change handler
|
||||||
activity.interval = interval
|
activity.interval = interval
|
||||||
activity.repeats = true
|
activity.repeats = true
|
||||||
activity.qualityOfService = .utility
|
activity.qualityOfService = .utility
|
||||||
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
|
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
|
||||||
|
NSLog("---- INTERVAL ----")
|
||||||
self.updateView()
|
self.updateView()
|
||||||
completion(NSBackgroundActivityScheduler.Result.finished)
|
completion(NSBackgroundActivityScheduler.Result.finished)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateView()
|
updateView()
|
||||||
|
|
||||||
let upperBoundsDate = Date(timeIntervalSinceNow: futureSearchCutoff)
|
let upperBoundsDate = Date(timeIntervalSinceNow: futureSearchCutoff)
|
||||||
@ -66,7 +68,7 @@ class UpNextScrubberTouchBarItem: NSCustomTouchBarItem {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateView() {
|
private func updateView() -> Void {
|
||||||
items = []
|
items = []
|
||||||
var upcomingEvents = self.getUpcomingEvents()
|
var upcomingEvents = self.getUpcomingEvents()
|
||||||
NSLog("Found \(upcomingEvents.count) events")
|
NSLog("Found \(upcomingEvents.count) events")
|
||||||
@ -206,7 +208,9 @@ struct UpNextEventModel {
|
|||||||
protocol IUpNextSource {
|
protocol IUpNextSource {
|
||||||
static var bundleIdentifier: String { get }
|
static var bundleIdentifier: String { get }
|
||||||
var hasPermission : Bool { get }
|
var hasPermission : Bool { get }
|
||||||
init()
|
var updateCallback : () -> Void { get set }
|
||||||
|
|
||||||
|
init(updateCallback: @escaping () -> Void)
|
||||||
func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel]
|
func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,31 +219,35 @@ class UpNextCalenderSource : IUpNextSource {
|
|||||||
|
|
||||||
public var hasPermission: Bool = false
|
public var hasPermission: Bool = false
|
||||||
private var eventStore = EKEventStore()
|
private var eventStore = EKEventStore()
|
||||||
|
internal var updateCallback: () -> Void
|
||||||
|
|
||||||
|
required init(updateCallback: @escaping () -> Void = {}) {
|
||||||
|
self.updateCallback = updateCallback
|
||||||
|
NotificationCenter.default.addObserver(forName: .EKEventStoreChanged, object: eventStore, queue: nil, using: handleUpdate)
|
||||||
|
|
||||||
required init() {
|
|
||||||
eventStore.requestAccess(to: .event){ granted, error in
|
eventStore.requestAccess(to: .event){ granted, error in
|
||||||
self.hasPermission = granted;
|
self.hasPermission = granted;
|
||||||
|
self.handleUpdate(note: Notification(name: Notification.Name("refresh view")))
|
||||||
if(!granted) {
|
if(!granted) {
|
||||||
NSLog("Error: MTMR UpNextBarWidget not given calendar access.")
|
NSLog("Error: MTMR UpNextBarWidget not given calendar access.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func handleUpdate(note: Notification) {
|
||||||
|
self.updateCallback()
|
||||||
|
}
|
||||||
|
|
||||||
public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] {
|
public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] {
|
||||||
NSLog("Getting calendar events...")
|
NSLog("Getting calendar events...")
|
||||||
eventStore = EKEventStore()
|
|
||||||
var upcomingEvents: [UpNextEventModel] = []
|
var upcomingEvents: [UpNextEventModel] = []
|
||||||
let calendars = self.eventStore.calendars(for: .event)
|
let calendars = self.eventStore.calendars(for: .event)
|
||||||
|
let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: calendars)
|
||||||
for calendar in calendars {
|
let events = self.eventStore.events(matching: predicate)
|
||||||
let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: [calendar])
|
for event in events {
|
||||||
|
upcomingEvents.append(UpNextEventModel(title: event.title, startDate: event.startDate, sourceType: UpNextSourceType.iCalendar))
|
||||||
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.")
|
print("Found " + String(upcomingEvents.count) + " events.")
|
||||||
return upcomingEvents
|
return upcomingEvents
|
||||||
}
|
}
|
||||||
|
|||||||
@ -347,7 +347,7 @@ To close a group, use the button:
|
|||||||
#### `upnext`
|
#### `upnext`
|
||||||
|
|
||||||
> Calender next event plugin
|
> 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
|
```js
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user