|
|
@ -18,12 +18,13 @@ enum ViewType { |
|
|
|
case dayInMenubar |
|
|
|
case dayInMenubar |
|
|
|
case menubarCompactMode |
|
|
|
case menubarCompactMode |
|
|
|
case dstTransitionInfo |
|
|
|
case dstTransitionInfo |
|
|
|
|
|
|
|
case sync |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class DataStore: NSObject { |
|
|
|
class DataStore: NSObject { |
|
|
|
private static var sharedStore = DataStore(with: UserDefaults.standard) |
|
|
|
private static var sharedStore = DataStore(with: UserDefaults.standard) |
|
|
|
private var userDefaults: UserDefaults! |
|
|
|
private var userDefaults: UserDefaults! |
|
|
|
private var ubiquitousStore: NSUbiquitousKeyValueStore! |
|
|
|
private var ubiquitousStore: NSUbiquitousKeyValueStore? |
|
|
|
|
|
|
|
|
|
|
|
// Since these pref can accessed every second, let's cache this |
|
|
|
// Since these pref can accessed every second, let's cache this |
|
|
|
private var shouldDisplayDayInMenubar: Bool = false |
|
|
|
private var shouldDisplayDayInMenubar: Bool = false |
|
|
@ -41,9 +42,30 @@ class DataStore: NSObject { |
|
|
|
init(with defaults: UserDefaults) { |
|
|
|
init(with defaults: UserDefaults) { |
|
|
|
super.init() |
|
|
|
super.init() |
|
|
|
userDefaults = defaults |
|
|
|
userDefaults = defaults |
|
|
|
ubiquitousStore = NSUbiquitousKeyValueStore.default |
|
|
|
|
|
|
|
shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar) |
|
|
|
shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar) |
|
|
|
shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar) |
|
|
|
shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (shouldDisplay(.sync)) { |
|
|
|
|
|
|
|
ubiquitousStore = NSUbiquitousKeyValueStore.default |
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, |
|
|
|
|
|
|
|
selector: #selector(ubiquitousKeyValueStoreChanged), |
|
|
|
|
|
|
|
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, |
|
|
|
|
|
|
|
object: NSUbiquitousKeyValueStore.default) |
|
|
|
|
|
|
|
ubiquitousStore?.synchronize() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@objc func ubiquitousKeyValueStoreChanged(_ notification: Notification) { |
|
|
|
|
|
|
|
let userInfo = notification.userInfo ?? [:] |
|
|
|
|
|
|
|
let ubiquitousStore = notification.object as? NSUbiquitousKeyValueStore |
|
|
|
|
|
|
|
print("--- User Info is \(userInfo)") |
|
|
|
|
|
|
|
let currentTimezones = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data] |
|
|
|
|
|
|
|
let cloudTimezones = ubiquitousStore?.object(forKey: CLDefaultPreferenceKey) as? [Data] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cloudTimezones != currentTimezones { |
|
|
|
|
|
|
|
Logger.info("Syncing local timezones with data from the ☁️") |
|
|
|
|
|
|
|
userDefaults.set(cloudTimezones, forKey: CLDefaultPreferenceKey) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func timezones() -> [Data] { |
|
|
|
func timezones() -> [Data] { |
|
|
@ -57,7 +79,7 @@ class DataStore: NSObject { |
|
|
|
func setTimezones(_ timezones: [Data]?) { |
|
|
|
func setTimezones(_ timezones: [Data]?) { |
|
|
|
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) |
|
|
|
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) |
|
|
|
// iCloud sync |
|
|
|
// iCloud sync |
|
|
|
ubiquitousStore.set(timezones, forKey: CLDefaultPreferenceKey) |
|
|
|
ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func menubarTimezones() -> [Data]? { |
|
|
|
func menubarTimezones() -> [Data]? { |
|
|
@ -158,6 +180,8 @@ class DataStore: NSObject { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return value == 0 |
|
|
|
return value == 0 |
|
|
|
|
|
|
|
case .sync: |
|
|
|
|
|
|
|
return shouldDisplayHelper(CLEnableSyncKey) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|