Browse Source

Removing DataStore dep from the model!

pull/92/head
Abhishek 4 years ago
parent
commit
a0f73b734f
  1. 76
      Clocker/Panel/Data Layer/TimezoneData.swift
  2. 2
      Clocker/Panel/Data Layer/TimezoneDataOperations.swift
  3. 6
      Clocker/Preferences/Menu Bar/StatusContainerView.swift
  4. 2
      Clocker/Preferences/Menu Bar/StatusItemHandler.swift

76
Clocker/Panel/Data Layer/TimezoneData.swift

@ -3,25 +3,25 @@
import Cocoa import Cocoa
import CoreLoggerKit import CoreLoggerKit
struct DateFormat { public struct DateFormat {
static let twelveHour = "h:mm a" public static let twelveHour = "h:mm a"
static let twelveHourWithSeconds = "h:mm:ss a" public static let twelveHourWithSeconds = "h:mm:ss a"
static let twentyFourHour = "HH:mm" public static let twentyFourHour = "HH:mm"
static let twentyFourHourWithSeconds = "HH:mm:ss" public static let twentyFourHourWithSeconds = "HH:mm:ss"
static let twelveHourWithZero = "hh:mm a" public static let twelveHourWithZero = "hh:mm a"
static let twelveHourWithZeroSeconds = "hh:mm:ss a" public static let twelveHourWithZeroSeconds = "hh:mm:ss a"
static let twelveHourWithoutSuffix = "hh:mm" public static let twelveHourWithoutSuffix = "hh:mm"
static let twelveHourWithoutSuffixAndSeconds = "hh:mm:ss" public static let twelveHourWithoutSuffixAndSeconds = "hh:mm:ss"
} }
// Non-class type cannot conform to NSCoding! // Non-class type cannot conform to NSCoding!
class TimezoneData: NSObject, NSCoding { class TimezoneData: NSObject, NSCoding {
enum SelectionType: Int { public enum SelectionType: Int {
case city case city
case timezone case timezone
} }
enum DateDisplayType: Int { public enum DateDisplayType: Int {
case panel case panel
case menu case menu
} }
@ -71,7 +71,7 @@ class TimezoneData: NSObject, NSCoding {
public var isSystemTimezone = false public var isSystemTimezone = false
public var overrideFormat: TimezoneOverride = .globalFormat public var overrideFormat: TimezoneOverride = .globalFormat
override init() { public override init() {
selectionType = .timezone selectionType = .timezone
isFavourite = 0 isFavourite = 0
note = CLEmptyString note = CLEmptyString
@ -80,7 +80,7 @@ class TimezoneData: NSObject, NSCoding {
placeID = UUID().uuidString placeID = UUID().uuidString
} }
init(with dictionary: [String: Any]) { public init(with dictionary: [String: Any]) {
if let label = dictionary[CLCustomLabel] as? String { if let label = dictionary[CLCustomLabel] as? String {
customLabel = label customLabel = label
} else { } else {
@ -164,7 +164,7 @@ class TimezoneData: NSObject, NSCoding {
overrideFormat = TimezoneOverride(rawValue: override)! overrideFormat = TimezoneOverride(rawValue: override)!
} }
class func customObject(from encodedData: Data?) -> TimezoneData? { public class func customObject(from encodedData: Data?) -> TimezoneData? {
guard let dataObject = encodedData else { guard let dataObject = encodedData else {
return TimezoneData() return TimezoneData()
} }
@ -176,33 +176,6 @@ class TimezoneData: NSObject, NSCoding {
return nil 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) { func encode(with aCoder: NSCoder) {
aCoder.encode(placeID, forKey: "place_id") aCoder.encode(placeID, forKey: "place_id")
@ -233,7 +206,7 @@ class TimezoneData: NSObject, NSCoding {
aCoder.encode(overrideFormat.rawValue, forKey: "overrideFormat") aCoder.encode(overrideFormat.rawValue, forKey: "overrideFormat")
} }
func formattedTimezoneLabel() -> String { public func formattedTimezoneLabel() -> String {
// First check if there's an user preferred custom label set // First check if there's an user preferred custom label set
if let label = customLabel, !label.isEmpty { if let label = customLabel, !label.isEmpty {
return label return label
@ -261,11 +234,11 @@ class TimezoneData: NSObject, NSCoding {
return "Error" return "Error"
} }
func setLabel(_ label: String) { public func setLabel(_ label: String) {
customLabel = !label.isEmpty ? label : CLEmptyString customLabel = !label.isEmpty ? label : CLEmptyString
} }
func setShouldOverrideGlobalTimeFormat(_ shouldOverride: Int) { public func setShouldOverrideGlobalTimeFormat(_ shouldOverride: Int) {
if shouldOverride == 0 { if shouldOverride == 0 {
overrideFormat = .globalFormat overrideFormat = .globalFormat
} else if shouldOverride == 1 { } else if shouldOverride == 1 {
@ -290,7 +263,7 @@ class TimezoneData: NSObject, NSCoding {
} }
} }
func timezone() -> String { public func timezone() -> String {
if isSystemTimezone { if isSystemTimezone {
timezoneID = TimeZone.autoupdatingCurrent.identifier timezoneID = TimeZone.autoupdatingCurrent.identifier
formattedAddress = TimeZone.autoupdatingCurrent.identifier formattedAddress = TimeZone.autoupdatingCurrent.identifier
@ -314,8 +287,8 @@ class TimezoneData: NSObject, NSCoding {
return TimeZone.autoupdatingCurrent.identifier return TimeZone.autoupdatingCurrent.identifier
} }
func timezoneFormat() -> String { public func timezoneFormat(_ currentFormat: NSNumber) -> String {
let chosenDefault = DataStore.shared().timezoneFormat() let chosenDefault = currentFormat
let timeFormat = TimezoneData.values[chosenDefault] ?? DateFormat.twelveHour let timeFormat = TimezoneData.values[chosenDefault] ?? DateFormat.twelveHour
if overrideFormat == .globalFormat { if overrideFormat == .globalFormat {
@ -341,9 +314,8 @@ class TimezoneData: NSObject, NSCoding {
return timeFormat return timeFormat
} }
func shouldShowSeconds() -> Bool { public func shouldShowSeconds(_ currentFormat: NSNumber) -> Bool {
if overrideFormat == .globalFormat { if overrideFormat == .globalFormat {
let currentFormat = DataStore.shared().timezoneFormat()
let formatInString = TimezoneData.values[currentFormat] ?? DateFormat.twelveHour let formatInString = TimezoneData.values[currentFormat] ?? DateFormat.twelveHour
return formatInString.contains("ss") return formatInString.contains("ss")
} }
@ -352,7 +324,7 @@ class TimezoneData: NSObject, NSCoding {
return formatInString.contains("ss") return formatInString.contains("ss")
} }
override var hash: Int { public override var hash: Int {
guard let placeIdentifier = placeID, let timezone = timezoneID else { guard let placeIdentifier = placeID, let timezone = timezoneID else {
return -1 return -1
} }
@ -364,14 +336,14 @@ class TimezoneData: NSObject, NSCoding {
return lhs.placeID == rhs.placeID 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 { if let other = object as? TimezoneData {
return placeID == other.placeID return placeID == other.placeID
} }
return false return false
} }
override func isEqual(_ object: Any?) -> Bool { public override func isEqual(_ object: Any?) -> Bool {
guard let compared = object as? TimezoneData else { guard let compared = object as? TimezoneData else {
return false return false
} }

2
Clocker/Panel/Data Layer/TimezoneDataOperations.swift

@ -27,7 +27,7 @@ extension TimezoneDataOperations {
} }
let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none, let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none,
format: dataObject.timezoneFormat(), format: dataObject.timezoneFormat(DataStore.shared().timezoneFormat()),
timezoneIdentifier: dataObject.timezone(), timezoneIdentifier: dataObject.timezone(),
locale: Locale.autoupdatingCurrent) locale: Locale.autoupdatingCurrent)

6
Clocker/Preferences/Menu Bar/StatusContainerView.swift

@ -23,7 +23,7 @@ func bufferCalculatedWidth() -> Int {
func compactWidth(for timezone: TimezoneData) -> Int { func compactWidth(for timezone: TimezoneData) -> Int {
var totalWidth = 55 var totalWidth = 55
let timeFormat = timezone.timezoneFormat() let timeFormat = timezone.timezoneFormat(DataStore.shared().timezoneFormat())
if DataStore.shared().shouldShowDayInMenubar() { if DataStore.shared().shouldShowDayInMenubar() {
totalWidth += 12 totalWidth += 12
@ -39,7 +39,7 @@ func compactWidth(for timezone: TimezoneData) -> Int {
totalWidth += 0 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 // Slight buffer needed when the Menubar supplementary text was Mon 9:27:58 AM
totalWidth += 15 totalWidth += 15
} }
@ -87,7 +87,7 @@ class StatusContainerView: NSView {
let operationObject = TimezoneDataOperations(with: timezoneObject) let operationObject = TimezoneDataOperations(with: timezoneObject)
let calculatedSubtitleSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeBasedAttributes) let calculatedSubtitleSize = compactModeTimeFont.size(operationObject.compactMenuSubtitle(), precalculatedWidth, attributes: timeBasedAttributes)
let calculatedTitleSize = compactModeTimeFont.size(operationObject.compactMenuTitle(), 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 let secondsBuffer: CGFloat = showSeconds ? 7 : 0
return result + max(calculatedTitleSize.width, calculatedSubtitleSize.width) + bufferWidth + secondsBuffer return result + max(calculatedTitleSize.width, calculatedSubtitleSize.width) + bufferWidth + secondsBuffer
} }

2
Clocker/Preferences/Menu Bar/StatusItemHandler.swift

@ -204,7 +204,7 @@ class StatusItemHandler: NSObject {
for timezone in syncedTimezones { for timezone in syncedTimezones {
if let timezoneObj = TimezoneData.customObject(from: timezone) { if let timezoneObj = TimezoneData.customObject(from: timezone) {
let shouldShowSeconds = timezoneObj.shouldShowSeconds() let shouldShowSeconds = timezoneObj.shouldShowSeconds(DataStore.shared().timezoneFormat())
if shouldShowSeconds { if shouldShowSeconds {
return true return true
} }

Loading…
Cancel
Save