diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 090b2e7..9ca74e7 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -423,85 +423,94 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate } if let edit = object as? String { - let formattedValue = edit.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) + setNewLabel(edit, for: dataObject, at: row) + } else if let isFavouriteValue = object as? NSNumber { + dataObject.isFavourite = isFavouriteValue.intValue + insert(timezone: dataObject, at: row) + dataObject.isFavourite == 1 ? + markAsFavorite(dataObject) : + unfavourite(dataObject) + updateStatusItem() + refreshTimezoneTableView() + } - if selectedTimeZones.count > row { - Logger.log(object: [ - "Old Label": dataObject.customLabel ?? "Error", - "New Label": formattedValue - ], - for: "Custom Label Changed") + refreshMainTable() + } - dataObject.setLabel(formattedValue) + private func markAsFavorite(_ dataObject: TimezoneData) { + guard let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { + return + } - insert(timezone: dataObject, at: row) + var mutableArray = menubarTitles + let archivedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject) + mutableArray.append(archivedObject) - updateMenubarTitles() - } else { - Logger.log(object: [ - "MethodName": "SetObjectValue", - "Selected Timezone Count": selectedTimeZones.count, - "Current Row": row - ], - for: "Error in selected row count") - } - } else if let isFavouriteValue = object as? NSNumber { - dataObject.isFavourite = isFavouriteValue.intValue - insert(timezone: dataObject, at: row) + UserDefaults.standard.set(mutableArray, forKey: CLMenubarFavorites) - if dataObject.isFavourite == 1, let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] { + if dataObject.customLabel != nil { + Logger.log(object: ["label": dataObject.customLabel ?? "Error"], for: "favouriteSelected") + } - var mutableArray = menubarTitles - let archivedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject) - mutableArray.append(archivedObject) + if let appDelegate = NSApplication.shared.delegate as? AppDelegate { + appDelegate.setupMenubarTimer() + } - UserDefaults.standard.set(mutableArray, forKey: CLMenubarFavorites) + if mutableArray.count > 1 { + showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() + } + } - if dataObject.customLabel != nil { - Logger.log(object: ["label": dataObject.customLabel ?? "Error"], for: "favouriteSelected") - } + private func unfavourite(_ dataObject: TimezoneData) { + guard let menubarTimers = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { + assertionFailure("Menubar timers is unexpectedly nil") + return + } - if let appDelegate = NSApplication.shared.delegate as? AppDelegate { - appDelegate.setupMenubarTimer() - } + Logger.log(object: ["label": dataObject.customLabel ?? "Error"], + for: "favouriteRemoved") - if mutableArray.count > 1 { - showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() - } + let filteredMenubars = menubarTimers.filter { + guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else { + return false + } + return current.isEqual(dataObject) == false + } - } else { - guard let menubarTimers = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { - assertionFailure("Menubar timers is unexpectedly nil") - return - } + UserDefaults.standard.set(filteredMenubars, forKey: CLMenubarFavorites) - Logger.log(object: ["label": dataObject.customLabel ?? "Error"], - for: "favouriteRemoved") + if let appDelegate = NSApplication.shared.delegate as? AppDelegate, let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data], menubarFavourites.isEmpty, DataStore.shared().shouldDisplay(.showMeetingInMenubar) == false { + appDelegate.invalidateMenubarTimer(true) + } - let filteredMenubars = menubarTimers.filter { - guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else { - return false - } - return current != dataObject - } + if let appDelegate = NSApplication.shared.delegate as? AppDelegate { + appDelegate.setupMenubarTimer() + } + } - UserDefaults.standard.set(filteredMenubars, forKey: CLMenubarFavorites) + private func setNewLabel(_ label: String, for dataObject: TimezoneData, at row: Int) { + let formattedValue = label.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) - if let appDelegate = NSApplication.shared.delegate as? AppDelegate, let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data], menubarFavourites.isEmpty, DataStore.shared().shouldDisplay(.showMeetingInMenubar) == false { - appDelegate.invalidateMenubarTimer(true) - } + if selectedTimeZones.count > row { + Logger.log(object: [ + "Old Label": dataObject.customLabel ?? "Error", + "New Label": formattedValue + ], + for: "Custom Label Changed") - if let appDelegate = NSApplication.shared.delegate as? AppDelegate { - appDelegate.setupMenubarTimer() - } - } + dataObject.setLabel(formattedValue) - updateStatusItem() + insert(timezone: dataObject, at: row) - refreshTimezoneTableView() + updateMenubarTitles() + } else { + Logger.log(object: [ + "MethodName": "SetObjectValue", + "Selected Timezone Count": selectedTimeZones.count, + "Current Row": row + ], + for: "Error in selected row count") } - - refreshMainTable() } private func showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() {