|
|
@ -7,13 +7,15 @@ import CoreModelKit |
|
|
|
|
|
|
|
|
|
|
|
class TimezoneDataOperations: NSObject { |
|
|
|
class TimezoneDataOperations: NSObject { |
|
|
|
private let dataObject: TimezoneData |
|
|
|
private let dataObject: TimezoneData |
|
|
|
|
|
|
|
private let store: DataStore |
|
|
|
private lazy var nsCalendar = Calendar.autoupdatingCurrent |
|
|
|
private lazy var nsCalendar = Calendar.autoupdatingCurrent |
|
|
|
private static var gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian) |
|
|
|
private static var gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian) |
|
|
|
private static var swiftyCalendar = Calendar(identifier: .gregorian) |
|
|
|
private static var swiftyCalendar = Calendar(identifier: .gregorian) |
|
|
|
private static let currentLocale = Locale.current.identifier |
|
|
|
private static let currentLocale = Locale.current.identifier |
|
|
|
|
|
|
|
|
|
|
|
init(with timezone: TimezoneData) { |
|
|
|
init(with timezone: TimezoneData, store: DataStore) { |
|
|
|
dataObject = timezone |
|
|
|
self.dataObject = timezone |
|
|
|
|
|
|
|
self.store = store |
|
|
|
super.init() |
|
|
|
super.init() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -29,7 +31,7 @@ extension TimezoneDataOperations { |
|
|
|
return CLEmptyString |
|
|
|
return CLEmptyString |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if dataObject.timezoneFormat(DataStore.shared().timezoneFormat()) == DateFormat.epochTime { |
|
|
|
if dataObject.timezoneFormat(store.timezoneFormat()) == DateFormat.epochTime { |
|
|
|
let timezone = TimeZone(identifier: dataObject.timezone()) |
|
|
|
let timezone = TimeZone(identifier: dataObject.timezone()) |
|
|
|
let offset = timezone?.secondsFromGMT(for: newDate) ?? 0 |
|
|
|
let offset = timezone?.secondsFromGMT(for: newDate) ?? 0 |
|
|
|
let value = Int(Date().timeIntervalSince1970 + Double(offset)) |
|
|
|
let value = Int(Date().timeIntervalSince1970 + Double(offset)) |
|
|
@ -37,7 +39,7 @@ extension TimezoneDataOperations { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none, |
|
|
|
let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none, |
|
|
|
format: dataObject.timezoneFormat(DataStore.shared().timezoneFormat()), |
|
|
|
format: dataObject.timezoneFormat(store.timezoneFormat()), |
|
|
|
timezoneIdentifier: dataObject.timezone(), |
|
|
|
timezoneIdentifier: dataObject.timezone(), |
|
|
|
locale: Locale.autoupdatingCurrent) |
|
|
|
locale: Locale.autoupdatingCurrent) |
|
|
|
|
|
|
|
|
|
|
@ -81,15 +83,15 @@ extension TimezoneDataOperations { |
|
|
|
func compactMenuTitle() -> String { |
|
|
|
func compactMenuTitle() -> String { |
|
|
|
var subtitle = CLEmptyString |
|
|
|
var subtitle = CLEmptyString |
|
|
|
|
|
|
|
|
|
|
|
let shouldDayBeShown = DataStore.shared().shouldShowDayInMenubar() |
|
|
|
let shouldDayBeShown = store.shouldShowDayInMenubar() |
|
|
|
let shouldLabelBeShownAlongWithTime = !DataStore.shared().shouldDisplay(.placeInMenubar) |
|
|
|
let shouldLabelBeShownAlongWithTime = !store.shouldDisplay(.placeInMenubar) |
|
|
|
|
|
|
|
|
|
|
|
if shouldDayBeShown, shouldLabelBeShownAlongWithTime { |
|
|
|
if shouldDayBeShown, shouldLabelBeShownAlongWithTime { |
|
|
|
let substring = date(with: 0, displayType: .menu) |
|
|
|
let substring = date(with: 0, displayType: .menu) |
|
|
|
subtitle.append(substring) |
|
|
|
subtitle.append(substring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let shouldDateBeShown = DataStore.shared().shouldShowDateInMenubar() |
|
|
|
let shouldDateBeShown = store.shouldShowDateInMenubar() |
|
|
|
if shouldDateBeShown, shouldLabelBeShownAlongWithTime { |
|
|
|
if shouldDateBeShown, shouldLabelBeShownAlongWithTime { |
|
|
|
let date = Date().formatter(with: "MMM d", timeZone: dataObject.timezone()) |
|
|
|
let date = Date().formatter(with: "MMM d", timeZone: dataObject.timezone()) |
|
|
|
subtitle.isEmpty ? subtitle.append("\(date)") : subtitle.append(" \(date)") |
|
|
|
subtitle.isEmpty ? subtitle.append("\(date)") : subtitle.append(" \(date)") |
|
|
@ -101,15 +103,15 @@ extension TimezoneDataOperations { |
|
|
|
func compactMenuSubtitle() -> String { |
|
|
|
func compactMenuSubtitle() -> String { |
|
|
|
var subtitle = CLEmptyString |
|
|
|
var subtitle = CLEmptyString |
|
|
|
|
|
|
|
|
|
|
|
let shouldDayBeShown = DataStore.shared().shouldShowDayInMenubar() |
|
|
|
let shouldDayBeShown = store.shouldShowDayInMenubar() |
|
|
|
let shouldLabelsNotBeShownAlongWithTime = DataStore.shared().shouldDisplay(.placeInMenubar) |
|
|
|
let shouldLabelsNotBeShownAlongWithTime = store.shouldDisplay(.placeInMenubar) |
|
|
|
|
|
|
|
|
|
|
|
if shouldDayBeShown, shouldLabelsNotBeShownAlongWithTime { |
|
|
|
if shouldDayBeShown, shouldLabelsNotBeShownAlongWithTime { |
|
|
|
let substring = date(with: 0, displayType: .menu) |
|
|
|
let substring = date(with: 0, displayType: .menu) |
|
|
|
subtitle.append(substring) |
|
|
|
subtitle.append(substring) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let shouldDateBeShown = DataStore.shared().shouldShowDateInMenubar() |
|
|
|
let shouldDateBeShown = store.shouldShowDateInMenubar() |
|
|
|
if shouldDateBeShown, shouldLabelsNotBeShownAlongWithTime { |
|
|
|
if shouldDateBeShown, shouldLabelsNotBeShownAlongWithTime { |
|
|
|
let date = Date().formatter(with: "MMM d", timeZone: dataObject.timezone()) |
|
|
|
let date = Date().formatter(with: "MMM d", timeZone: dataObject.timezone()) |
|
|
|
subtitle.isEmpty ? subtitle.append("\(date)") : subtitle.append(" \(date)") |
|
|
|
subtitle.isEmpty ? subtitle.append("\(date)") : subtitle.append(" \(date)") |
|
|
@ -123,7 +125,7 @@ extension TimezoneDataOperations { |
|
|
|
func menuTitle() -> String { |
|
|
|
func menuTitle() -> String { |
|
|
|
var menuTitle = CLEmptyString |
|
|
|
var menuTitle = CLEmptyString |
|
|
|
|
|
|
|
|
|
|
|
let dataStore = DataStore.shared() |
|
|
|
let dataStore = store |
|
|
|
|
|
|
|
|
|
|
|
let shouldCityBeShown = dataStore.shouldDisplay(.placeInMenubar) |
|
|
|
let shouldCityBeShown = dataStore.shouldDisplay(.placeInMenubar) |
|
|
|
let shouldDayBeShown = dataStore.shouldShowDayInMenubar() |
|
|
|
let shouldDayBeShown = dataStore.shouldShowDayInMenubar() |
|
|
@ -200,7 +202,7 @@ extension TimezoneDataOperations { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func date(with sliderValue: Int, displayType: TimezoneData.DateDisplayType) -> String { |
|
|
|
func date(with sliderValue: Int, displayType: TimezoneData.DateDisplayType) -> String { |
|
|
|
guard let relativeDayPreference = DataStore.shared().retrieve(key: CLRelativeDateKey) as? NSNumber else { |
|
|
|
guard let relativeDayPreference = store.retrieve(key: CLRelativeDateKey) as? NSNumber else { |
|
|
|
assertionFailure("Data was unexpectedly nil") |
|
|
|
assertionFailure("Data was unexpectedly nil") |
|
|
|
return CLEmptyString |
|
|
|
return CLEmptyString |
|
|
|
} |
|
|
|
} |
|
|
@ -389,7 +391,7 @@ extension TimezoneDataOperations { |
|
|
|
let dateFormatter = DateFormatter() |
|
|
|
let dateFormatter = DateFormatter() |
|
|
|
dateFormatter.locale = Locale(identifier: "en_US") |
|
|
|
dateFormatter.locale = Locale(identifier: "en_US") |
|
|
|
dateFormatter.timeZone = TimeZone(identifier: dataObject.timezone()) |
|
|
|
dateFormatter.timeZone = TimeZone(identifier: dataObject.timezone()) |
|
|
|
dateFormatter.dateFormat = dataObject.timezoneFormat(DataStore.shared().timezoneFormat()) |
|
|
|
dateFormatter.dateFormat = dataObject.timezoneFormat(store.timezoneFormat()) |
|
|
|
return dateFormatter.string(from: correct) |
|
|
|
return dateFormatter.string(from: correct) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -418,10 +420,10 @@ extension TimezoneDataOperations { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func saveObject(at index: Int = -1) { |
|
|
|
func saveObject(at index: Int = -1) { |
|
|
|
var defaults = DataStore.shared().timezones() |
|
|
|
var defaults = store.timezones() |
|
|
|
let encodedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject as Any) |
|
|
|
let encodedObject = NSKeyedArchiver.archivedData(withRootObject: dataObject as Any) |
|
|
|
index == -1 ? defaults.append(encodedObject) : defaults.insert(encodedObject, at: index) |
|
|
|
index == -1 ? defaults.append(encodedObject) : defaults.insert(encodedObject, at: index) |
|
|
|
DataStore.shared().setTimezones(defaults) |
|
|
|
store.setTimezones(defaults) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|