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 menubarCompactMode
case dstTransitionInfo
case sync
}
class DataStore: NSObject {
private static var sharedStore = DataStore(with: UserDefaults.standard)
private var userDefaults: UserDefaults!
private var ubiquitousStore: NSUbiquitousKeyValueStore!
private var ubiquitousStore: NSUbiquitousKeyValueStore?
// Since these pref can accessed every second, let's cache this
private var shouldDisplayDayInMenubar: Bool = false
@ -41,9 +42,30 @@ class DataStore: NSObject {
init(with defaults: UserDefaults) {
super.init()
userDefaults = defaults
ubiquitousStore = NSUbiquitousKeyValueStore.default
shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar)
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] {
@ -57,7 +79,7 @@ class DataStore: NSObject {
func setTimezones(_ timezones: [Data]?) {
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey)
// iCloud sync
ubiquitousStore.set(timezones, forKey: CLDefaultPreferenceKey)
ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey)
}
func menubarTimezones() -> [Data]? {
@ -158,6 +180,8 @@ class DataStore: NSObject {
}
return value == 0
case .sync:
return shouldDisplayHelper(CLEnableSyncKey)
}
}

17
Clocker/Panel/ParentPanelController.swift

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

Loading…
Cancel
Save