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

Added ability to click touchbar item and go to calendar

This commit is contained in:
connorgmeehan 2020-07-26 13:16:16 +10:00
parent 42d6c077f2
commit 292da17aee

View File

@ -67,32 +67,38 @@ class UpNextBarItem: NSCustomTouchBarItem {
private func updateView() {
items = []
var upcomingEvents = self.getUpcomingEvents()
NSLog("Found \(upcomingEvents.count) events")
upcomingEvents.sort(by: {$0.startDate.compare($1.startDate) == .orderedAscending})
var index = 1
for event in upcomingEvents {
// Create UpNextItem
let item = UpNextItem(event: event)
item.isBordered = false;
items.append(item)
// Check if should display any more
if (index == self.nthEvent) {
break;
DispatchQueue.main.async {
for event in upcomingEvents {
// Create UpNextItem
let item = UpNextItem(event: event)
item.backgroundColor = self.getBackgroundColor(startDate: event.startDate)
// Bind tap event
item.tapClosure = { [weak self] in
self?.switchToApp(event: event)
}
// Add to view
self.items.append(item)
// Check if should display any more
if (index == self.nthEvent) {
break;
}
index += 1
}
index += 1
self.reloadData()
}
self.reloadData()
}
private func reloadData() {
NSLog("Displaying \(items.count) items...")
let stackView = NSStackView(views: items.compactMap { $0.view })
DispatchQueue.main.async {
stackView.spacing = 1
stackView.orientation = .horizontal
let visibleRect = self.scrollView.documentVisibleRect
self.scrollView.documentView = stackView
stackView.scroll(visibleRect.origin)
}
stackView.spacing = 5
stackView.orientation = .horizontal
let visibleRect = self.scrollView.documentVisibleRect
self.scrollView.documentView = stackView
stackView.scroll(visibleRect.origin)
}
private func getUpcomingEvents() -> [UpNextEventModel] {
@ -113,9 +119,29 @@ class UpNextBarItem: NSCustomTouchBarItem {
return upcomingEvents
}
func gotoAppleCalendar() {
print("CLICK")
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Calendar.app"))
public func switchToApp(event: UpNextEventModel) {
var bundleIdentifier: String
switch(event.sourceType) {
case .iCalendar:
bundleIdentifier = UpNextCalenderSource.bundleIdentifier
}
NSWorkspace.shared.launchApplication(withBundleIdentifier: bundleIdentifier, options: [.default], additionalEventParamDescriptor: nil, launchIdentifier: nil)
// NB: if you can't open app which on another space, try to check mark
// "When switching to an application, switch to a Space with open windows for the application"
// in Mission control settings
}
func getBackgroundColor(startDate: Date) -> NSColor {
let distance = abs(Date().timeIntervalSinceReferenceDate/60 - startDate.timeIntervalSinceReferenceDate/60) // Get time difference in minutes
if(distance < 30 as TimeInterval) { // Less than 30 minutes, backround is red
return NSColor.systemRed
} else if (distance < 120 as TimeInterval) { // Less than 2 hours, background is yellow
return NSColor.systemOrange
}
return NSColor.clear
}
}
@ -166,14 +192,18 @@ struct UpNextEventModel {
// Interface for any event source
protocol IUpNextSource {
static var bundleIdentifier: String { get }
var hasPermission : Bool { get }
init()
func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel]
}
class UpNextCalenderSource : IUpNextSource {
static public let bundleIdentifier: String = "com.apple.iCal"
public var hasPermission: Bool = false
private var eventStore = EKEventStore()
required init() {
eventStore.requestAccess(to: .event){ granted, error in
self.hasPermission = granted;
@ -190,7 +220,6 @@ class UpNextCalenderSource : IUpNextSource {
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)