diff --git a/Clocker/ClockerUnitTests/ClockerUnitTests.swift b/Clocker/ClockerUnitTests/ClockerUnitTests.swift index 538069b..9d5ce7a 100644 --- a/Clocker/ClockerUnitTests/ClockerUnitTests.swift +++ b/Clocker/ClockerUnitTests/ClockerUnitTests.swift @@ -90,7 +90,11 @@ class ClockerUnitTests: XCTestCase { func testDeletingATimezone() { let defaults = UserDefaults.standard - var currentFavourites = defaults.object(forKey: CLDefaultPreferenceKey) as! [Data] + + guard var currentFavourites = defaults.object(forKey: CLDefaultPreferenceKey) as? [Data] else { + XCTFail("Default preferences aren't in the correct format") + return + } let oldCount = currentFavourites.count currentFavourites = currentFavourites.filter { diff --git a/Clocker/Dependencies/Date Additions/Date+TimeAgo.swift b/Clocker/Dependencies/Date Additions/Date+TimeAgo.swift index a2752bf..734972d 100755 --- a/Clocker/Dependencies/Date Additions/Date+TimeAgo.swift +++ b/Clocker/Dependencies/Date Additions/Date+TimeAgo.swift @@ -173,12 +173,7 @@ public extension Date { } private func logicalLocalizedStringFromFormat(format: String, value: Int) -> String { - #if os(Linux) - let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value)) as! CVarArg) // this may not work, unclear!! - #else - let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value))) - #endif - + let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value))) return String.init(format: DateToolsLocalizedStrings(localeFormat), value) } diff --git a/Clocker/Dependencies/Date Additions/TimePeriodCollection.swift b/Clocker/Dependencies/Date Additions/TimePeriodCollection.swift index b928b38..5fbaa60 100755 --- a/Clocker/Dependencies/Date Additions/TimePeriodCollection.swift +++ b/Clocker/Dependencies/Date Additions/TimePeriodCollection.swift @@ -238,7 +238,7 @@ open class TimePeriodCollection: TimePeriodGroup { } internal func updateExtremes() { - if periods.count == 0 { + if periods.isEmpty { _beginning = nil _end = nil } else { diff --git a/Clocker/Dependencies/Solar.swift b/Clocker/Dependencies/Solar.swift index 67df70e..1897c23 100644 --- a/Clocker/Dependencies/Solar.swift +++ b/Clocker/Dependencies/Solar.swift @@ -78,35 +78,35 @@ public struct Solar { let lngHour = coordinate.longitude / 15 let hourTime: Double = sunriseSunset == .sunrise ? 6 : 18 - let t = day + ((hourTime - lngHour) / 24) + let time = day + ((hourTime - lngHour) / 24) // Calculate the suns mean anomaly - let M = (0.9856 * t) - 3.289 + let meanAnomaly = (0.9856 * time) - 3.289 // Calculate the sun's true longitude - let subexpression1 = 1.916 * sin(M.degreesToRadians) - let subexpression2 = 0.020 * sin(2 * M.degreesToRadians) - var L = M + subexpression1 + subexpression2 + 282.634 + let subexpression1 = 1.916 * sin(meanAnomaly.degreesToRadians) + let subexpression2 = 0.020 * sin(2 * meanAnomaly.degreesToRadians) + var longitude = meanAnomaly + subexpression1 + subexpression2 + 282.634 // Normalise L into [0, 360] range - L = normalise(L, withMaximum: 360) + longitude = normalise(longitude, withMaximum: 360) // Calculate the Sun's right ascension - var RA = atan(0.91764 * tan(L.degreesToRadians)).radiansToDegrees + var rightAscenscion = atan(0.91764 * tan(longitude.degreesToRadians)).radiansToDegrees // Normalise RA into [0, 360] range - RA = normalise(RA, withMaximum: 360) + rightAscenscion = normalise(rightAscenscion, withMaximum: 360) // Right ascension value needs to be in the same quadrant as L... - let Lquadrant = floor(L / 90) * 90 - let RAquadrant = floor(RA / 90) * 90 - RA = RA + (Lquadrant - RAquadrant) + let Lquadrant = floor(longitude / 90) * 90 + let RAquadrant = floor(rightAscenscion / 90) * 90 + rightAscenscion = rightAscenscion + (Lquadrant - RAquadrant) // Convert RA into hours - RA = RA / 15 + rightAscenscion = rightAscenscion / 15 // Calculate Sun's declination - let sinDec = 0.39782 * sin(L.degreesToRadians) + let sinDec = 0.39782 * sin(longitude.degreesToRadians) let cosDec = cos(asin(sinDec)) // Calculate the Sun's local hour angle @@ -124,24 +124,24 @@ public struct Solar { // Finish calculating H and convert into hours let tempH = sunriseSunset == .sunrise ? 360 - acos(cosH).radiansToDegrees : acos(cosH).radiansToDegrees - let H = tempH / 15.0 + let hours = tempH / 15.0 // Calculate local mean time of rising - let T = H + RA - (0.06571 * t) - 6.622 + let localMeanRisingTime = hours + rightAscenscion - (0.06571 * time) - 6.622 // Adjust time back to UTC - var UT = T - lngHour + var utcCompatibleTime = localMeanRisingTime - lngHour // Normalise UT into [0, 24] range - UT = normalise(UT, withMaximum: 24) + utcCompatibleTime = normalise(utcCompatibleTime, withMaximum: 24) // Calculate all of the sunrise's / sunset's date components - let hour = floor(UT) - let minute = floor((UT - hour) * 60.0) - let second = (((UT - hour) * 60) - minute) * 60.0 + let hour = floor(utcCompatibleTime) + let minute = floor((utcCompatibleTime - hour) * 60.0) + let second = (((utcCompatibleTime - hour) * 60) - minute) * 60.0 - let shouldBeYesterday = lngHour > 0 && UT > 12 && sunriseSunset == .sunrise - let shouldBeTomorrow = lngHour < 0 && UT < 12 && sunriseSunset == .sunset + let shouldBeYesterday = lngHour > 0 && utcCompatibleTime > 12 && sunriseSunset == .sunrise + let shouldBeTomorrow = lngHour < 0 && utcCompatibleTime < 12 && sunriseSunset == .sunset let setDate: Date if shouldBeYesterday { diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index f210c7f..064585c 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -80,86 +80,45 @@ class DataStore: NSObject { userDefaults.set(currentLineup, forKey: CLDefaultPreferenceKey) } + + private func shouldDisplayHelper(_ key: String) -> Bool { + guard let value = retrieve(key: key) as? NSNumber else { + return false + } + return value.isEqual(to: NSNumber(value: 0)) + } func shouldDisplay(_ type: ViewType) -> Bool { switch type { case .futureSlider: - guard let value = retrieve(key: CLDisplayFutureSliderKey) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLDisplayFutureSliderKey) case .upcomingEventView: guard let value = retrieve(key: CLShowUpcomingEventView) as? NSString else { return false } return value == "YES" - case .twelveHour: - - guard let value = retrieve(key: CL24hourFormatSelectedKey) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CL24hourFormatSelectedKey) case .showAllDayEventsInMenubar: - - guard let value = retrieve(key: CLShowAllDayEventsInUpcomingView) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLShowAllDayEventsInUpcomingView) case .sunrise: - - guard let value = retrieve(key: CLSunriseSunsetTime) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLSunriseSunsetTime) case .seconds: - - guard let value = retrieve(key: CLShowSecondsInMenubar) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLShowSecondsInMenubar) case .showMeetingInMenubar: - - guard let value = retrieve(key: CLShowMeetingInMenubar) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLShowMeetingInMenubar) case .showAppInForeground: - guard let value = retrieve(key: CLShowAppInForeground) as? NSNumber else { return false } return value.isEqual(to: NSNumber(value: 1)) - case .dateInMenubar: - - guard let value = retrieve(key: CLShowDateInMenu) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLShowDateInMenu) case .placeInMenubar: - - guard let value = retrieve(key: CLShowPlaceInMenu) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLShowPlaceInMenu) case .dayInMenubar: - - guard let value = retrieve(key: CLShowDayInMenu) as? NSNumber else { - return false - } - return value.isEqual(to: NSNumber(value: 0)) - + return shouldDisplayHelper(CLShowDayInMenu) case .menubarCompactMode: - guard let value = retrieve(key: CLMenubarCompactMode) as? Int else { return false } diff --git a/Clocker/Panel/Data Layer/TimezoneData.swift b/Clocker/Panel/Data Layer/TimezoneData.swift index 78fb008..86a27a8 100644 --- a/Clocker/Panel/Data Layer/TimezoneData.swift +++ b/Clocker/Panel/Data Layer/TimezoneData.swift @@ -30,8 +30,8 @@ class TimezoneData: NSObject, NSCoding { } enum SecondsOverride: Int { - case yes - case no + case showSeconds + case hideSeconds case globalFormat } @@ -87,7 +87,7 @@ class TimezoneData: NSObject, NSCoding { overrideSecondsFormat = .globalFormat } - init(with dictionary: Dictionary) { + init(with dictionary: [String: Any]) { if let label = dictionary[CLCustomLabel] as? String { customLabel = label } else { @@ -338,9 +338,9 @@ class TimezoneData: NSObject, NSCoding { func setShouldOverrideSecondsFormat(_ shouldOverride: Int) { if shouldOverride == 0 { - overrideSecondsFormat = .yes + overrideSecondsFormat = .showSeconds } else if shouldOverride == 1 { - overrideSecondsFormat = .no + overrideSecondsFormat = .hideSeconds } else { overrideSecondsFormat = .globalFormat } @@ -410,7 +410,7 @@ class TimezoneData: NSObject, NSCoding { return DataStore.shared().shouldDisplay(.seconds) } - return overrideSecondsFormat == .yes + return overrideSecondsFormat == .showSeconds } override var hash: Int { @@ -424,6 +424,13 @@ class TimezoneData: NSObject, NSCoding { static func == (lhs: TimezoneData, rhs: TimezoneData) -> Bool { return lhs.placeID == rhs.placeID } + + override func isEqual(to object: Any?) -> Bool { + if let other = object as? TimezoneData { + return placeID == other.placeID + } + return false + } override func isEqual(_ object: Any?) -> Bool { guard let compared = object as? TimezoneData else { diff --git a/Clocker/Panel/Notes Popover/NotesPopover.swift b/Clocker/Panel/Notes Popover/NotesPopover.swift index 4f1595c..bf215fd 100644 --- a/Clocker/Panel/Notes Popover/NotesPopover.swift +++ b/Clocker/Panel/Notes Popover/NotesPopover.swift @@ -320,12 +320,10 @@ class NotesPopover: NSViewController { } } - for timezoneObject in timezoneObjects { - if timezoneObject == dataObject { - overrideType == .timezoneFormat ? - timezoneObject.setShouldOverrideGlobalTimeFormat(override) : - timezoneObject.setShouldOverrideSecondsFormat(override) - } + for timezoneObject in timezoneObjects where timezoneObject == dataObject { + overrideType == .timezoneFormat ? + timezoneObject.setShouldOverrideGlobalTimeFormat(override) : + timezoneObject.setShouldOverrideSecondsFormat(override) } var datas: [Data] = [] @@ -351,12 +349,10 @@ class NotesPopover: NSViewController { } } - for timezoneObject in timezoneObjects { - if timezoneObject == dataObject { - overrideType == .timezoneFormat ? - timezoneObject.setShouldOverrideGlobalTimeFormat(override) : - timezoneObject.setShouldOverrideSecondsFormat(override) - } + for timezoneObject in timezoneObjects where timezoneObject == dataObject { + overrideType == .timezoneFormat ? + timezoneObject.setShouldOverrideGlobalTimeFormat(override) : + timezoneObject.setShouldOverrideSecondsFormat(override) } var datas: [Data] = [] diff --git a/Clocker/Panel/PanelController.swift b/Clocker/Panel/PanelController.swift index df55d4f..2a78f3e 100644 --- a/Clocker/Panel/PanelController.swift +++ b/Clocker/Panel/PanelController.swift @@ -46,7 +46,7 @@ class PanelController: ParentPanelController { func setFrameTheNewWay(_ rect: NSRect, _ maxX: CGFloat) { // Calculate window's top left point. // First, center window under status item. - let w = CGFloat(NSWidth((window?.frame)!)) + let w = (window?.frame)!.width var x = CGFloat(roundf(Float(rect.midX - w / 2))) let y = CGFloat(rect.minY - 2) let kMinimumSpaceBetweenWindowAndScreenEdge: CGFloat = 10 @@ -120,15 +120,15 @@ class PanelController: ParentPanelController { var testPoint = statusItemFrame.origin testPoint.y -= 100 - for screen in NSScreen.screens { - if NSPointInRect(testPoint, screen.frame) { - statusItemScreen = screen - break - } + for screen in NSScreen.screens where screen.frame.contains(testPoint) { + statusItemScreen = screen + break } - let screenMaxX = NSMaxX((statusItemScreen?.frame)!) - let minY = statusItemFrame.origin.y < NSMaxY((statusItemScreen?.frame)!) ? statusItemFrame.origin.y : NSMaxY((statusItemScreen?.frame)!) + let screenMaxX = (statusItemScreen?.frame)!.maxX + let minY = statusItemFrame.origin.y < (statusItemScreen?.frame)!.maxY ? + statusItemFrame.origin.y : + (statusItemScreen?.frame)!.maxY statusItemFrame.origin.y = minY setFrameTheNewWay(statusItemFrame, screenMaxX) diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index 0a937b7..096e2c6 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -378,7 +378,7 @@ class ParentPanelController: NSWindowController { } // This is for the Add Cell View case - if preferences.count == 0 { + if preferences.isEmpty { scrollViewHeight.constant = 100.0 return } diff --git a/Clocker/Panel/UI/CustomSliderCell.swift b/Clocker/Panel/UI/CustomSliderCell.swift index cd08197..fb02bac 100644 --- a/Clocker/Panel/UI/CustomSliderCell.swift +++ b/Clocker/Panel/UI/CustomSliderCell.swift @@ -14,7 +14,7 @@ class CustomSliderCell: NSSliderCell { return } - let finalWidth = value * (NSWidth(control.frame) - 8) + let finalWidth = value * (control.frame.width - 8) // Left Part var leftRect = rect diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 6d1e743..e237d0c 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -151,7 +151,7 @@ class AppearanceViewController: ParentViewController { panelController.sharingButton.image = Themer.shared().sharingImage() let defaultTimezones = panelController.defaultPreferences - if defaultTimezones.count == 0 { + if defaultTimezones.isEmpty { panelController.updatePanelColor() } diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 8fe4e0a..8570b5e 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -218,7 +218,7 @@ class PreferencesViewController: ParentViewController { [placeholderLabel, additionalSortOptions].forEach { $0.isHidden = true } - if timezoneArray.count == 0 { + if timezoneArray.isEmpty { timezoneArray.append("UTC") timezoneArray.append("Anywhere on Earth") timezoneArray.append(contentsOf: NSTimeZone.knownTimeZoneNames) @@ -936,7 +936,7 @@ extension PreferencesViewController { return } - if messageLabel.stringValue.count == 0 { + if messageLabel.stringValue.isEmpty { searchField.stringValue = CLEmptyString guard let latitude = dataObject.latitude, let longitude = dataObject.longitude else { @@ -1075,7 +1075,7 @@ extension PreferencesViewController { refreshMainTable() - if selectedTimeZones.count == 0 { + if selectedTimeZones.isEmpty { UserDefaults.standard.set(nil, forKey: CLMenubarFavorites) }