From a0f73b734fe9dd2b100aa02a35dff00d2a056e7d Mon Sep 17 00:00:00 2001 From: Abhishek Date: Mon, 28 Dec 2020 09:06:39 -0600 Subject: [PATCH] Removing DataStore dep from the model! --- Clocker/Panel/Data Layer/TimezoneData.swift | 76 ++++++------------- .../Data Layer/TimezoneDataOperations.swift | 2 +- .../Menu Bar/StatusContainerView.swift | 6 +- .../Menu Bar/StatusItemHandler.swift | 2 +- 4 files changed, 29 insertions(+), 57 deletions(-) diff --git a/Clocker/Panel/Data Layer/TimezoneData.swift b/Clocker/Panel/Data Layer/TimezoneData.swift index c4e3bc1..65bac35 100644 --- a/Clocker/Panel/Data Layer/TimezoneData.swift +++ b/Clocker/Panel/Data Layer/TimezoneData.swift @@ -3,25 +3,25 @@ import Cocoa import CoreLoggerKit -struct DateFormat { - static let twelveHour = "h:mm a" - 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" +public struct DateFormat { + public static let twelveHour = "h:mm a" + public static let twelveHourWithSeconds = "h:mm:ss a" + public static let twentyFourHour = "HH:mm" + public static let twentyFourHourWithSeconds = "HH:mm:ss" + public static let twelveHourWithZero = "hh:mm a" + public static let twelveHourWithZeroSeconds = "hh:mm:ss a" + public static let twelveHourWithoutSuffix = "hh:mm" + public static let twelveHourWithoutSuffixAndSeconds = "hh:mm:ss" } // Non-class type cannot conform to NSCoding! class TimezoneData: NSObject, NSCoding { - enum SelectionType: Int { + public enum SelectionType: Int { case city case timezone } - enum DateDisplayType: Int { + public enum DateDisplayType: Int { case panel case menu } @@ -71,7 +71,7 @@ class TimezoneData: NSObject, NSCoding { public var isSystemTimezone = false public var overrideFormat: TimezoneOverride = .globalFormat - override init() { + public override init() { selectionType = .timezone isFavourite = 0 note = CLEmptyString @@ -80,7 +80,7 @@ class TimezoneData: NSObject, NSCoding { placeID = UUID().uuidString } - init(with dictionary: [String: Any]) { + public init(with dictionary: [String: Any]) { if let label = dictionary[CLCustomLabel] as? String { customLabel = label } else { @@ -164,7 +164,7 @@ class TimezoneData: NSObject, NSCoding { overrideFormat = TimezoneOverride(rawValue: override)! } - class func customObject(from encodedData: Data?) -> TimezoneData? { + public class func customObject(from encodedData: Data?) -> TimezoneData? { guard let dataObject = encodedData else { return TimezoneData() } @@ -176,33 +176,6 @@ class TimezoneData: NSObject, NSCoding { return nil } - private class func converter(_ timezones: [Data]) -> [Data] { - var newModels: [TimezoneData] = [] - - for timezone in timezones { - // Get the old (aka CLTimezoneData) model object - let old = NSKeyedUnarchiver.unarchiveObject(with: timezone) - if let newModel = old as? Clocker.TimezoneData { - if UserDefaults.standard.object(forKey: "migrateOverrideFormat") == nil { - print("Resetting Global Format") - newModel.setShouldOverrideGlobalTimeFormat(0) - } - newModels.append(newModel) - } - } - - if UserDefaults.standard.object(forKey: "migrateOverrideFormat") == nil { - UserDefaults.standard.set("YES", forKey: "migrateOverrideFormat") - } - - // Do the serialization - let serializedModels = newModels.map { (place) -> Data in - NSKeyedArchiver.archivedData(withRootObject: place) - } - - return serializedModels - } - func encode(with aCoder: NSCoder) { aCoder.encode(placeID, forKey: "place_id") @@ -233,7 +206,7 @@ class TimezoneData: NSObject, NSCoding { aCoder.encode(overrideFormat.rawValue, forKey: "overrideFormat") } - func formattedTimezoneLabel() -> String { + public func formattedTimezoneLabel() -> String { // First check if there's an user preferred custom label set if let label = customLabel, !label.isEmpty { return label @@ -261,11 +234,11 @@ class TimezoneData: NSObject, NSCoding { return "Error" } - func setLabel(_ label: String) { + public func setLabel(_ label: String) { customLabel = !label.isEmpty ? label : CLEmptyString } - func setShouldOverrideGlobalTimeFormat(_ shouldOverride: Int) { + public func setShouldOverrideGlobalTimeFormat(_ shouldOverride: Int) { if shouldOverride == 0 { overrideFormat = .globalFormat } else if shouldOverride == 1 { @@ -290,7 +263,7 @@ class TimezoneData: NSObject, NSCoding { } } - func timezone() -> String { + public func timezone() -> String { if isSystemTimezone { timezoneID = TimeZone.autoupdatingCurrent.identifier formattedAddress = TimeZone.autoupdatingCurrent.identifier @@ -314,8 +287,8 @@ class TimezoneData: NSObject, NSCoding { return TimeZone.autoupdatingCurrent.identifier } - func timezoneFormat() -> String { - let chosenDefault = DataStore.shared().timezoneFormat() + public func timezoneFormat(_ currentFormat: NSNumber) -> String { + let chosenDefault = currentFormat let timeFormat = TimezoneData.values[chosenDefault] ?? DateFormat.twelveHour if overrideFormat == .globalFormat { @@ -341,9 +314,8 @@ class TimezoneData: NSObject, NSCoding { return timeFormat } - func shouldShowSeconds() -> Bool { + public func shouldShowSeconds(_ currentFormat: NSNumber) -> Bool { if overrideFormat == .globalFormat { - let currentFormat = DataStore.shared().timezoneFormat() let formatInString = TimezoneData.values[currentFormat] ?? DateFormat.twelveHour return formatInString.contains("ss") } @@ -352,7 +324,7 @@ class TimezoneData: NSObject, NSCoding { return formatInString.contains("ss") } - override var hash: Int { + public override var hash: Int { guard let placeIdentifier = placeID, let timezone = timezoneID else { return -1 } @@ -364,14 +336,14 @@ class TimezoneData: NSObject, NSCoding { return lhs.placeID == rhs.placeID } - override func isEqual(to object: Any?) -> Bool { + public 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 { + public override func isEqual(_ object: Any?) -> Bool { guard let compared = object as? TimezoneData else { return false } diff --git a/Clocker/Panel/Data Layer/TimezoneDataOperations.swift b/Clocker/Panel/Data Layer/TimezoneDataOperations.swift index 2571f9a..2b5f6f1 100644 --- a/Clocker/Panel/Data Layer/TimezoneDataOperations.swift +++ b/Clocker/Panel/Data Layer/TimezoneDataOperations.swift @@ -27,7 +27,7 @@ extension TimezoneDataOperations { } let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none, - format: dataObject.timezoneFormat(), + format: dataObject.timezoneFormat(DataStore.shared().timezoneFormat()), timezoneIdentifier: dataObject.timezone(), locale: Locale.autoupdatingCurrent) diff --git a/Clocker/Preferences/Menu Bar/StatusContainerView.swift b/Clocker/Preferences/Menu Bar/StatusContainerView.swift index e6c060b..1e991cc 100644 --- a/Clocker/Preferences/Menu Bar/StatusContainerView.swift +++ b/Clocker/Preferences/Menu Bar/StatusContainerView.swift @@ -23,7 +23,7 @@ func bufferCalculatedWidth() -> Int { func compactWidth(for timezone: TimezoneData) -> Int { var totalWidth = 55 - let timeFormat = timezone.timezoneFormat() + let timeFormat = timezone.timezoneFormat(DataStore.shared().timezoneFormat()) if DataStore.shared().shouldShowDayInMenubar() { totalWidth += 12 @@ -39,7 +39,7 @@ func compactWidth(for timezone: TimezoneData) -> Int { totalWidth += 0 } - if timezone.shouldShowSeconds() { + if timezone.shouldShowSeconds(DataStore.shared().timezoneFormat()) { // Slight buffer needed when the Menubar supplementary text was Mon 9:27:58 AM totalWidth += 15 } @@ -87,7 +87,7 @@ class StatusContainerView: NSView { let operationObject = TimezoneDataOperations(with: timezoneObject) let calculatedSubtitleSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeBasedAttributes) let calculatedTitleSize = compactModeTimeFont.size(operationObject.compactMenuTitle(), precalculatedWidth, attributes: timeBasedAttributes) - let showSeconds = timezoneObject.shouldShowSeconds() + let showSeconds = timezoneObject.shouldShowSeconds(DataStore.shared().timezoneFormat()) let secondsBuffer: CGFloat = showSeconds ? 7 : 0 return result + max(calculatedTitleSize.width, calculatedSubtitleSize.width) + bufferWidth + secondsBuffer } diff --git a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift index ca7e895..88e9379 100644 --- a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift @@ -204,7 +204,7 @@ class StatusItemHandler: NSObject { for timezone in syncedTimezones { if let timezoneObj = TimezoneData.customObject(from: timezone) { - let shouldShowSeconds = timezoneObj.shouldShowSeconds() + let shouldShowSeconds = timezoneObj.shouldShowSeconds(DataStore.shared().timezoneFormat()) if shouldShowSeconds { return true }