Skip to content

Commit

Permalink
Fix no name attendee and calendars with same name issue
Browse files Browse the repository at this point in the history
  • Loading branch information
leits committed Jul 3, 2020
1 parent a7040f0 commit bea939f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
8 changes: 4 additions & 4 deletions MeetingBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MeetingBar/Preview Content\"";
DEVELOPMENT_TEAM = KGH289N6T8;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -349,7 +349,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1;
MARKETING_VERSION = 1.2;
PRODUCT_BUNDLE_IDENTIFIER = leits.MeetingBar;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -365,7 +365,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MeetingBar/Preview Content\"";
DEVELOPMENT_TEAM = KGH289N6T8;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -376,7 +376,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1;
MARKETING_VERSION = 1.2;
PRODUCT_BUNDLE_IDENTIFIER = leits.MeetingBar;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
30 changes: 20 additions & 10 deletions MeetingBar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import KeyboardShortcuts
class AppDelegate: NSObject, NSApplicationDelegate {
var statusBarItem: StatusBarItemControler!

var selectedCalendarsObserver: DefaultsObservation?
var selectedCalendarIDsObserver: DefaultsObservation?
var showEventDetailsObserver: DefaultsObservation?
var showEventTitleInStatusBarObserver: DefaultsObservation?
var titleLengthObserver: DefaultsObservation?
Expand Down Expand Up @@ -47,13 +47,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func setup() {
DispatchQueue.main.async {

// Backward compatibility
var calendarTitles: [String] = []
if Defaults[.calendarTitle] != "" {
Defaults[.selectedCalendars].append(Defaults[.calendarTitle])
calendarTitles.append(Defaults[.calendarTitle])
Defaults[.calendarTitle] = ""
}

self.statusBarItem.updateTitle()
self.statusBarItem.updateMenu()
if !Defaults[.selectedCalendars].isEmpty {
calendarTitles.append(contentsOf: Defaults[.selectedCalendars])
Defaults[.selectedCalendars] = []
}
if !calendarTitles.isEmpty {
let matchCalendars = self.statusBarItem.eventStore.getCalendars(titles: calendarTitles)
for calendar in matchCalendars {
Defaults[.selectedCalendarIDs].append(calendar.calendarIdentifier)
}

}
self.statusBarItem.loadCalendars()

self.scheduleUpdateStatusBarTitle()
self.scheduleUpdateEvents()
Expand All @@ -67,11 +79,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.eventStoreChanged), name: .EKEventStoreChanged, object: self.statusBarItem.eventStore)

self.selectedCalendarsObserver = Defaults.observe(.selectedCalendars) { change in
NSLog("Changed selectedCalendars from \(change.oldValue) to \(change.newValue)")
self.statusBarItem.calendars = self.statusBarItem.eventStore.getCalendars(change.newValue)
self.statusBarItem.updateMenu()
self.statusBarItem.updateTitle()
self.selectedCalendarIDsObserver = Defaults.observe(.selectedCalendarIDs) { change in
NSLog("Changed selectedCalendarIDs from \(change.oldValue) to \(change.newValue)")
self.statusBarItem.loadCalendars()
}
self.showEventDetailsObserver = Defaults.observe(.showEventDetails) { change in
NSLog("Change showEventDetails from \(change.oldValue) to \(change.newValue)")
Expand Down
5 changes: 3 additions & 2 deletions MeetingBar/DefaultsKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import Defaults

extension Defaults.Keys {
static let calendarTitle = Key<String>("calendarTitle", default: "")
static let calendarTitle = Key<String>("calendarTitle", default: "") // Backward compatibility
static let selectedCalendars = Key<[String]>("selectedCalendars", default: []) // Backward compatibility
static let selectedCalendarIDs = Key<[String]>("selectedCalendarIDs", default: [])
static let useChromeForMeetLinks = Key<Bool>("useChromeForMeetLinks", default: false)
static let launchAtLogin = Key<Bool>("launchAtLogin", default: false)
static let showEventDetails = Key<Bool>("showEventDetails", default: true)
static let createMeetingService = Key<MeetingServices>("createMeetingService", default: .meet)
static let selectedCalendars = Key<[String]>("selectedCalendars", default: [])
static let showEventTitleInStatusBar = Key<Bool>("showEventTitleInStatusBar", default: true)
static let titleLength = Key<Double>("titleLength", default: TitleLengthLimits.max)
static let timeFormat = Key<TimeFormat>("timeFormat", default: .military)
Expand Down
11 changes: 6 additions & 5 deletions MeetingBar/EventStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ extension EKEventStore {
}
}

func getCalendars(_ titles: [String]) -> [EKCalendar] {
var selectedCalendars: [EKCalendar] = []
func getCalendars(titles: [String] = [], ids: [String] = []) -> [EKCalendar] {
var matchedCalendars: [EKCalendar] = []

let allCalendars = self.calendars(for: .event)
for calendar in allCalendars {
if titles.contains(calendar.title) {
selectedCalendars.append(calendar)
if titles.contains(calendar.title) || ids.contains(calendar.calendarIdentifier) {
print("\(calendar.title): \(calendar.calendarIdentifier)")
matchedCalendars.append(calendar)
}
}
return selectedCalendars
return matchedCalendars
}

func loadTodayEvents(calendars: [EKCalendar]) -> [EKEvent] {
Expand Down
10 changes: 5 additions & 5 deletions MeetingBar/PreferencesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct ContentView: View {
@Default(.useChromeForMeetLinks) var useChromeForMeetLinks
@Default(.showEventDetails) var showEventDetails
@Default(.createMeetingService) var createMeetingService
@Default(.selectedCalendars) var selectedCalendars
@Default(.selectedCalendarIDs) var selectedCalendarIDs
@Default(.showEventTitleInStatusBar) var showEventTitleInStatusBar
@Default(.titleLength) var titleLength
@Default(.timeFormat) var timeFormat
Expand Down Expand Up @@ -88,11 +88,11 @@ struct ContentView: View {
Form {
Text("Select your calendars:")
List(calendars, id: \.calendarIdentifier) { calendar in
MultipleSelectionRow(title: calendar.title, isSelected: self.selectedCalendars.contains(calendar.title), color: Color(calendar.color)) {
if self.selectedCalendars.contains(calendar.title) {
self.selectedCalendars.removeAll(where: { $0 == calendar.title })
MultipleSelectionRow(title: calendar.title, isSelected: self.selectedCalendarIDs.contains(calendar.calendarIdentifier), color: Color(calendar.color)) {
if self.selectedCalendarIDs.contains(calendar.calendarIdentifier) {
self.selectedCalendarIDs.removeAll(where: { $0 == calendar.calendarIdentifier })
} else {
self.selectedCalendars.append(calendar.title)
self.selectedCalendarIDs.append(calendar.calendarIdentifier)
}
}
}
Expand Down
25 changes: 16 additions & 9 deletions MeetingBar/StatusBarItemControler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class StatusBarItemControler {
)
let statusBarMenu = NSMenu(title: "MeetingBar in Status Bar Menu")
self.item.menu = statusBarMenu
self.calendars = self.eventStore.getCalendars(Defaults[.selectedCalendars])
}

func loadCalendars() {
self.calendars = self.eventStore.getCalendars(ids: Defaults[.selectedCalendarIDs])
self.updateTitle()
self.updateMenu()
}

func updateTitle() {
Expand Down Expand Up @@ -168,7 +173,7 @@ class StatusBarItemControler {
eventMenu.addItem(NSMenuItem.separator())

// Calendar
if Defaults[.selectedCalendars].count > 1 {
if Defaults[.selectedCalendarIDs].count > 1 {
eventMenu.addItem(withTitle: "Calendar:", action: nil, keyEquivalent: "")
eventMenu.addItem(withTitle: event.calendar.title, action: nil, keyEquivalent: "")
eventMenu.addItem(NSMenuItem.separator())
Expand Down Expand Up @@ -201,9 +206,9 @@ class StatusBarItemControler {
}

// Location
if event.location != nil {
if let location = event.location {
eventMenu.addItem(withTitle: "Location:", action: nil, keyEquivalent: "")
eventMenu.addItem(withTitle: "\(event.location!)", action: nil, keyEquivalent: "")
eventMenu.addItem(withTitle: "\(location)", action: nil, keyEquivalent: "")
eventMenu.addItem(NSMenuItem.separator())
}

Expand All @@ -217,7 +222,7 @@ class StatusBarItemControler {

// Notes
if event.hasNotes {
let notes = cleanUpNotes(event.notes!)
let notes = cleanUpNotes(event.notes ?? "")
if notes.count > 0 {
eventMenu.addItem(withTitle: "Notes:", action: nil, keyEquivalent: "")
let item = eventMenu.addItem(withTitle: "", action: nil, keyEquivalent: "")
Expand All @@ -230,7 +235,7 @@ class StatusBarItemControler {

// Attendees
if event.hasAttendees {
let attendees: [EKParticipant] = event.attendees!
let attendees: [EKParticipant] = event.attendees ?? []
let count = attendees.filter { $0.participantType == .person }.count
let sortedAttendees = attendees.sorted {
if $0.participantRole.rawValue != $1.participantRole.rawValue {
Expand All @@ -246,7 +251,7 @@ class StatusBarItemControler {
}
var attributes: [NSAttributedString.Key: Any] = [:]

var name = attendee.name!
var name = attendee.name ?? "No name attendee"

if attendee.isCurrentUser {
name = "\(name) (you)"
Expand Down Expand Up @@ -354,8 +359,10 @@ func openEvent(_ event: EKEvent) {

func getEventStatus(_ event: EKEvent) -> EKParticipantStatus? {
if event.hasAttendees {
if let currentUser = event.attendees!.first(where: { $0.isCurrentUser }) {
return currentUser.participantStatus
if let attendees = event.attendees {
if let currentUser = attendees.first(where: { $0.isCurrentUser }) {
return currentUser.participantStatus
}
}
}
return nil
Expand Down

0 comments on commit bea939f

Please sign in to comment.