Browse Source

☁️

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
18b80cdcda
  1. 30
      Clocker/Overall App/DataStore.swift
  2. 3
      Clocker/Panel/ParentPanelController.swift
  3. 2
      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)
} }
} }

3
Clocker/Panel/ParentPanelController.swift

@ -195,6 +195,8 @@ class ParentPanelController: NSWindowController {
selector: #selector(systemTimezoneDidChange), selector: #selector(systemTimezoneDidChange),
name: NSNotification.Name.NSSystemTimeZoneDidChange, name: NSNotification.Name.NSSystemTimeZoneDidChange,
object: nil) object: nil)
if (DataStore.shared().shouldDisplay(.sync)) {
NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification, NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: self, object: self,
queue: OperationQueue.main) queue: OperationQueue.main)
@ -204,6 +206,7 @@ class ParentPanelController: NSWindowController {
sSelf.setScrollViewConstraint() sSelf.setScrollViewConstraint()
} }
} }
}
// Setup upcoming events view // Setup upcoming events view
upcomingEventContainerView.setAccessibility("UpcomingEventView") upcomingEventContainerView.setAccessibility("UpcomingEventView")

2
Clocker/Preferences/General/PreferencesViewController.swift

@ -93,6 +93,7 @@ class PreferencesViewController: ParentViewController {
name: NSNotification.Name.customLabelChanged, name: NSNotification.Name.customLabelChanged,
object: nil) object: nil)
if (DataStore.shared().shouldDisplay(.sync)) {
NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification, NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: self, object: self,
queue: OperationQueue.main) queue: OperationQueue.main)
@ -101,6 +102,7 @@ class PreferencesViewController: ParentViewController {
sSelf.refreshTimezoneTableView() sSelf.refreshTimezoneTableView()
} }
} }
}
refreshTimezoneTableView() refreshTimezoneTableView()

Loading…
Cancel
Save