Browse Source

Cleaning up.

pull/92/head
Abhishek Banthia 6 years ago
parent
commit
c752cd5afc
  1. 129
      Clocker/Preferences/General/PreferencesViewController.swift

129
Clocker/Preferences/General/PreferencesViewController.swift

@ -423,85 +423,94 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
} }
if let edit = object as? String { 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 { refreshMainTable()
Logger.log(object: [ }
"Old Label": dataObject.customLabel ?? "Error",
"New Label": formattedValue
],
for: "Custom Label Changed")
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() UserDefaults.standard.set(mutableArray, forKey: CLMenubarFavorites)
} 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)
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 if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
let archivedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject) appDelegate.setupMenubarTimer()
mutableArray.append(archivedObject) }
UserDefaults.standard.set(mutableArray, forKey: CLMenubarFavorites) if mutableArray.count > 1 {
showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar()
}
}
if dataObject.customLabel != nil { private func unfavourite(_ dataObject: TimezoneData) {
Logger.log(object: ["label": dataObject.customLabel ?? "Error"], for: "favouriteSelected") 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 { Logger.log(object: ["label": dataObject.customLabel ?? "Error"],
appDelegate.setupMenubarTimer() for: "favouriteRemoved")
}
if mutableArray.count > 1 { let filteredMenubars = menubarTimers.filter {
showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else {
} return false
}
return current.isEqual(dataObject) == false
}
} else { UserDefaults.standard.set(filteredMenubars, forKey: CLMenubarFavorites)
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"], if let appDelegate = NSApplication.shared.delegate as? AppDelegate, let menubarFavourites = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data], menubarFavourites.isEmpty, DataStore.shared().shouldDisplay(.showMeetingInMenubar) == false {
for: "favouriteRemoved") appDelegate.invalidateMenubarTimer(true)
}
let filteredMenubars = menubarTimers.filter { if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else { appDelegate.setupMenubarTimer()
return false }
} }
return current != dataObject
}
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 { if selectedTimeZones.count > row {
appDelegate.invalidateMenubarTimer(true) Logger.log(object: [
} "Old Label": dataObject.customLabel ?? "Error",
"New Label": formattedValue
],
for: "Custom Label Changed")
if let appDelegate = NSApplication.shared.delegate as? AppDelegate { dataObject.setLabel(formattedValue)
appDelegate.setupMenubarTimer()
}
}
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() { private func showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() {

Loading…
Cancel
Save