From 1e4c96d6503378f05136e5aadc884ae504d77cdc Mon Sep 17 00:00:00 2001 From: Abhishek Date: Wed, 2 Dec 2020 23:32:09 -0600 Subject: [PATCH] Remove overriding seconds format. --- Clocker/Overall App/DataStore.swift | 4 + Clocker/Panel/Data Layer/TimezoneData.swift | 115 +++++++---------- .../Panel/Notes Popover/NotesPopover.swift | 15 +-- .../Appearance/AppearanceViewController.swift | 16 ++- Clocker/Preferences/Preferences.storyboard | 121 +++++++++--------- 5 files changed, 130 insertions(+), 141 deletions(-) diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 3fad483..d8824a1 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -107,6 +107,10 @@ class DataStore: NSObject { return value.isEqual(to: NSNumber(value: 0)) } + func timezoneFormat() -> NSNumber { + return userDefaults.object(forKey: CL24hourFormatSelectedKey) as? NSNumber ?? NSNumber(integerLiteral: 0) + } + func shouldDisplay(_ type: ViewType) -> Bool { switch type { case .futureSlider: diff --git a/Clocker/Panel/Data Layer/TimezoneData.swift b/Clocker/Panel/Data Layer/TimezoneData.swift index 465ba56..1b415cb 100644 --- a/Clocker/Panel/Data Layer/TimezoneData.swift +++ b/Clocker/Panel/Data Layer/TimezoneData.swift @@ -8,6 +8,10 @@ struct DateFormat { static let twelveHourWithSeconds = "h:mm:ss a" static let twentyFourHour = "HH:mm" static let twentyFourHourWithSeconds = "HH:mm:ss" + static let twelveHourWithZero = "hh:mm a" + static let twelveHourWithZeroSeconds = "hh:mm:ss a" + static let twelveHourWithoutSuffix = "hh:mm" + static let twelveHourWithoutSuffixAndSeconds = "hh:mm:ss" } // Non-class type cannot conform to NSCoding! @@ -28,11 +32,16 @@ class TimezoneData: NSObject, NSCoding { case globalFormat } - enum SecondsOverride: Int { - case showSeconds - case hideSeconds - case globalFormat - } + let values = [ + NSNumber(integerLiteral: 0): DateFormat.twelveHour, + NSNumber(integerLiteral: 1): DateFormat.twelveHourWithSeconds, + NSNumber(integerLiteral: 2): DateFormat.twentyFourHour, + NSNumber(integerLiteral: 3): DateFormat.twentyFourHourWithSeconds, + NSNumber(integerLiteral: 4): DateFormat.twelveHourWithZero, + NSNumber(integerLiteral: 5): DateFormat.twelveHourWithZeroSeconds, + NSNumber(integerLiteral: 6): DateFormat.twelveHourWithoutSuffix, + NSNumber(integerLiteral: 7): DateFormat.twelveHourWithoutSuffixAndSeconds, + ] var customLabel: String? var formattedAddress: String? @@ -49,7 +58,6 @@ class TimezoneData: NSObject, NSCoding { var selectionType: SelectionType = .city var isSystemTimezone = false var overrideFormat: TimezoneOverride = .globalFormat - var overrideSecondsFormat: SecondsOverride = .globalFormat override init() { selectionType = .timezone @@ -57,7 +65,6 @@ class TimezoneData: NSObject, NSCoding { note = CLEmptyString isSystemTimezone = false overrideFormat = .globalFormat - overrideSecondsFormat = .globalFormat placeID = UUID().uuidString } @@ -84,7 +91,6 @@ class TimezoneData: NSObject, NSCoding { selectionType = originalTimezone.selectionType == CLSelection.citySelection ? .city : .timezone isSystemTimezone = originalTimezone.isSystemTimezone overrideFormat = .globalFormat - overrideSecondsFormat = .globalFormat } init(with dictionary: [String: Any]) { @@ -137,7 +143,6 @@ class TimezoneData: NSObject, NSCoding { isSystemTimezone = false overrideFormat = .globalFormat - overrideSecondsFormat = .globalFormat } required init?(coder aDecoder: NSCoder) { @@ -170,9 +175,6 @@ class TimezoneData: NSObject, NSCoding { let override = aDecoder.decodeInteger(forKey: "overrideFormat") overrideFormat = TimezoneOverride(rawValue: override)! - - let secondsOverride = aDecoder.decodeInteger(forKey: "secondsOverrideFormat") - overrideSecondsFormat = SecondsOverride(rawValue: secondsOverride)! } class func customObject(from encodedData: Data?) -> TimezoneData? { @@ -235,7 +237,6 @@ class TimezoneData: NSObject, NSCoding { let newTimezone = TimezoneData(with: oldModel) newModels.append(newTimezone) } else if let newModel = old as? TimezoneData { - shouldOverrideSecondsFormatBugFix(model: newModel) newModels.append(newModel) } } @@ -252,12 +253,6 @@ class TimezoneData: NSObject, NSCoding { return serializedModels } - private class func shouldOverrideSecondsFormatBugFix(model: TimezoneData) { - if UserDefaults.standard.object(forKey: "shouldOverrideSecondsFormatBug") == nil { - model.setShouldOverrideSecondsFormat(2) - } - } - func encode(with aCoder: NSCoder) { aCoder.encode(placeID, forKey: "place_id") @@ -286,8 +281,6 @@ class TimezoneData: NSObject, NSCoding { aCoder.encode(isSystemTimezone, forKey: "isSystemTimezone") aCoder.encode(overrideFormat.rawValue, forKey: "overrideFormat") - - aCoder.encode(overrideSecondsFormat.rawValue, forKey: "secondsOverrideFormat") } func formattedTimezoneLabel() -> String { @@ -332,16 +325,6 @@ class TimezoneData: NSObject, NSCoding { } } - func setShouldOverrideSecondsFormat(_ shouldOverride: Int) { - if shouldOverride == 0 { - overrideSecondsFormat = .showSeconds - } else if shouldOverride == 1 { - overrideSecondsFormat = .hideSeconds - } else { - overrideSecondsFormat = .globalFormat - } - } - func timezone() -> String { if isSystemTimezone { timezoneID = TimeZone.autoupdatingCurrent.identifier @@ -367,46 +350,45 @@ class TimezoneData: NSObject, NSCoding { } func timezoneFormat() -> String { - let showSeconds = shouldShowSeconds() - let isTwelveHourFormatSelected = DataStore.shared().shouldDisplay(.twelveHour) - - var timeFormat = DateFormat.twentyFourHour - - if showSeconds { - if overrideFormat == .globalFormat { - timeFormat = isTwelveHourFormatSelected ? DateFormat.twelveHourWithSeconds : DateFormat.twentyFourHourWithSeconds - } else if overrideFormat == .twelveHourFormat { - timeFormat = DateFormat.twelveHourWithSeconds - } else { - timeFormat = DateFormat.twentyFourHourWithSeconds - } - } else { - if overrideFormat == .globalFormat { - timeFormat = isTwelveHourFormatSelected ? DateFormat.twelveHour : DateFormat.twentyFourHour - } else if overrideFormat == .twelveHourFormat { - timeFormat = DateFormat.twelveHour - } else { - timeFormat = DateFormat.twentyFourHour - } - } - - return timeFormat - } - - func shouldDisplayTwelveHourFormat() -> Bool { - if overrideSecondsFormat == .globalFormat { - return DataStore.shared().shouldDisplay(.twelveHour) - } - - return overrideFormat == .twelveHourFormat + let chosenDefault = DataStore.shared().timezoneFormat() + + let currentValue = values[chosenDefault] ?? DateFormat.twelveHour + + Swift.print("Format is \(currentValue)") + return currentValue + +// var timeFormat = DateFormat.twentyFourHour +// +// if shouldShowSeconds() { +// if overrideFormat == .globalFormat { +// timeFormat = true ? DateFormat.twelveHourWithSeconds : DateFormat.twentyFourHourWithSeconds +// } else if overrideFormat == .twelveHourFormat { +// timeFormat = DateFormat.twelveHourWithSeconds +// } else { +// timeFormat = DateFormat.twentyFourHourWithSeconds +// } +// } else { +// if overrideFormat == .globalFormat { +// timeFormat = true ? DateFormat.twelveHour : DateFormat.twentyFourHour +// } else if overrideFormat == .twelveHourFormat { +// timeFormat = DateFormat.twelveHour +// } else { +// timeFormat = DateFormat.twentyFourHour +// } +// } +// +// return timeFormat } func shouldShowSeconds() -> Bool { - if overrideSecondsFormat == .globalFormat { - return DataStore.shared().shouldDisplay(.seconds) + if overrideFormat == .globalFormat { + let currentFormat = DataStore.shared().timezoneFormat() + let formatInString = values[currentFormat] ?? DateFormat.twelveHour + return formatInString.contains("ss") } - return overrideSecondsFormat == .showSeconds + let formatInString = values[NSNumber(integerLiteral: overrideFormat.rawValue)] ?? DateFormat.twelveHour + return formatInString.contains("ss") } override var hash: Int { @@ -472,7 +454,6 @@ extension TimezoneData { Note: \(note ?? "Error") Is System Timezone: \(isSystemTimezone) Override: \(overrideFormat) - Seconds Override: \(overrideSecondsFormat) """ return customString diff --git a/Clocker/Panel/Notes Popover/NotesPopover.swift b/Clocker/Panel/Notes Popover/NotesPopover.swift index 6b68ef4..b9f1fa7 100644 --- a/Clocker/Panel/Notes Popover/NotesPopover.swift +++ b/Clocker/Panel/Notes Popover/NotesPopover.swift @@ -215,7 +215,6 @@ class NotesPopover: NSViewController { dataObject?.note = notesTextView.string dataObject?.setShouldOverrideGlobalTimeFormat(timeFormatControl.selectedSegment) - dataObject?.setShouldOverrideSecondsFormat(secondsFormatControl.selectedSegment) insertTimezoneInDefaultPreferences() if setReminderCheckbox.state == .on { @@ -285,7 +284,8 @@ class NotesPopover: NSViewController { DataStore.shared().setTimezones(timezones) } - private func updateTimezoneInDefaultPreferences(with override: Int, _ overrideType: OverrideType) { + private func updateTimezoneInDefaultPreferences(with override: Int, + _: OverrideType) { let timezones = DataStore.shared().timezones() var timezoneObjects: [TimezoneData] = [] @@ -297,9 +297,7 @@ class NotesPopover: NSViewController { } for timezoneObject in timezoneObjects where timezoneObject.isEqual(dataObject) { - overrideType == .timezoneFormat ? - timezoneObject.setShouldOverrideGlobalTimeFormat(override) : - timezoneObject.setShouldOverrideSecondsFormat(override) + timezoneObject.setShouldOverrideGlobalTimeFormat(override) } var datas: [Data] = [] @@ -452,7 +450,6 @@ extension NotesPopover { setInitialReminderTime() updateTimeFormat() - updateSecondsFormat() } private func updateTimeFormat() { @@ -461,12 +458,6 @@ extension NotesPopover { } } - private func updateSecondsFormat() { - if let overrideFormat = dataObject?.overrideSecondsFormat.rawValue { - secondsFormatControl.setSelected(true, forSegment: overrideFormat) - } - } - private func enableReminderView(_ shouldEnable: Bool) { reminderPicker.isEnabled = shouldEnable alertPopupButton.isEnabled = shouldEnable diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 2c535b5..b467a11 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -25,15 +25,27 @@ class AppearanceViewController: ParentViewController { informationLabel.stringValue = "Favourite a timezone to enable menubar display options.".localized() informationLabel.textColor = NSColor.secondaryLabelColor + let chosenFormat = DataStore.shared().timezoneFormat().intValue let supportedTimeFormats = ["h:mm a (7:08 PM)", - "h:mm:ss a (7:08:09 PM)", "HH:mm (19:08)", + "-- With Seconds --", + "h:mm:ss a (7:08:09 PM)", "HH:mm:ss (19:08:09)", + "-- 12 Hour with Preceding 0 --", "hh:mm a (07:08 PM)", - "hh:mm:ss a (07:08:09 PM)"] + "hh:mm:ss a (07:08:09 PM)", + "-- 12 Hour w/o AM/PM --", + "hh:mm (07:08)", + "hh:mm:ss (07:08:09)"] timeFormat.removeAllItems() timeFormat.addItems(withTitles: supportedTimeFormats) + timeFormat.item(at: 2)?.isEnabled = false + timeFormat.item(at: 5)?.isEnabled = false + timeFormat.item(at: 8)?.isEnabled = false + timeFormat.autoenablesItems = false + timeFormat.selectItem(at: chosenFormat) + informationLabel.setAccessibilityIdentifier("InformationLabel") sliderDayRangePopup.removeAllItems() diff --git a/Clocker/Preferences/Preferences.storyboard b/Clocker/Preferences/Preferences.storyboard index 1568d6e..d83d4bf 100644 --- a/Clocker/Preferences/Preferences.storyboard +++ b/Clocker/Preferences/Preferences.storyboard @@ -193,7 +193,7 @@ - + @@ -213,7 +213,7 @@ - + @@ -277,10 +277,10 @@ - + - + When selected, your upcoming meeting title will appear in the menubar 30 mins before it starts. All Day Events won't be shown in the menubar! @@ -298,7 +298,7 @@ - + @@ -315,7 +315,7 @@ - + @@ -327,7 +327,7 @@ - + @@ -339,7 +339,7 @@ - + @@ -358,7 +358,7 @@ - + @@ -366,10 +366,10 @@ - + - + @@ -386,19 +386,19 @@ - + - + - + - + @@ -411,7 +411,7 @@ - + @@ -434,7 +434,7 @@ - + - + @@ -494,10 +494,10 @@