You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

// Copyright © 2015 Abhishek Banthia
import Cocoa
import EventKit
class MenubarHandler: NSObject {
@objc func titleForMenubar() -> String? {
6 years ago
if let nextEvent = checkForUpcomingEvents() {
return nextEvent
}
6 years ago
guard let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else {
return nil
}
6 years ago
// If the menubar is in compact mode, we don't need any of the below calculations; exit early
if DataStore.shared().shouldDisplay(.menubarCompactMode) {
return nil
}
6 years ago
if menubarTitles.isEmpty == false {
let titles = menubarTitles.map({ (data) -> String? in
let timezone = TimezoneData.customObject(from: data)
let operationsObject = TimezoneDataOperations(with: timezone!)
return "\(operationsObject.menuTitle().trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines))"
})
let titlesStringified = titles.compactMap { $0 }
return titlesStringified.joined(separator: " ")
}
return nil
}
private func checkForUpcomingEvents() -> String? {
6 years ago
if DataStore.shared().shouldDisplay(.showMeetingInMenubar) {
let filteredDates = EventCenter.sharedCenter().eventsForDate
let autoupdatingCal = EventCenter.sharedCenter().autoupdatingCalendar
guard let events = filteredDates[autoupdatingCal.startOfDay(for: Date())] else {
return nil
}
6 years ago
for event in events {
6 years ago
if event.event.startDate.timeIntervalSinceNow > 0 && !event.isAllDay {
6 years ago
let timeForEventToStart = event.event.startDate.timeIntervalSinceNow / 60
6 years ago
if timeForEventToStart > 30 {
6 years ago
print("Our next event: \(event.event.title ?? "Error") starts in \(timeForEventToStart) mins")
6 years ago
continue
}
6 years ago
return EventCenter.sharedCenter().format(event: event.event)
}
}
}
return nil
}
}