From 6211bc78a5ddeee09f096e5f0fabf343b5043e85 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Fri, 1 Apr 2022 21:19:55 -0400 Subject: [PATCH 01/16] Toggle for Sync. --- Clocker/Clocker/Clocker.entitlements | 4 - Clocker/Clocker/ar.lproj/Localizable.strings | 3 + Clocker/Clocker/ca.lproj/Localizable.strings | 3 + Clocker/Clocker/de.lproj/Localizable.strings | 3 + Clocker/Clocker/en.lproj/Localizable.strings | 3 + Clocker/Clocker/es.lproj/Localizable.strings | 3 + Clocker/Clocker/fr.lproj/Localizable.strings | 3 + Clocker/Clocker/hi.lproj/Localizable.strings | 3 + Clocker/Clocker/hr.lproj/Localizable.strings | 3 + Clocker/Clocker/it.lproj/Localizable.strings | 3 + Clocker/Clocker/ja.lproj/Localizable.strings | 3 + Clocker/Clocker/ko.lproj/Localizable.strings | 3 + Clocker/Clocker/nl.lproj/Localizable.strings | 3 + Clocker/Clocker/pl.lproj/Localizable.strings | 3 + .../Clocker/pt-BR.lproj/Localizable.strings | 3 + Clocker/Clocker/ru.lproj/Localizable.strings | 3 + Clocker/Clocker/tr.lproj/Localizable.strings | 3 + .../Clocker/zh-Hans.lproj/Localizable.strings | 3 + .../Clocker/zh-Hant.lproj/Localizable.strings | 3 + Clocker/Overall App/Strings.swift | 1 + .../Appearance/AppearanceViewController.swift | 11 ++- Clocker/Preferences/Preferences.storyboard | 79 +++++++++++++------ 22 files changed, 119 insertions(+), 30 deletions(-) diff --git a/Clocker/Clocker/Clocker.entitlements b/Clocker/Clocker/Clocker.entitlements index 1754713..9f43c40 100644 --- a/Clocker/Clocker/Clocker.entitlements +++ b/Clocker/Clocker/Clocker.entitlements @@ -2,10 +2,6 @@ - com.apple.developer.icloud-container-identifiers - - com.apple.developer.ubiquity-kvstore-identifier - $(TeamIdentifierPrefix)$(CFBundleIdentifier) com.apple.security.app-sandbox com.apple.security.cs.disable-library-validation diff --git a/Clocker/Clocker/ar.lproj/Localizable.strings b/Clocker/Clocker/ar.lproj/Localizable.strings index 9652a9e..4a0d690 100644 --- a/Clocker/Clocker/ar.lproj/Localizable.strings +++ b/Clocker/Clocker/ar.lproj/Localizable.strings @@ -158,3 +158,6 @@ "New Zealand" = "New Zealand"; "Florida" = "Florida"; "San Francisco" = "San Francisco"; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/ca.lproj/Localizable.strings b/Clocker/Clocker/ca.lproj/Localizable.strings index d4d47b5..a7e6ad8 100644 --- a/Clocker/Clocker/ca.lproj/Localizable.strings +++ b/Clocker/Clocker/ca.lproj/Localizable.strings @@ -164,3 +164,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/de.lproj/Localizable.strings b/Clocker/Clocker/de.lproj/Localizable.strings index 30cb9aa..27ce101 100644 --- a/Clocker/Clocker/de.lproj/Localizable.strings +++ b/Clocker/Clocker/de.lproj/Localizable.strings @@ -164,3 +164,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/en.lproj/Localizable.strings b/Clocker/Clocker/en.lproj/Localizable.strings index 0840228..73ae1c9 100644 --- a/Clocker/Clocker/en.lproj/Localizable.strings +++ b/Clocker/Clocker/en.lproj/Localizable.strings @@ -168,3 +168,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/es.lproj/Localizable.strings b/Clocker/Clocker/es.lproj/Localizable.strings index 7870ae7..77f94d7 100644 --- a/Clocker/Clocker/es.lproj/Localizable.strings +++ b/Clocker/Clocker/es.lproj/Localizable.strings @@ -166,3 +166,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/fr.lproj/Localizable.strings b/Clocker/Clocker/fr.lproj/Localizable.strings index 5182cb7..5174df9 100644 --- a/Clocker/Clocker/fr.lproj/Localizable.strings +++ b/Clocker/Clocker/fr.lproj/Localizable.strings @@ -165,3 +165,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/hi.lproj/Localizable.strings b/Clocker/Clocker/hi.lproj/Localizable.strings index 13353a5..0044df9 100644 --- a/Clocker/Clocker/hi.lproj/Localizable.strings +++ b/Clocker/Clocker/hi.lproj/Localizable.strings @@ -160,3 +160,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/hr.lproj/Localizable.strings b/Clocker/Clocker/hr.lproj/Localizable.strings index e5d28ac..11e6925 100644 --- a/Clocker/Clocker/hr.lproj/Localizable.strings +++ b/Clocker/Clocker/hr.lproj/Localizable.strings @@ -166,3 +166,6 @@ "No upcoming events for today!" = "Za danas nema predstojećih događaja!"; "Great going." = "Super."; "Happy Weekend." = "Uživaj vikend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/it.lproj/Localizable.strings b/Clocker/Clocker/it.lproj/Localizable.strings index e1b4c00..3f0369f 100644 --- a/Clocker/Clocker/it.lproj/Localizable.strings +++ b/Clocker/Clocker/it.lproj/Localizable.strings @@ -165,3 +165,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/ja.lproj/Localizable.strings b/Clocker/Clocker/ja.lproj/Localizable.strings index 9548555..8733095 100644 --- a/Clocker/Clocker/ja.lproj/Localizable.strings +++ b/Clocker/Clocker/ja.lproj/Localizable.strings @@ -355,3 +355,6 @@ "start-at-login" = "ログイン時に開始"; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/ko.lproj/Localizable.strings b/Clocker/Clocker/ko.lproj/Localizable.strings index d424b7d..e2dbb7c 100644 --- a/Clocker/Clocker/ko.lproj/Localizable.strings +++ b/Clocker/Clocker/ko.lproj/Localizable.strings @@ -167,3 +167,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/nl.lproj/Localizable.strings b/Clocker/Clocker/nl.lproj/Localizable.strings index d4d47b5..a7e6ad8 100644 --- a/Clocker/Clocker/nl.lproj/Localizable.strings +++ b/Clocker/Clocker/nl.lproj/Localizable.strings @@ -164,3 +164,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/pl.lproj/Localizable.strings b/Clocker/Clocker/pl.lproj/Localizable.strings index add8334..f3e319b 100644 --- a/Clocker/Clocker/pl.lproj/Localizable.strings +++ b/Clocker/Clocker/pl.lproj/Localizable.strings @@ -158,3 +158,6 @@ "New Zealand" = "Nowa Zelandia"; "Florida" = "Floryda"; "San Francisco" = "San Francisco"; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/pt-BR.lproj/Localizable.strings b/Clocker/Clocker/pt-BR.lproj/Localizable.strings index 6bb78f1..62fff41 100644 --- a/Clocker/Clocker/pt-BR.lproj/Localizable.strings +++ b/Clocker/Clocker/pt-BR.lproj/Localizable.strings @@ -165,3 +165,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/ru.lproj/Localizable.strings b/Clocker/Clocker/ru.lproj/Localizable.strings index 864f2b6..15bf74d 100644 --- a/Clocker/Clocker/ru.lproj/Localizable.strings +++ b/Clocker/Clocker/ru.lproj/Localizable.strings @@ -157,3 +157,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/tr.lproj/Localizable.strings b/Clocker/Clocker/tr.lproj/Localizable.strings index 3a1e01b..a8c05ff 100644 --- a/Clocker/Clocker/tr.lproj/Localizable.strings +++ b/Clocker/Clocker/tr.lproj/Localizable.strings @@ -159,3 +159,6 @@ Takvimleri Görüntüle"; "New Zealand" = "New Zealand"; "Florida" = "Florida"; "San Francisco" = "San Francisco"; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/zh-Hans.lproj/Localizable.strings b/Clocker/Clocker/zh-Hans.lproj/Localizable.strings index 1c4358f..c1bc99c 100644 --- a/Clocker/Clocker/zh-Hans.lproj/Localizable.strings +++ b/Clocker/Clocker/zh-Hans.lproj/Localizable.strings @@ -158,3 +158,6 @@ "No upcoming events for today!" = "No upcoming events for today!"; "Great going." = "Great going."; "Happy Weekend." = "Happy Weekend."; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Clocker/zh-Hant.lproj/Localizable.strings b/Clocker/Clocker/zh-Hant.lproj/Localizable.strings index 24b5603..7fb5639 100644 --- a/Clocker/Clocker/zh-Hant.lproj/Localizable.strings +++ b/Clocker/Clocker/zh-Hant.lproj/Localizable.strings @@ -355,3 +355,6 @@ "start-at-login" = "在登入時啟動"; + +// iCloud +"Enable iCloud Sync" = "Enable iCloud Sync"; diff --git a/Clocker/Overall App/Strings.swift b/Clocker/Overall App/Strings.swift index e9fa746..b28c608 100644 --- a/Clocker/Overall App/Strings.swift +++ b/Clocker/Overall App/Strings.swift @@ -37,3 +37,4 @@ let CLInstallHomeIndicatorObject = "installHomeIndicatorObject" let CLSwitchToCompactModeAlert = "com.abhishek.switchToCompactMode" let CLDisplayDSTTransitionInfo = "com.abhishek.showDSTTransitionInfo" let CLAppleInterfaceStyleKey = "AppleInterfaceStyle" +let CLEnableSyncKey = "com.abhishek.enableSync" diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 79558af..bf0d79f 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -16,7 +16,8 @@ class AppearanceViewController: ParentViewController { @IBOutlet var includePlaceNameControl: NSSegmentedControl! @IBOutlet var appearanceTab: NSTabView! @IBOutlet var appDisplayControl: NSSegmentedControl! - + @IBOutlet weak var syncLabel: NSTextField! + private var themeDidChangeNotification: NSObjectProtocol? private var previewTimezones: [TimezoneData] = [] @@ -162,6 +163,7 @@ class AppearanceViewController: ParentViewController { showSliderLabel.stringValue = "Time Scroller".localized() showSunriseLabel.stringValue = "Show Sunrise/Sunset".localized() largerTextLabel.stringValue = "Larger Text".localized() + syncLabel.stringValue = "Enable iCloud Sync".localized() futureSliderRangeLabel.stringValue = "Future Slider Range".localized() includeDateLabel.stringValue = "Include Date".localized() includeDayLabel.stringValue = "Include Day".localized() @@ -172,7 +174,7 @@ class AppearanceViewController: ParentViewController { [timeFormatLabel, panelTheme, dayDisplayOptionsLabel, showSliderLabel, - showSunriseLabel, largerTextLabel, futureSliderRangeLabel, + showSunriseLabel, largerTextLabel, syncLabel, futureSliderRangeLabel, includeDayLabel, includeDateLabel, includePlaceLabel, appDisplayLabel, menubarModeLabel, previewLabel, miscelleaneousLabel, dstTransitionField].forEach { $0?.textColor = Themer.shared().mainTextColor() @@ -356,6 +358,11 @@ class AppearanceViewController: ParentViewController { @IBAction func toggleDSTTransitionOption(_: Any) { previewPanelTableView.reloadData() } + + @IBAction func toggleSync(_ sender: Any) { + print("Toggle Sync") + } + } extension AppearanceViewController: NSTableViewDataSource, NSTableViewDelegate { diff --git a/Clocker/Preferences/Preferences.storyboard b/Clocker/Preferences/Preferences.storyboard index f3fe745..99c81cd 100644 --- a/Clocker/Preferences/Preferences.storyboard +++ b/Clocker/Preferences/Preferences.storyboard @@ -768,20 +768,20 @@ - + - + - + - + @@ -793,6 +793,7 @@ + @@ -801,7 +802,7 @@ - + @@ -814,7 +815,7 @@ - + @@ -834,7 +835,7 @@ - + @@ -844,7 +845,7 @@ - + @@ -866,7 +867,7 @@ - + @@ -879,7 +880,7 @@ - + @@ -897,7 +898,7 @@ - + @@ -910,7 +911,7 @@ - + @@ -926,7 +927,7 @@ - + @@ -939,7 +940,7 @@ - + @@ -955,7 +956,7 @@ - + @@ -968,7 +969,7 @@ - + @@ -976,10 +977,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -1156,7 +1186,7 @@ - + @@ -1487,6 +1517,7 @@ + @@ -1513,7 +1544,7 @@ - + @@ -1570,13 +1601,13 @@ DQ - + - + - + @@ -1666,14 +1697,14 @@ DQ - + - + From 18b80cdcdaf37de0224cdd8439270427faaedc1b Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Fri, 1 Apr 2022 21:48:29 -0400 Subject: [PATCH 02/16] =?UTF-8?q?=E2=98=81=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Clocker/Overall App/DataStore.swift | 30 +++++++++++++++++-- Clocker/Panel/ParentPanelController.swift | 17 ++++++----- .../General/PreferencesViewController.swift | 14 +++++---- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 8be2c92..87ecf70 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/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) } } diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index d101ed9..f2598f9 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/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() + } } } diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 09a3578..99920a8 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/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() + } } } From 93c36cd25c8a75040ba49471413560bf830770d5 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Fri, 1 Apr 2022 21:51:38 -0400 Subject: [PATCH 03/16] Swiftformat. --- .../StandardMenubarHandlerTests.swift | 17 +++++++++-------- Clocker/Overall App/DataStore.swift | 8 ++++---- Clocker/Panel/ParentPanelController.swift | 4 ++-- .../Appearance/AppearanceViewController.swift | 9 ++++----- .../General/PreferencesViewController.swift | 2 +- .../Menu Bar/MenubarTitleProvider.swift | 6 +++--- .../Menu Bar/StatusItemHandler.swift | 2 +- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift b/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift index 8d8c53e..1eae0a7 100644 --- a/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift +++ b/Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift @@ -13,7 +13,7 @@ class StandardMenubarHandlerTests: XCTestCase { "nextUpdate": "", "latitude": "19.0759837", "longitude": "72.8776559"] - + private func makeMockStore(with menubarMode: Int = 1) -> DataStore { // Wipe all timezones from UserDefaults guard let defaults = UserDefaults(suiteName: "com.abhishek.Clocker.StandardMenubarHandlerTests") else { @@ -25,10 +25,11 @@ class StandardMenubarHandlerTests: XCTestCase { XCTAssertNotEqual(defaults, UserDefaults.standard) return DataStore(with: defaults) } - + private func saveObject(object: TimezoneData, in store: DataStore, - at index: Int = -1) { + at index: Int = -1) + { var defaults = store.timezones() let encodedObject = NSKeyedArchiver.archivedData(withRootObject: object as Any) index == -1 ? defaults.append(encodedObject) : defaults.insert(encodedObject, at: index) @@ -81,14 +82,14 @@ class StandardMenubarHandlerTests: XCTestCase { // Test menubar string is absent XCTAssertTrue(menubarString.count == 0) } - + func testWithEmptyMenubarTimezones() { let store = makeMockStore() store.setTimezones(nil) let menubarHandler = MenubarTitleProvider(with: store) XCTAssertNil(menubarHandler.titleForMenubar()) } - + func testWithStandardMenubarMode() { // Set mode to standard mode let store = makeMockStore(with: 0) @@ -96,11 +97,11 @@ class StandardMenubarHandlerTests: XCTestCase { let dataObject = TimezoneData(with: mumbai) dataObject.isFavourite = 1 saveObject(object: dataObject, in: store) - + let menubarHandler = MenubarTitleProvider(with: store) XCTAssertNil(menubarHandler.titleForMenubar()) } - + func testProviderPassingAllConditions() { // Set mode to standard mode let store = makeMockStore() @@ -108,7 +109,7 @@ class StandardMenubarHandlerTests: XCTestCase { let dataObject = TimezoneData(with: mumbai) dataObject.isFavourite = 1 saveObject(object: dataObject, in: store) - + let menubarHandler = MenubarTitleProvider(with: store) XCTAssertNotNil(menubarHandler.titleForMenubar()) } diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 87ecf70..7cf5f40 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -44,8 +44,8 @@ class DataStore: NSObject { userDefaults = defaults shouldDisplayDayInMenubar = shouldDisplay(.dayInMenubar) shouldDisplayDateInMenubar = shouldDisplay(.dateInMenubar) - - if (shouldDisplay(.sync)) { + + if shouldDisplay(.sync) { ubiquitousStore = NSUbiquitousKeyValueStore.default NotificationCenter.default.addObserver(self, selector: #selector(ubiquitousKeyValueStoreChanged), @@ -54,14 +54,14 @@ class DataStore: NSObject { 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) diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index f2598f9..5fb0cbe 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -195,8 +195,8 @@ class ParentPanelController: NSWindowController { selector: #selector(systemTimezoneDidChange), name: NSNotification.Name.NSSystemTimeZoneDidChange, object: nil) - - if (DataStore.shared().shouldDisplay(.sync)) { + + if DataStore.shared().shouldDisplay(.sync) { NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: self, queue: OperationQueue.main) diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index bf0d79f..5730ff6 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -16,8 +16,8 @@ class AppearanceViewController: ParentViewController { @IBOutlet var includePlaceNameControl: NSSegmentedControl! @IBOutlet var appearanceTab: NSTabView! @IBOutlet var appDisplayControl: NSSegmentedControl! - @IBOutlet weak var syncLabel: NSTextField! - + @IBOutlet var syncLabel: NSTextField! + private var themeDidChangeNotification: NSObjectProtocol? private var previewTimezones: [TimezoneData] = [] @@ -358,11 +358,10 @@ class AppearanceViewController: ParentViewController { @IBAction func toggleDSTTransitionOption(_: Any) { previewPanelTableView.reloadData() } - - @IBAction func toggleSync(_ sender: Any) { + + @IBAction func toggleSync(_: Any) { print("Toggle Sync") } - } extension AppearanceViewController: NSTableViewDataSource, NSTableViewDelegate { diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 99920a8..c29f067 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -93,7 +93,7 @@ class PreferencesViewController: ParentViewController { name: NSNotification.Name.customLabelChanged, object: nil) - if (DataStore.shared().shouldDisplay(.sync)) { + if DataStore.shared().shouldDisplay(.sync) { NotificationCenter.default.addObserver(forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: self, queue: OperationQueue.main) diff --git a/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift b/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift index 27070de..d88d7a5 100644 --- a/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift +++ b/Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift @@ -7,12 +7,12 @@ import EventKit class MenubarTitleProvider: NSObject { private let store: DataStore - + init(with dataStore: DataStore) { - self.store = dataStore + store = dataStore super.init() } - + func titleForMenubar() -> String? { if let nextEvent = checkForUpcomingEvents() { return nextEvent diff --git a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift index 3ebed0f..693c785 100644 --- a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift @@ -256,7 +256,7 @@ class StatusItemHandler: NSObject { // This will internally call `statusItemViewSetNeedsDisplay` on all subviews ensuring all text in the menubar is up-to-date. statusContainerView?.updateTime() } - + private func removeUpcomingStatusItemView() { NSAnimationContext.runAnimationGroup({ context in context.duration = 0.2 From a697a2920601e1b0eea626904f3412d70a2e1c02 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Fri, 1 Apr 2022 22:50:30 -0400 Subject: [PATCH 04/16] Update StatusItemHandler.swift --- Clocker/Preferences/Menu Bar/StatusItemHandler.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift index 693c785..743a0ed 100644 --- a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift @@ -240,8 +240,7 @@ class StatusItemHandler: NSObject { } func updateCompactMenubar() { - if let upcomingEvent = menubarTitleHandler.checkForUpcomingEvents() { - print("Need to construct upcoming event view \(upcomingEvent)") + if menubarTitleHandler.checkForUpcomingEvents() != nil { // Iterate and see if we're showing the calendar item view let upcomingEventView = retrieveUpcomingEventStatusView() // If not, reconstruct Status Container View with another view 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 05/16] 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() } } 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 06/16] 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() } } From cba1a407fcdddceea154c3d5c2b5552815b2b256 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sat, 2 Apr 2022 21:19:39 -0400 Subject: [PATCH 07/16] DST changes.. --- .../Appearance/AppearanceViewController.swift | 15 +-- Clocker/Preferences/Preferences.storyboard | 95 +++++++------------ 2 files changed, 40 insertions(+), 70 deletions(-) diff --git a/Clocker/Preferences/Appearance/AppearanceViewController.swift b/Clocker/Preferences/Appearance/AppearanceViewController.swift index 6030146..9e81982 100644 --- a/Clocker/Preferences/Appearance/AppearanceViewController.swift +++ b/Clocker/Preferences/Appearance/AppearanceViewController.swift @@ -17,6 +17,7 @@ class AppearanceViewController: ParentViewController { @IBOutlet var appearanceTab: NSTabView! @IBOutlet var appDisplayControl: NSSegmentedControl! @IBOutlet var syncLabel: NSTextField! + @IBOutlet var syncSegementedControl: NSSegmentedControl! private var themeDidChangeNotification: NSObjectProtocol? @@ -135,6 +136,10 @@ class AppearanceViewController: ParentViewController { // True is Menubar Only and False is Menubar + Dock let appDisplayOptions = DataStore.shared().shouldDisplay(.appDisplayOptions) appDisplayControl.setSelected(true, forSegment: appDisplayOptions ? 0 : 1) + + // Set the Sync value from NSUbiqutousKeyValueStore + let syncEnabled = NSUbiquitousKeyValueStore.default.bool(forKey: CLEnableSyncKey) + syncSegementedControl.setSelected(true, forSegment: syncEnabled ? 0 : 1) } @IBOutlet var timeFormatLabel: NSTextField! @@ -151,7 +156,6 @@ class AppearanceViewController: ParentViewController { @IBOutlet var menubarModeLabel: NSTextField! @IBOutlet var previewLabel: NSTextField! @IBOutlet var miscelleaneousLabel: NSTextField! - @IBOutlet var dstTransitionField: NSTextField! // Panel Preview @IBOutlet var previewPanelTableView: NSTableView! @@ -176,7 +180,7 @@ class AppearanceViewController: ParentViewController { dayDisplayOptionsLabel, showSliderLabel, showSunriseLabel, largerTextLabel, syncLabel, futureSliderRangeLabel, includeDayLabel, includeDateLabel, includePlaceLabel, appDisplayLabel, menubarModeLabel, - previewLabel, miscelleaneousLabel, dstTransitionField].forEach { + previewLabel, miscelleaneousLabel].forEach { $0?.textColor = Themer.shared().mainTextColor() } @@ -355,11 +359,8 @@ class AppearanceViewController: ParentViewController { previewPanelTableView.reloadData() } - @IBAction func toggleDSTTransitionOption(_: Any) { - previewPanelTableView.reloadData() - } - - @IBAction func toggleSync(_: NSSegmentedControl) { + @IBAction func toggleSync(_ sender: NSSegmentedControl) { + NSUbiquitousKeyValueStore.default.set(sender.selectedSegment == 0, forKey: CLEnableSyncKey) DataStore.shared().setupSyncNotification() } } diff --git a/Clocker/Preferences/Preferences.storyboard b/Clocker/Preferences/Preferences.storyboard index 99c81cd..8bffd78 100644 --- a/Clocker/Preferences/Preferences.storyboard +++ b/Clocker/Preferences/Preferences.storyboard @@ -768,20 +768,20 @@ - + - + - + - + @@ -791,7 +791,6 @@ - @@ -802,7 +801,7 @@ - + @@ -815,7 +814,7 @@ - + @@ -835,7 +834,7 @@ - + @@ -845,7 +844,7 @@ - + @@ -867,7 +866,7 @@ - + @@ -880,7 +879,7 @@ - + @@ -898,7 +897,7 @@ - + @@ -911,7 +910,7 @@ - + @@ -925,38 +924,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -969,7 +939,7 @@ - + @@ -979,7 +949,7 @@ - + @@ -992,7 +962,7 @@ - + @@ -1002,19 +972,18 @@ - - - + + - + @@ -1186,7 +1155,7 @@ - + @@ -1498,7 +1467,6 @@ - @@ -1518,6 +1486,7 @@ + @@ -1544,7 +1513,7 @@ - + @@ -1601,13 +1570,13 @@ DQ - + - - + + - + @@ -1697,14 +1666,14 @@ DQ - + - + @@ -1752,7 +1721,7 @@ DQ