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.
63 lines
2.1 KiB
63 lines
2.1 KiB
4 years ago
|
// Copyright © 2015 Abhishek Banthia
|
||
|
|
||
|
import Cocoa
|
||
4 years ago
|
import CoreLoggerKit
|
||
4 years ago
|
import CoreModelKit
|
||
4 years ago
|
import EventKit
|
||
|
|
||
3 years ago
|
class MenubarTitleProvider: NSObject {
|
||
4 years ago
|
func titleForMenubar() -> String? {
|
||
|
if let nextEvent = checkForUpcomingEvents() {
|
||
|
return nextEvent
|
||
|
}
|
||
|
|
||
|
guard let menubarTitles = DataStore.shared().menubarTimezones() else {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
if menubarTitles.isEmpty == false {
|
||
3 years ago
|
let titles = menubarTitles.map { data -> String? in
|
||
4 years ago
|
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? {
|
||
|
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
|
||
|
}
|
||
|
|
||
|
for event in events {
|
||
|
if event.event.startDate.timeIntervalSinceNow > 0, !event.isAllDay {
|
||
|
let timeForEventToStart = event.event.startDate.timeIntervalSinceNow / 60
|
||
|
|
||
|
if timeForEventToStart > 30 {
|
||
|
Logger.info("Our next event: \(event.event.title ?? "Error") starts in \(timeForEventToStart) mins")
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
return EventCenter.sharedCenter().format(event: event.event)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
}
|