Browse Source

Cleaning up.

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

67
Clocker/Preferences/General/PreferencesViewController.swift

@ -423,33 +423,24 @@ 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)
if selectedTimeZones.count > row {
Logger.log(object: [
"Old Label": dataObject.customLabel ?? "Error",
"New Label": formattedValue
],
for: "Custom Label Changed")
dataObject.setLabel(formattedValue)
insert(timezone: dataObject, at: row)
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 { } else if let isFavouriteValue = object as? NSNumber {
dataObject.isFavourite = isFavouriteValue.intValue dataObject.isFavourite = isFavouriteValue.intValue
insert(timezone: dataObject, at: row) insert(timezone: dataObject, at: row)
dataObject.isFavourite == 1 ?
markAsFavorite(dataObject) :
unfavourite(dataObject)
updateStatusItem()
refreshTimezoneTableView()
}
refreshMainTable()
}
if dataObject.isFavourite == 1, let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] { private func markAsFavorite(_ dataObject: TimezoneData) {
guard let menubarTitles = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else {
return
}
var mutableArray = menubarTitles var mutableArray = menubarTitles
let archivedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject) let archivedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject)
@ -468,8 +459,9 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
if mutableArray.count > 1 { if mutableArray.count > 1 {
showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar()
} }
}
} else { private func unfavourite(_ dataObject: TimezoneData) {
guard let menubarTimers = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else { guard let menubarTimers = DataStore.shared().retrieve(key: CLMenubarFavorites) as? [Data] else {
assertionFailure("Menubar timers is unexpectedly nil") assertionFailure("Menubar timers is unexpectedly nil")
return return
@ -482,7 +474,7 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else { guard let current = NSKeyedUnarchiver.unarchiveObject(with: $0) as? TimezoneData else {
return false return false
} }
return current != dataObject return current.isEqual(dataObject) == false
} }
UserDefaults.standard.set(filteredMenubars, forKey: CLMenubarFavorites) UserDefaults.standard.set(filteredMenubars, forKey: CLMenubarFavorites)
@ -496,12 +488,29 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
} }
} }
updateStatusItem() private func setNewLabel(_ label: String, for dataObject: TimezoneData, at row: Int) {
let formattedValue = label.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
refreshTimezoneTableView() if selectedTimeZones.count > row {
} Logger.log(object: [
"Old Label": dataObject.customLabel ?? "Error",
"New Label": formattedValue
],
for: "Custom Label Changed")
refreshMainTable() dataObject.setLabel(formattedValue)
insert(timezone: dataObject, at: row)
updateMenubarTitles()
} else {
Logger.log(object: [
"MethodName": "SetObjectValue",
"Selected Timezone Count": selectedTimeZones.count,
"Current Row": row
],
for: "Error in selected row count")
}
} }
private func showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() { private func showAlertIfMoreThanOneTimezoneHasBeenAddedToTheMenubar() {

Loading…
Cancel
Save