From 70dc952f0a0b4b8e858a8a4f4d2a3327af82c51c Mon Sep 17 00:00:00 2001 From: Abhishek Date: Fri, 10 Jan 2020 08:38:15 +0000 Subject: [PATCH] Removing CLMenubarFavorites. --- Clocker/Clocker/Utilities/CommonStrings.h | 1 - Clocker/Clocker/Utilities/CommonStrings.m | 1 - Clocker/Menu Bar/StatusItemHandler.swift | 8 +- Clocker/Overall App/AppDefaults.swift | 6 +- Clocker/Overall App/DataStore.swift | 5 +- Clocker/Overall App/Strings.swift | 1 - Clocker/Panel/Data Layer/TimezoneData.swift | 14 ---- .../Panel/Notes Popover/NotesPopover.swift | 48 ------------ Clocker/Panel/PanelController.swift | 4 +- Clocker/Panel/ParentPanelController.swift | 35 +-------- .../Appearance/AppearanceViewController.swift | 2 +- .../General/PreferencesDataSource.swift | 17 ---- .../General/PreferencesViewController.swift | 77 +------------------ 13 files changed, 15 insertions(+), 204 deletions(-) diff --git a/Clocker/Clocker/Utilities/CommonStrings.h b/Clocker/Clocker/Utilities/CommonStrings.h index d1671ae..baef60a 100644 --- a/Clocker/Clocker/Utilities/CommonStrings.h +++ b/Clocker/Clocker/Utilities/CommonStrings.h @@ -25,7 +25,6 @@ extern NSString *const CLLocationSearchURL; extern NSString *const CLShowSecondsInMenubar; extern NSString *const CLUserFontSizePreference; extern NSString *const CLShowUpcomingEventView; -extern NSString *const CLMenubarFavorites; extern NSString *const CLShowAllDayEventsInUpcomingView; extern NSString *const CLShowMeetingInMenubar; extern NSString *const CLTruncateTextLength; diff --git a/Clocker/Clocker/Utilities/CommonStrings.m b/Clocker/Clocker/Utilities/CommonStrings.m index 6272266..f62f20c 100644 --- a/Clocker/Clocker/Utilities/CommonStrings.m +++ b/Clocker/Clocker/Utilities/CommonStrings.m @@ -24,7 +24,6 @@ NSString *const CLSunriseSunsetTime = @"showSunriseSetTime"; NSString *const CLShowSecondsInMenubar = @"showSeconds"; NSString *const CLUserFontSizePreference = @"userFontSize"; NSString *const CLShowUpcomingEventView = @"ShowUpcomingEventView"; -NSString *const CLMenubarFavorites = @"menubarFavourites"; NSString *const CLFutureSliderRange = @"sliderDayRange"; NSString *const CLShowAllDayEventsInUpcomingView = @"showAllDayEventsInUpcomingView"; NSString *const CLShowMeetingInMenubar = @"showMeetingInfoInMenubar"; diff --git a/Clocker/Menu Bar/StatusItemHandler.swift b/Clocker/Menu Bar/StatusItemHandler.swift index a5477f2..1d1d888 100644 --- a/Clocker/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Menu Bar/StatusItemHandler.swift @@ -74,7 +74,7 @@ class StatusItemHandler: NSObject { // Let's figure out the initial menubar state var menubarState = MenubarState.icon - let shouldTextBeDisplayed = (DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data])?.isEmpty ?? true + let shouldTextBeDisplayed = DataStore.shared().menubarTimezones()?.isEmpty ?? true if !shouldTextBeDisplayed || DataStore.shared().shouldDisplay(.showMeetingInMenubar) { if DataStore.shared().shouldDisplay(.menubarCompactMode) { @@ -219,7 +219,7 @@ class StatusItemHandler: NSObject { private func calculateFireDate() -> Date? { let shouldDisplaySeconds = shouldDisplaySecondsInMenubar() - let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) + let menubarFavourites = DataStore.shared().menubarTimezones() if !units.contains(.second), shouldDisplaySeconds { units.insert(.second) @@ -228,7 +228,7 @@ class StatusItemHandler: NSObject { var components = nsCalendar.dateComponents(units, from: Date()) // We want to update every second only when there's a timezone present! - if shouldDisplaySeconds, let seconds = components.second, let favourites = menubarFavourites as? [Data], !favourites.isEmpty { + if shouldDisplaySeconds, let seconds = components.second, let favourites = menubarFavourites, !favourites.isEmpty { components.second = seconds + 1 } else if let minutes = components.minute { components.minute = minutes + 1 @@ -281,7 +281,7 @@ class StatusItemHandler: NSObject { // 1. Timezones // 2. Upcoming Event - let menubarFavourites = (DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data]) ?? [] + let menubarFavourites = DataStore.shared().menubarTimezones() ?? [] if menubarFavourites.isEmpty, DataStore.shared().shouldDisplay(.showMeetingInMenubar) == false { print("Invalidating menubar timer!") diff --git a/Clocker/Overall App/AppDefaults.swift b/Clocker/Overall App/AppDefaults.swift index d7505a3..6e5af28 100644 --- a/Clocker/Overall App/AppDefaults.swift +++ b/Clocker/Overall App/AppDefaults.swift @@ -20,7 +20,6 @@ class AppDefaults { private class func initializeDefaults() { let userDefaults = UserDefaults.standard - let menubarFavourites = userDefaults.object(forKey: CLMenubarFavorites) let timezones = userDefaults.object(forKey: CLDefaultPreferenceKey) let selectedCalendars = userDefaults.object(forKey: CLSelectedCalendars) @@ -30,8 +29,6 @@ class AppDefaults { // Register the usual suspects userDefaults.register(defaults: defaultsDictionary()) - // Set arrays and our custom objects - userDefaults.set(menubarFavourites, forKey: CLMenubarFavorites) userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) userDefaults.set(selectedCalendars, forKey: CLSelectedCalendars) @@ -53,7 +50,7 @@ class AppDefaults { if userDefaults.bool(forKey: CLSwitchToCompactModeAlert) == false { userDefaults.set(true, forKey: CLSwitchToCompactModeAlert) - if let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data], menubarFavourites.count > 1 { + if let menubarFavourites = DataStore.shared().menubarTimezones(), menubarFavourites.count > 1 { // If the user is already using the compact mode, abort. if DataStore.shared().shouldDisplay(.menubarCompactMode) { return @@ -124,7 +121,6 @@ class AppDefaults { CLUserFontSizePreference: 4, CLShowUpcomingEventView: "YES", CLShowAppInForeground: 0, - CLMenubarFavorites: [], CLFutureSliderRange: 0, CLShowAllDayEventsInUpcomingView: 1, CLShowMeetingInMenubar: 1, diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 91a68b4..55b4cc5 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -45,7 +45,10 @@ class DataStore: NSObject { } func menubarTimezones() -> [Data]? { - return DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] + return timezones().filter { + let customTimezone = TimezoneData.customObject(from: $0) + return customTimezone?.isFavourite == 1 + } } func updateDayPreference() { diff --git a/Clocker/Overall App/Strings.swift b/Clocker/Overall App/Strings.swift index 0a2df2d..7af5834 100644 --- a/Clocker/Overall App/Strings.swift +++ b/Clocker/Overall App/Strings.swift @@ -22,7 +22,6 @@ let CLSunriseSunsetTime = "showSunriseSetTime" let CLShowSecondsInMenubar = "showSeconds" let CLUserFontSizePreference = "userFontSize" let CLShowUpcomingEventView = "ShowUpcomingEventView" -let CLMenubarFavorites = "menubarFavourites" let CLShowAllDayEventsInUpcomingView = "showAllDayEventsInUpcomingView" let CLShowMeetingInMenubar = "showMeetingInfoInMenubar" let CLTruncateTextLength = "truncateTextLength" diff --git a/Clocker/Panel/Data Layer/TimezoneData.swift b/Clocker/Panel/Data Layer/TimezoneData.swift index 1a0338a..0487a13 100644 --- a/Clocker/Panel/Data Layer/TimezoneData.swift +++ b/Clocker/Panel/Data Layer/TimezoneData.swift @@ -220,20 +220,6 @@ class TimezoneData: NSObject, NSCoding { DataStore.shared().setTimezones(newModels) } } - - if let menubarTimezones = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data], !menubarTimezones.isEmpty { - let newMenubarModels = converter(menubarTimezones) - - if newMenubarModels.count == menubarTimezones.count { - // Now point preferences to empty - UserDefaults.standard.set(nil, forKey: CLMenubarFavorites) - - // Now point it to new models - UserDefaults.standard.set(newMenubarModels, forKey: CLMenubarFavorites) - - print("Successfully converted: \(newMenubarModels.count) menubar objects.") - } - } } private class func converter(_ timezones: [Data]) -> [Data] { diff --git a/Clocker/Panel/Notes Popover/NotesPopover.swift b/Clocker/Panel/Notes Popover/NotesPopover.swift index 2c88784..5dcb982 100644 --- a/Clocker/Panel/Notes Popover/NotesPopover.swift +++ b/Clocker/Panel/Notes Popover/NotesPopover.swift @@ -265,7 +265,6 @@ class NotesPopover: NSViewController { @IBAction func customizeTimeFormat(_ sender: NSSegmentedControl) { updateTimezoneInDefaultPreferences(with: sender.selectedSegment, .timezoneFormat) - updateMenubarTimezoneInDefaultPreferences(with: sender.selectedSegment, .timezoneFormat) refreshMainTableView() // Update the display if the chosen menubar mode is compact! @@ -282,22 +281,6 @@ class NotesPopover: NSViewController { DataStore.shared().setTimezones(timezones) } - private func updateMenubarTitles() { - guard let model = dataObject, model.isFavourite == 1, var timezones = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { return } - let menubarIndex = timezones.firstIndex { (menubarLocation) -> Bool in - if let convertedObject = TimezoneData.customObject(from: menubarLocation) { - return convertedObject.isEqual(dataObject) - } - return false - } - - if let index = menubarIndex { - let encodedObject = NSKeyedArchiver.archivedData(withRootObject: model) - timezones[index] = encodedObject - UserDefaults.standard.set(timezones, forKey: CLMenubarFavorites) - } - } - private func updateTimezoneInDefaultPreferences(with override: Int, _ overrideType: OverrideType) { let timezones = DataStore.shared().timezones() @@ -325,35 +308,6 @@ class NotesPopover: NSViewController { DataStore.shared().setTimezones(datas) } - private func updateMenubarTimezoneInDefaultPreferences(with override: Int, _ overrideType: OverrideType) { - guard let timezones = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { - return - } - - var timezoneObjects: [TimezoneData] = [] - - for timezone in timezones { - if let model = TimezoneData.customObject(from: timezone) { - timezoneObjects.append(model) - } - } - - for timezoneObject in timezoneObjects where timezoneObject.isEqual(dataObject) { - overrideType == .timezoneFormat ? - timezoneObject.setShouldOverrideGlobalTimeFormat(override) : - timezoneObject.setShouldOverrideSecondsFormat(override) - } - - var datas: [Data] = [] - - for updatedObject in timezoneObjects { - let dataObject = NSKeyedArchiver.archivedData(withRootObject: updatedObject) - datas.append(dataObject) - } - - UserDefaults.standard.set(datas, forKey: CLMenubarFavorites) - } - private func setReminderAlarm() { let eventCenter = EventCenter.sharedCenter() @@ -438,7 +392,6 @@ class NotesPopover: NSViewController { @IBAction func customizeSecondsFormat(_ sender: NSSegmentedControl) { updateTimezoneInDefaultPreferences(with: sender.selectedSegment, .seconds) - updateMenubarTimezoneInDefaultPreferences(with: sender.selectedSegment, .seconds) refreshMainTableView() // Update the display if the chosen menubar mode is compact! @@ -529,7 +482,6 @@ extension NotesPopover: NSTextFieldDelegate { model.setLabel(customLabel.stringValue) insertTimezoneInDefaultPreferences() - updateMenubarTitles() NotificationCenter.default.post(name: NSNotification.Name.customLabelChanged, object: nil) diff --git a/Clocker/Panel/PanelController.swift b/Clocker/Panel/PanelController.swift index 70806c2..702d096 100644 --- a/Clocker/Panel/PanelController.swift +++ b/Clocker/Panel/PanelController.swift @@ -249,7 +249,7 @@ class PanelController: ParentPanelController { } private func stopMenubarTimerIfNeccesary() { - let count = (DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data])?.count ?? 0 + let count = DataStore.shared().menubarTimezones()?.count ?? 0 if count >= 1 || DataStore.shared().shouldDisplay(.showMeetingInMenubar) { if let delegate = NSApplication.shared.delegate as? AppDelegate { @@ -271,7 +271,7 @@ class PanelController: ParentPanelController { func minimize() { let delegate = NSApplication.shared.delegate as? AppDelegate - let count = (DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data])?.count ?? 0 + let count = DataStore.shared().menubarTimezones()?.count ?? 0 if count >= 1 || DataStore.shared().shouldDisplay(.showMeetingInMenubar) == true { if let handler = delegate?.statusItemForPanel(), let timer = handler.menubarTimer, !timer.isValid { diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index dde064f..d5b13f0 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -222,7 +222,6 @@ class ParentPanelController: NSWindowController { DataStore.shared().setTimezones(datas) - // Update appereance if in compact menubar mode if let appDelegate = NSApplication.shared.delegate as? AppDelegate { appDelegate.setupMenubarTimer() } @@ -487,20 +486,11 @@ class ParentPanelController: NSWindowController { func deleteTimezone(at row: Int) { var defaults = defaultPreferences - // Remove object from menubar favourites if present - if let dataObject = TimezoneData.customObject(from: defaults[row]) { - removeFromMenubarFavourites(timezone: dataObject) - } - // Remove from panel defaults.remove(at: row) DataStore.shared().setTimezones(defaults) updateDefaultPreferences() - if defaults.isEmpty { - UserDefaults.standard.set([], forKey: CLMenubarFavorites) - } - NotificationCenter.default.post(name: Notification.Name.customLabelChanged, object: nil) @@ -508,35 +498,12 @@ class ParentPanelController: NSWindowController { Logger.log(object: [:], for: "Deleted Timezone Through Swipe") } - private func removeFromMenubarFavourites(timezone: TimezoneData) { - if timezone.isFavourite == 1, let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] { - let filtered = menubarTitles.filter { - let dataObject = TimezoneData.customObject(from: $0) - - // Special check for home indicator objects! - if timezone.isSystemTimezone, let isSystem = dataObject?.isSystemTimezone, isSystem { - return false - } - - return dataObject?.placeID != timezone.placeID - } - - UserDefaults.standard.set(filtered, forKey: CLMenubarFavorites) - - // Update the status bar's appearance if it is in custom mode. - if let delegate = NSApplication.shared.delegate as? AppDelegate { - let statusItemPanel = delegate.statusItemForPanel() - statusItemPanel.setupStatusItem() - } - } - } - private lazy var menubarTitleHandler = MenubarHandler() @objc func updateTime() { let store = DataStore.shared() - let menubarCount = (store.retrieve(key: CLMenubarFavorites) as? [Data])?.count ?? 0 + let menubarCount = store.menubarTimezones()?.count ?? 0 if menubarCount >= 1 || store.shouldDisplay(.showMeetingInMenubar) == true { if let status = (NSApplication.shared.delegate as? AppDelegate)?.statusItemForPanel() { diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 727f31e..6984f6b 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -60,7 +60,7 @@ class AppearanceViewController: ParentViewController { override func viewWillAppear() { super.viewWillAppear() - if let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] { + if let menubarFavourites = DataStore.shared().menubarTimezones() { visualEffectView.isHidden = menubarFavourites.isEmpty ? false : true informationLabel.isHidden = menubarFavourites.isEmpty ? false : true } diff --git a/Clocker/Preferences/General/PreferencesDataSource.swift b/Clocker/Preferences/General/PreferencesDataSource.swift index 7f346eb..97823cd 100644 --- a/Clocker/Preferences/General/PreferencesDataSource.swift +++ b/Clocker/Preferences/General/PreferencesDataSource.swift @@ -186,23 +186,6 @@ extension PreferencesDataSource: NSTableViewDataSource { } private func updateMenubarTitles() { - let defaultTimezones = DataStore.shared().timezones() - UserDefaults.standard.set([], forKey: CLMenubarFavorites) - - let menubarTimes = defaultTimezones.compactMap { (data) -> TimezoneData? in - if let model = TimezoneData.customObject(from: data), model.isFavourite == 1 { - return model - } - return nil - } - - let archivedObjects = menubarTimes.map { (timezone) -> Data in - NSKeyedArchiver.archivedData(withRootObject: timezone) - } - - UserDefaults.standard.set(archivedObjects, forKey: CLMenubarFavorites) - - // Update appereance if in compact menubar mode if let appDelegate = NSApplication.shared.delegate as? AppDelegate { appDelegate.setupMenubarTimer() } diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 268e722..d62cc54 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -195,23 +195,6 @@ class PreferencesViewController: ParentViewController { } private func updateMenubarTitles() { - let defaultTimezones = DataStore.shared().timezones() - UserDefaults.standard.set([], forKey: CLMenubarFavorites) - - let menubarTimes = defaultTimezones.compactMap { (data) -> TimezoneData? in - if let model = TimezoneData.customObject(from: data), model.isFavourite == 1 { - return model - } - return nil - } - - let archivedObjects = menubarTimes.map { (timezone) -> Data in - NSKeyedArchiver.archivedData(withRootObject: timezone) - } - - UserDefaults.standard.set(archivedObjects, forKey: CLMenubarFavorites) - - // Update appereance if in compact menubar mode if let appDelegate = NSApplication.shared.delegate as? AppDelegate { appDelegate.setupMenubarTimer() } @@ -316,16 +299,6 @@ class PreferencesViewController: ParentViewController { extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate { private func _markAsFavorite(_ dataObject: TimezoneData) { - guard let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { - return - } - - var mutableArray = menubarTitles - let archivedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject) - mutableArray.append(archivedObject) - - UserDefaults.standard.set(mutableArray, forKey: CLMenubarFavorites) - if dataObject.customLabel != nil { Logger.log(object: ["label": dataObject.customLabel ?? "Error"], for: "favouriteSelected") } @@ -334,31 +307,17 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate appDelegate.setupMenubarTimer() } - if mutableArray.count > 1 { + if let menubarTimezones = DataStore.shared().menubarTimezones(), menubarTimezones.count > 1 { showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() } } private func _unfavourite(_ dataObject: TimezoneData) { - guard let menubarTimers = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { - assertionFailure("Menubar timers is unexpectedly nil") - return - } - Logger.log(object: ["label": dataObject.customLabel ?? "Error"], for: "favouriteRemoved") - let filteredMenubars = menubarTimers.filter { - guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else { - return false - } - return current.isEqual(dataObject) == false - } - - UserDefaults.standard.set(filteredMenubars, forKey: CLMenubarFavorites) - if let appDelegate = NSApplication.shared.delegate as? AppDelegate, - let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data], + let menubarFavourites = DataStore.shared().menubarTimezones(), menubarFavourites.isEmpty, DataStore.shared().shouldDisplay(.showMeetingInMenubar) == false { appDelegate.invalidateMenubarTimer(true) @@ -874,16 +833,6 @@ extension PreferencesViewController { return } - let currentObject = selectedTimeZones[timezoneTableView.selectedRow] - guard let model = TimezoneData.customObject(from: currentObject) else { - assertionFailure("Data was unexpectedly nil") - return - } - - if model.isFavourite == 1 { - removeFromMenubarFavourites(object: model) - } - var newDefaults = selectedTimeZones let objectsToRemove = timezoneTableView.selectedRowIndexes.map { (index) -> Data in @@ -900,10 +849,6 @@ extension PreferencesViewController { refreshMainTable() - if selectedTimeZones.isEmpty { - UserDefaults.standard.set(nil, forKey: CLMenubarFavorites) - } - updateStatusBarAppearance() updateStatusItem() @@ -926,24 +871,6 @@ extension PreferencesViewController { statusItem.setupStatusItem() } - private func removeFromMenubarFavourites(object: TimezoneData?) { - guard let model = object else { - assertionFailure("Data was unexpectedly nil") - return - } - - if model.isFavourite == 1 { - if let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] { - let updated = menubarTitles.filter { (data) -> Bool in - let current = TimezoneData.customObject(from: data) - return current != model - } - - UserDefaults.standard.set(updated, forKey: CLMenubarFavorites) - } - } - } - @IBAction func filterArray(_: Any?) { messageLabel.stringValue = CLEmptyString