Browse Source

☁️

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
18b80cdcda
  1. 30
      Clocker/Overall App/DataStore.swift
  2. 17
      Clocker/Panel/ParentPanelController.swift
  3. 14
      Clocker/Preferences/General/PreferencesViewController.swift

30
Clocker/Overall App/DataStore.swift

@ -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)
} }
} }

17
Clocker/Panel/ParentPanelController.swift

@ -195,13 +195,16 @@ class ParentPanelController: NSWindowController {
selector: #selector(systemTimezoneDidChange), selector: #selector(systemTimezoneDidChange),
name: NSNotification.Name.NSSystemTimeZoneDidChange, name: NSNotification.Name.NSSystemTimeZoneDidChange,
object: nil) object: nil)
NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: self, if (DataStore.shared().shouldDisplay(.sync)) {
queue: OperationQueue.main) NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
{ [weak self] _ in object: self,
if let sSelf = self { queue: OperationQueue.main)
sSelf.mainTableView.reloadData() { [weak self] _ in
sSelf.setScrollViewConstraint() if let sSelf = self {
sSelf.mainTableView.reloadData()
sSelf.setScrollViewConstraint()
}
} }
} }

14
Clocker/Preferences/General/PreferencesViewController.swift

@ -93,12 +93,14 @@ class PreferencesViewController: ParentViewController {
name: NSNotification.Name.customLabelChanged, name: NSNotification.Name.customLabelChanged,
object: nil) object: nil)
NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification, if (DataStore.shared().shouldDisplay(.sync)) {
object: self, NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
queue: OperationQueue.main) object: self,
{ [weak self] _ in queue: OperationQueue.main)
if let sSelf = self { { [weak self] _ in
sSelf.refreshTimezoneTableView() if let sSelf = self {
sSelf.refreshTimezoneTableView()
}
} }
} }

Loading…
Cancel
Save