From 75bcb10d9a1b945eeb150153c02af9a72aa122eb Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sat, 2 Apr 2022 20:58:13 -0400 Subject: [PATCH] Sync setup.. --- Clocker/Overall App/DataStore.swift | 10 ++++++++-- Clocker/Panel/ParentPanelController.swift | 16 +++++++--------- .../Appearance/AppearanceViewController.swift | 2 +- .../General/PreferencesViewController.swift | 14 ++++++-------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index bce8b24..57264c6 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -46,8 +46,8 @@ class DataStore: NSObject { shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar) setupSyncNotification() } - - func setupSyncNotification() { + + func setupSyncNotification() { if shouldDisplay(.sync) { ubiquitousStore = NSUbiquitousKeyValueStore.default NotificationCenter.default.addObserver(self, @@ -72,6 +72,8 @@ class DataStore: NSObject { if cloudTimezones != currentTimezones { Logger.info("Syncing local timezones with data from the ☁️") userDefaults.set(cloudTimezones, forKey: CLDefaultPreferenceKey) + NotificationCenter.default.post(name: DataStore.didSyncFromExternalSourceNotification, + object: self) } } @@ -201,3 +203,7 @@ class DataStore: NSObject { return value.isEqual(to: NSNumber(value: 0)) } } + +extension DataStore { + public static let didSyncFromExternalSourceNotification: NSNotification.Name = .init("didSyncFromExternalSourceNotification") +} diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index 5fb0cbe..146dade 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -196,15 +196,13 @@ class ParentPanelController: NSWindowController { name: NSNotification.Name.NSSystemTimeZoneDidChange, object: nil) - 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() - } + NotificationCenter.default.addObserver(forName: DataStore.didSyncFromExternalSourceNotification, + object: self, + queue: OperationQueue.main) + { [weak self] _ in + if let sSelf = self { + sSelf.mainTableView.reloadData() + sSelf.setScrollViewConstraint() } } diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 04b66a3..6030146 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -359,7 +359,7 @@ class AppearanceViewController: ParentViewController { previewPanelTableView.reloadData() } - @IBAction func toggleSync(_ sender: NSSegmentedControl) { + @IBAction func toggleSync(_: NSSegmentedControl) { DataStore.shared().setupSyncNotification() } } diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index c29f067..4226215 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -93,14 +93,12 @@ class PreferencesViewController: ParentViewController { name: NSNotification.Name.customLabelChanged, object: nil) - 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() - } + NotificationCenter.default.addObserver(forName: DataStore.didSyncFromExternalSourceNotification, + object: self, + queue: OperationQueue.main) + { [weak self] _ in + if let sSelf = self { + sSelf.refreshTimezoneTableView() } }