Browse Source

More linting.

pull/92/head
Abhishek Banthia 6 years ago
parent
commit
0fd72ab06f
  1. 2
      Clocker/AppDelegate.swift
  2. 4
      Clocker/Clocker.xcodeproj/project.pbxproj
  3. 18
      Clocker/Overall App/UserDefaults + KVOExtensions.swift
  4. 2
      Clocker/Panel/PanelController.swift
  5. 154
      Clocker/Panel/ParentPanelController.swift
  6. 92
      Clocker/Preferences/General/PreferencesViewController.swift

2
Clocker/AppDelegate.swift

@ -126,7 +126,7 @@ open class AppDelegate : NSObject, NSApplicationDelegate {
assignShortcut() assignShortcut()
panelObserver = panelController.observe(\.hasActivePanel, options: [.new]) { (obj, change) in panelObserver = panelController.observe(\.hasActivePanel, options: [.new]) { (obj, _) in
self.statusBarHandler.setHasActiveIcon(obj.hasActivePanelGetter()) self.statusBarHandler.setHasActiveIcon(obj.hasActivePanelGetter())
} }

4
Clocker/Clocker.xcodeproj/project.pbxproj

@ -7,6 +7,7 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
3595FAD0227F88BC0044A12A /* UserDefaults + KVOExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3595FACF227F88BC0044A12A /* UserDefaults + KVOExtensions.swift */; };
35C36EE422595EFD002FA5C6 /* StatusContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36EE022595EFD002FA5C6 /* StatusContainerView.swift */; }; 35C36EE422595EFD002FA5C6 /* StatusContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36EE022595EFD002FA5C6 /* StatusContainerView.swift */; };
35C36EE522595EFD002FA5C6 /* MenubarHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36EE122595EFD002FA5C6 /* MenubarHandler.swift */; }; 35C36EE522595EFD002FA5C6 /* MenubarHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36EE122595EFD002FA5C6 /* MenubarHandler.swift */; };
35C36EE622595EFD002FA5C6 /* StatusItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36EE222595EFD002FA5C6 /* StatusItemView.swift */; }; 35C36EE622595EFD002FA5C6 /* StatusItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35C36EE222595EFD002FA5C6 /* StatusItemView.swift */; };
@ -214,6 +215,7 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
3545C52A22612BCC00121E25 /* RateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RateTests.swift; sourceTree = "<group>"; }; 3545C52A22612BCC00121E25 /* RateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RateTests.swift; sourceTree = "<group>"; };
3595FACF227F88BC0044A12A /* UserDefaults + KVOExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UserDefaults + KVOExtensions.swift"; sourceTree = "<group>"; };
35C36EE022595EFD002FA5C6 /* StatusContainerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusContainerView.swift; sourceTree = "<group>"; }; 35C36EE022595EFD002FA5C6 /* StatusContainerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusContainerView.swift; sourceTree = "<group>"; };
35C36EE122595EFD002FA5C6 /* MenubarHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenubarHandler.swift; sourceTree = "<group>"; }; 35C36EE122595EFD002FA5C6 /* MenubarHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenubarHandler.swift; sourceTree = "<group>"; };
35C36EE222595EFD002FA5C6 /* StatusItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusItemView.swift; sourceTree = "<group>"; }; 35C36EE222595EFD002FA5C6 /* StatusItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusItemView.swift; sourceTree = "<group>"; };
@ -504,6 +506,7 @@
35C36F3D2259D892002FA5C6 /* Strings.swift */, 35C36F3D2259D892002FA5C6 /* Strings.swift */,
35C36F392259D892002FA5C6 /* Themer.swift */, 35C36F392259D892002FA5C6 /* Themer.swift */,
35C36F3A2259D892002FA5C6 /* Timer.swift */, 35C36F3A2259D892002FA5C6 /* Timer.swift */,
3595FACF227F88BC0044A12A /* UserDefaults + KVOExtensions.swift */,
); );
path = "Overall App"; path = "Overall App";
sourceTree = "<group>"; sourceTree = "<group>";
@ -1178,6 +1181,7 @@
C2CCCD8220619C4C00F2DFC2 /* LocationController.swift in Sources */, C2CCCD8220619C4C00F2DFC2 /* LocationController.swift in Sources */,
35C36F4B2259D971002FA5C6 /* UnderlinedButton.swift in Sources */, 35C36F4B2259D971002FA5C6 /* UnderlinedButton.swift in Sources */,
9AB6F1562259CF3900A44663 /* CalendarViewController.swift in Sources */, 9AB6F1562259CF3900A44663 /* CalendarViewController.swift in Sources */,
3595FAD0227F88BC0044A12A /* UserDefaults + KVOExtensions.swift in Sources */,
9AB6F1612259D1B000A44663 /* PreferencesViewController.swift in Sources */, 9AB6F1612259D1B000A44663 /* PreferencesViewController.swift in Sources */,
35C36F2022596253002FA5C6 /* OneWindowController.swift in Sources */, 35C36F2022596253002FA5C6 /* OneWindowController.swift in Sources */,
35C36F442259D892002FA5C6 /* Logger.swift in Sources */, 35C36F442259D892002FA5C6 /* Logger.swift in Sources */,

18
Clocker/Overall App/UserDefaults + KVOExtensions.swift

@ -0,0 +1,18 @@
// Copyright © 2015 Abhishek Banthia
import Cocoa
extension UserDefaults {
@objc dynamic var displayFutureSlider: Int {
return integer(forKey: CLDisplayFutureSliderKey)
}
@objc dynamic var userFontSize: Int {
return integer(forKey: CLUserFontSizePreference)
}
@objc dynamic var sliderDayRange: Int {
return integer(forKey: CLFutureSliderRange)
}
}

2
Clocker/Panel/PanelController.swift

@ -3,7 +3,7 @@
import Cocoa import Cocoa
class PanelController: ParentPanelController { class PanelController: ParentPanelController {
@objc dynamic var hasActivePanel: Bool = false @objc dynamic var hasActivePanel: Bool = false
static var sharedWindow = PanelController(windowNibName: .panel) static var sharedWindow = PanelController(windowNibName: .panel)

154
Clocker/Panel/ParentPanelController.swift

@ -13,6 +13,10 @@ struct PanelConstants {
class ParentPanelController: NSWindowController { class ParentPanelController: NSWindowController {
private var futureSliderObserver: NSKeyValueObservation?
private var userFontSizeSelectionObserver: NSKeyValueObservation?
private var futureSliderRangeObserver: NSKeyValueObservation?
private var eventStoreChangedNotification: NSObjectProtocol? private var eventStoreChangedNotification: NSObjectProtocol?
var dateFormatter = DateFormatter() var dateFormatter = DateFormatter()
@ -87,6 +91,32 @@ class ParentPanelController: NSWindowController {
if let eventStoreNotif = eventStoreChangedNotification { if let eventStoreNotif = eventStoreChangedNotification {
NotificationCenter.default.removeObserver(eventStoreNotif) NotificationCenter.default.removeObserver(eventStoreNotif)
} }
[futureSliderObserver, userFontSizeSelectionObserver, futureSliderRangeObserver].forEach {
$0?.invalidate()
}
}
private func setupObservers() {
futureSliderObserver = UserDefaults.standard.observe(\.displayFutureSlider, options: [.new]) { (_, change) in
if let changedValue = change.newValue {
self.futureSliderView.isHidden = changedValue == 1
}
}
userFontSizeSelectionObserver = UserDefaults.standard.observe(\.userFontSize, options: [.new]) { (_, change) in
if let newFontSize = change.newValue {
Logger.log(object: ["FontSize": newFontSize], for: "User Font Size Preference")
self.mainTableView.reloadData()
self.setScrollViewConstraint()
}
}
futureSliderRangeObserver = UserDefaults.standard.observe(\.sliderDayRange, options: [.new]) { (_, change) in
if change.newValue != nil {
self.adjustFutureSliderBasedOnPreferences()
}
}
} }
override func awakeFromNib() { override func awakeFromNib() {
@ -107,13 +137,7 @@ class ParentPanelController: NSWindowController {
mainTableView.selectionHighlightStyle = .none mainTableView.selectionHighlightStyle = .none
mainTableView.enclosingScrollView?.hasVerticalScroller = false mainTableView.enclosingScrollView?.hasVerticalScroller = false
[CLDisplayFutureSliderKey, CLUserFontSizePreference, CLThemeKey, CLFutureSliderRange].forEach { key in setupObservers()
UserDefaults.standard.addObserver(self,
forKeyPath: key,
options: .new,
context: nil)
}
updateReviewViewFontColor() updateReviewViewFontColor()
@ -281,6 +305,8 @@ class ParentPanelController: NSWindowController {
sharingButton.image = sharedThemer.sharingImage() sharingButton.image = sharedThemer.sharingImage()
sliderDatePicker.textColor = sharedThemer.mainTextColor() sliderDatePicker.textColor = sharedThemer.mainTextColor()
updateReviewViewFontColor()
} }
override func windowDidLoad() { override func windowDidLoad() {
@ -302,25 +328,6 @@ class ParentPanelController: NSWindowController {
} }
} }
override func observeValue(forKeyPath keyPath: String?, of _: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == CLDisplayFutureSliderKey, let changes = change, let new = changes[NSKeyValueChangeKey.newKey] as? NSNumber {
futureSliderView.isHidden = new.isEqual(to: NSNumber(value: 1))
} else if keyPath == CLUserFontSizePreference, let userFontSize = DataStore.shared().retrieve(key: CLUserFontSizePreference) as? NSNumber {
Logger.log(object: ["FontSize": userFontSize], for: "User Font Size Preference")
mainTableView.reloadData()
setScrollViewConstraint()
} else if keyPath == CLThemeKey {
updateReviewViewFontColor()
} else if keyPath == CLFutureSliderRange {
adjustFutureSliderBasedOnPreferences()
} else {
super.observeValue(forKeyPath: keyPath,
of: keyPath,
change: change,
context: context)
}
}
func screenHeight() -> CGFloat { func screenHeight() -> CGFloat {
guard let main = NSScreen.main else { return 100 } guard let main = NSScreen.main else { return 100 }
@ -343,38 +350,42 @@ class ParentPanelController: NSWindowController {
parentTimer = nil parentTimer = nil
} }
func setScrollViewConstraint() { private func getAdjustedRowHeight(for object: TimezoneData?, _ currentHeight: CGFloat) -> CGFloat {
var totalHeight: CGFloat = 0.0 var newHeight = currentHeight
let preferences = defaultPreferences
for cellIndex in 0 ..< preferences.count { if newHeight <= 68.0 {
let currentObject = TimezoneData.customObject(from: preferences[cellIndex]) newHeight = 69.0
let rowRect = mainTableView.rect(ofRow: cellIndex) }
var height: CGFloat = rowRect.size.height
if height <= 68.0 { if newHeight >= 68.0 {
height = 69.0 newHeight = 75.0
if let note = object?.note, note.isEmpty == false {
newHeight += 30
} }
}
if height >= 68.0 { if newHeight >= 88.0 {
height = 75.0 // Set it to 95 expicity in case the row height is calculated be higher.
if let note = currentObject?.note, note.isEmpty == false { newHeight = 95.0
height += 30
} if let note = object?.note, note.isEmpty {
newHeight -= 30.0
} }
}
if height >= 88.0 { newHeight += mainTableView.intercellSpacing.height
// Set it to 95 expicity in case the row height is calculated be higher.
height = 95.0
if let note = currentObject?.note, note.isEmpty { return newHeight
height -= 30.0 }
}
}
height += mainTableView.intercellSpacing.height func setScrollViewConstraint() {
var totalHeight: CGFloat = 0.0
let preferences = defaultPreferences
totalHeight += height for cellIndex in 0 ..< preferences.count {
let currentObject = TimezoneData.customObject(from: preferences[cellIndex])
let rowRect = mainTableView.rect(ofRow: cellIndex)
totalHeight += getAdjustedRowHeight(for: currentObject, rowRect.size.height)
} }
// This is for the Add Cell View case // This is for the Add Cell View case
@ -828,41 +839,44 @@ class ParentPanelController: NSWindowController {
return return
} }
NSAnimationContext.runAnimationGroup({ context in NSAnimationContext.runAnimationGroup({ (context) in
context.duration = 1 context.duration = 1
context.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut) context.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
leftButton.animator().alphaValue = 0.0 leftButton.animator().alphaValue = 0.0
rightButton.animator().alphaValue = 0.0 rightButton.animator().alphaValue = 0.0
}) { }, completionHandler: {
field.stringValue = title field.stringValue = title
NSAnimationContext.runAnimationGroup({ context in NSAnimationContext.runAnimationGroup({ (context) in
context.duration = 1 context.duration = 1
context.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeIn) context.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeIn)
self.leftButton.animator().alphaValue = 1.0 self.runAnimationCompletionBlock(leftTitle, rightTitle)
self.rightButton.animator().alphaValue = 1.0 }, completionHandler: {})
})
let paragraphStyle = NSMutableParagraphStyle() }
paragraphStyle.alignment = .center
let styleAttributes = [ private func runAnimationCompletionBlock(_ leftButtonTitle: String, _ rightButtonTitle: String) {
NSAttributedString.Key.paragraphStyle: paragraphStyle, self.leftButton.animator().alphaValue = 1.0
NSAttributedString.Key.font: NSFont(name: "Avenir-Light", size: 13)! self.rightButton.animator().alphaValue = 1.0
]
if self.leftButton.attributedTitle.string == "Not Really" { let paragraphStyle = NSMutableParagraphStyle()
self.leftButton.animator().attributedTitle = NSAttributedString(string: PanelConstants.noThanksTitle, attributes: styleAttributes) paragraphStyle.alignment = .center
}
if self.rightButton.attributedTitle.string == PanelConstants.yesWithExclamation { let styleAttributes = [
self.rightButton.animator().attributedTitle = NSAttributedString(string: "Yes, sure", attributes: styleAttributes) NSAttributedString.Key.paragraphStyle: paragraphStyle,
} NSAttributedString.Key.font: NSFont(name: "Avenir-Light", size: 13)!
]
self.leftButton.animator().attributedTitle = NSAttributedString(string: leftTitle, attributes: styleAttributes) if self.leftButton.attributedTitle.string == "Not Really" {
self.rightButton.animator().attributedTitle = NSAttributedString(string: rightTitle, attributes: styleAttributes) self.leftButton.animator().attributedTitle = NSAttributedString(string: PanelConstants.noThanksTitle, attributes: styleAttributes)
}
}) {} if self.rightButton.attributedTitle.string == PanelConstants.yesWithExclamation {
self.rightButton.animator().attributedTitle = NSAttributedString(string: "Yes, sure", attributes: styleAttributes)
} }
self.leftButton.animator().attributedTitle = NSAttributedString(string: leftButtonTitle, attributes: styleAttributes)
self.rightButton.animator().attributedTitle = NSAttributedString(string: rightButtonTitle, attributes: styleAttributes)
} }
// MARK: Date Picker + Slider // MARK: Date Picker + Slider

92
Clocker/Preferences/General/PreferencesViewController.swift

@ -340,30 +340,11 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
} }
if tableColumn?.identifier.rawValue == PreferencesConstants.timezoneNameIdentifier { if tableColumn?.identifier.rawValue == PreferencesConstants.timezoneNameIdentifier {
guard let model = selectedDataSource else { return handleTimezoneNameIdentifier(for: row, selectedDataSource)
return nil
}
if let address = model.formattedAddress, address.isEmpty == false {
return model.formattedAddress
}
return model.timezoneID
} }
if tableColumn?.identifier.rawValue == PreferencesConstants.availableTimezoneIdentifier { if tableColumn?.identifier.rawValue == PreferencesConstants.availableTimezoneIdentifier {
let criteria = searchCriteria.selectedSegment return handleAvailableTimezoneColumn(for: row, dataSource)
if criteria == 0 {
if row < filteredArray.count {
return dataSource?.formattedAddress
}
} else {
if searchField.stringValue.isEmpty == false && row < timezoneFilteredArray.count {
return timezoneFilteredArray[row]
}
return timezoneArray[row]
}
} }
if tableColumn?.identifier.rawValue == PreferencesConstants.customLabelIdentifier { if tableColumn?.identifier.rawValue == PreferencesConstants.customLabelIdentifier {
@ -375,31 +356,64 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
} }
if tableColumn?.identifier.rawValue == "abbreviation" { if tableColumn?.identifier.rawValue == "abbreviation" {
if searchField.stringValue.isEmpty == false && (row < timezoneFilteredArray.count) { return handleAbbreviationColumn(for: row)
let currentSelection = timezoneFilteredArray[row] }
if currentSelection == "UTC" {
return "UTC"
} else if currentSelection == "Anywhere on Earth" {
return "GMT+12"
}
return NSTimeZone(name: timezoneFilteredArray[row])?.abbreviation ?? "Error" return nil
} }
if timezoneArray.count > row { private func handleTimezoneNameIdentifier(for row: Int, _ selectedDataSource: TimezoneData?) -> Any? {
// Special return for manually inserted 'UTC' guard let model = selectedDataSource else {
if timezoneArray[row] == "UTC" { return nil
return "UTC" }
}
if timezoneArray[row] == "Anywhere on Earth" { if let address = model.formattedAddress, address.isEmpty == false {
return "AoE" return model.formattedAddress
} }
return model.timezoneID
}
return NSTimeZone(name: timezoneArray[row])?.abbreviation ?? "Error" private func handleAvailableTimezoneColumn(for row: Int, _ dataSource: TimezoneData?) -> Any? {
let criteria = searchCriteria.selectedSegment
if criteria == 0 {
if row < filteredArray.count {
return dataSource?.formattedAddress
} }
} else {
if searchField.stringValue.isEmpty == false && row < timezoneFilteredArray.count {
return timezoneFilteredArray[row]
}
return timezoneArray[row]
} }
return nil
}
private func handleAbbreviationColumn(for row: Int) -> Any? {
if searchField.stringValue.isEmpty == false && (row < timezoneFilteredArray.count) {
let currentSelection = timezoneFilteredArray[row]
if currentSelection == "UTC" {
return "UTC"
} else if currentSelection == "Anywhere on Earth" {
return "GMT+12"
}
return NSTimeZone(name: timezoneFilteredArray[row])?.abbreviation ?? "Error"
}
if timezoneArray.count > row {
// Special return for manually inserted 'UTC'
if timezoneArray[row] == "UTC" {
return "UTC"
}
if timezoneArray[row] == "Anywhere on Earth" {
return "AoE"
}
return NSTimeZone(name: timezoneArray[row])?.abbreviation ?? "Error"
}
return nil return nil
} }

Loading…
Cancel
Save