Browse Source

Cache timezones + menubar timezones.

master
Abhishek Banthia 9 months ago
parent
commit
4fc109e6cf
  1. 26
      Clocker/Overall App/DataStore.swift

26
Clocker/Overall App/DataStore.swift

@ -24,6 +24,8 @@ class DataStore: NSObject {
private static var sharedStore = DataStore(with: UserDefaults.standard) private static var sharedStore = DataStore(with: UserDefaults.standard)
private var userDefaults: UserDefaults! private var userDefaults: UserDefaults!
private var ubiquitousStore: NSUbiquitousKeyValueStore? private var ubiquitousStore: NSUbiquitousKeyValueStore?
private var cachedTimezones: [Data]
private var cachedMenubarTimezones: [Data]
private static let timeFormatsWithSuffix: Set<NSNumber> = Set([NSNumber(integerLiteral: 0), private static let timeFormatsWithSuffix: Set<NSNumber> = Set([NSNumber(integerLiteral: 0),
NSNumber(integerLiteral: 3), NSNumber(integerLiteral: 3),
NSNumber(integerLiteral: 4), NSNumber(integerLiteral: 4),
@ -35,8 +37,14 @@ class DataStore: NSObject {
} }
init(with defaults: UserDefaults) { init(with defaults: UserDefaults) {
super.init() cachedTimezones = (defaults.object(forKey: CLDefaultPreferenceKey) as? [Data]) ?? []
cachedMenubarTimezones = cachedTimezones.filter {
let customTimezone = TimezoneData.customObject(from: $0)
return customTimezone?.isFavourite == 1
}
userDefaults = defaults userDefaults = defaults
super.init()
setupSyncNotification() setupSyncNotification()
} }
@ -85,26 +93,24 @@ class DataStore: NSObject {
} }
func timezones() -> [Data] { func timezones() -> [Data] {
guard let preferences = userDefaults.object(forKey: CLDefaultPreferenceKey) as? [Data] else { return cachedTimezones
return []
}
return preferences
} }
func setTimezones(_ timezones: [Data]?) { func setTimezones(_ timezones: [Data]?) {
userDefaults.set(timezones, forKey: CLDefaultPreferenceKey) userDefaults.set(timezones, forKey: CLDefaultPreferenceKey)
userDefaults.set(Date(), forKey: CLUserDefaultsLastUpdateKey) userDefaults.set(Date(), forKey: CLUserDefaultsLastUpdateKey)
cachedTimezones = timezones ?? []
cachedMenubarTimezones = cachedTimezones.filter {
let customTimezone = TimezoneData.customObject(from: $0)
return customTimezone?.isFavourite == 1
}
// iCloud sync // iCloud sync
ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey) ubiquitousStore?.set(timezones, forKey: CLDefaultPreferenceKey)
ubiquitousStore?.set(Date(), forKey: CLUbiquitousStoreLastUpdateKey) ubiquitousStore?.set(Date(), forKey: CLUbiquitousStoreLastUpdateKey)
} }
func menubarTimezones() -> [Data]? { func menubarTimezones() -> [Data]? {
return timezones().filter { return cachedMenubarTimezones
let customTimezone = TimezoneData.customObject(from: $0)
return customTimezone?.isFavourite == 1
}
} }
func selectedCalendars() -> [String]? { func selectedCalendars() -> [String]? {

Loading…
Cancel
Save