From 298f65ce51c761cc640dccafa20e9c5cdd2d7b72 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Sat, 9 Apr 2022 16:39:10 -0400 Subject: [PATCH] Sync logic! --- Clocker/Overall App/DataStore.swift | 11 +++++++++-- Clocker/Overall App/Strings.swift | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Clocker/Overall App/DataStore.swift b/Clocker/Overall App/DataStore.swift index 70fc53d..3c24632 100644 --- a/Clocker/Overall App/DataStore.swift +++ b/Clocker/Overall App/DataStore.swift @@ -53,7 +53,9 @@ class DataStore: NSObject { selector: #selector(ubiquitousKeyValueStoreChanged), name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: NSUbiquitousKeyValueStore.default) - ubiquitousStore?.synchronize() + let synchronizationResult = ubiquitousStore?.synchronize() ?? false + let resultString = synchronizationResult ? "successfully" : "unsuccessfully" + Logger.info("Ubiquitous Store synchronized \(resultString)") } else { NotificationCenter.default.removeObserver(self, name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, @@ -67,10 +69,13 @@ class DataStore: NSObject { print("--- User Info is \(userInfo)") let currentTimezones = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data] let cloudTimezones = ubiquitousStore?.object(forKey: CLDefaultPreferenceKey) as? [Data] + let cloudLastUpdateDate = (ubiquitousStore?.object(forKey: CLUbiquitousStoreLastUpdateKey) as? Date) ?? Date() + let defaultsLastUpdateDate = (ubiquitousStore?.object(forKey: CLUserDefaultsLastUpdateKey) as? Date) ?? Date() - if cloudTimezones != currentTimezones { + if cloudTimezones != currentTimezones, cloudLastUpdateDate.isLaterThanOrEqual(to: defaultsLastUpdateDate) { Logger.info("Syncing local timezones with data from the ☁️") userDefaults.set(cloudTimezones, forKey: CLDefaultPreferenceKey) + userDefaults.set(Date(), forKey: CLUserDefaultsLastUpdateKey) NotificationCenter.default.post(name: DataStore.didSyncFromExternalSourceNotification, object: self) } @@ -86,8 +91,10 @@ class DataStore: NSObject { func setTimezones(_ timezones: [Data]?) { userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) + userDefaults.set(Date(), forKey: CLUserDefaultsLastUpdateKey) // iCloud sync ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey) + ubiquitousStore?.set(Date(), forKey: CLUbiquitousStoreLastUpdateKey) } func menubarTimezones() -> [Data]? { diff --git a/Clocker/Overall App/Strings.swift b/Clocker/Overall App/Strings.swift index 7c5ad3b..8a10454 100644 --- a/Clocker/Overall App/Strings.swift +++ b/Clocker/Overall App/Strings.swift @@ -36,4 +36,8 @@ let CLDefaultMenubarMode = "com.abhishek.shouldDefaultToCompactMode" let CLInstallHomeIndicatorObject = "installHomeIndicatorObject" let CLSwitchToCompactModeAlert = "com.abhishek.switchToCompactMode" let CLAppleInterfaceStyleKey = "AppleInterfaceStyle" + +// Sync Keys let CLEnableSyncKey = "com.abhishek.enableSync" +let CLUbiquitousStoreLastUpdateKey = "com.abhishek.ubiquitousLastUpdateKey" +let CLUserDefaultsLastUpdateKey = "com.abhishek.defaultsLastUpdateKey"