From 292da17aee0b871e3242b7db1c7826efc051d8ef Mon Sep 17 00:00:00 2001 From: connorgmeehan Date: Sun, 26 Jul 2020 13:16:16 +1000 Subject: [PATCH] Added ability to click touchbar item and go to calendar --- MTMR/Widgets/UpNextBarItem.swift | 71 ++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/MTMR/Widgets/UpNextBarItem.swift b/MTMR/Widgets/UpNextBarItem.swift index 0621e0a..519c69a 100644 --- a/MTMR/Widgets/UpNextBarItem.swift +++ b/MTMR/Widgets/UpNextBarItem.swift @@ -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)