From dc4456acfa2c2370498f87ecc5f29a9c2d818a5b Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sat, 2 Apr 2022 11:38:07 -0400 Subject: [PATCH] Remove observer if sync is turned off. --- Clocker/Overall App/DataStore.swift | 9 ++++++++- .../Appearance/AppearanceViewController.swift | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 7cf5f40..bce8b24 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -44,7 +44,10 @@ class DataStore: NSObject { userDefaults = defaults shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar) shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar) - + setupSyncNotification() + } + + func setupSyncNotification() { if shouldDisplay(.sync) { ubiquitousStore = NSUbiquitousKeyValueStore.default NotificationCenter.default.addObserver(self, @@ -52,6 +55,10 @@ class DataStore: NSObject { name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: NSUbiquitousKeyValueStore.default) ubiquitousStore?.synchronize() + } else { + NotificationCenter.default.removeObserver(self, + name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, + object: nil) } } diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 5730ff6..04b66a3 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -359,8 +359,8 @@ class AppearanceViewController: ParentViewController { previewPanelTableView.reloadData() } - @IBAction func toggleSync(_: Any) { - print("Toggle Sync") + @IBAction func toggleSync(_ sender: NSSegmentedControl) { + DataStore.shared().setupSyncNotification() } }