-
-
Notifications
You must be signed in to change notification settings - Fork 210
Custom UserDefaults suite #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Being able to substitute UserDefaults suites is valuable in other respects. In an app where I use the package, for unit tests and integration tests we use a custom suite for prefs, and it would be useful to be able to use the same suite with KeyboardShortcuts. |
….Name +++ Fix swiftlint warnings
…otentially existing observer +++ Update observer if UserDefaults being changed
I am just realizing that event streams also require observations. Will make another commit for that. |
…nor readability improvements
func update(suite new: UserDefaults) { | ||
invalidate() | ||
suite = new | ||
start() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm missing something, I think it would be better to just recreate observations than this update method.
Something like:
public static var userDefaults = UserDefaults.standard {
didSet {
for observer in userDefaultsObservers {
observer.invalidate()
}
userDefaultsObservers.removeAll()
for name in allNames {
startObservingShortcutIfNeeded(for: name)
}
}
}
return | ||
} | ||
|
||
let encodedString = change[.newKey] as? String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not assume String
. It could be a Bool
too:
Self.userDefaults.set(false, forKey: userDefaultsKey(for: name)) |
|
||
// check userDefaultsObservers to see if we are already observing this key | ||
if let observer = userDefaultsObservers.first(where: { $0.key == key }) { | ||
observer.start() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could use a better code comment because just looking at it, it's not immediate obvious why we may want to call start() on an already existing observation.
@@ -179,6 +204,12 @@ public enum KeyboardShortcuts { | |||
|
|||
legacyKeyDownHandlers = [:] | |||
legacyKeyUpHandlers = [:] | |||
|
|||
// invalidate and remove all elements of userDefaultsObservers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should comment that it's "defensive" since it's already done when calling removeAll()
(deinit).
UserDefaults.standard.dictionaryRepresentation() | ||
Self.userDefaults.dictionaryRepresentation() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self.
is not needed.
Make sure you manually test it in the example app and also a real app to ensure it doesn't introduce any new bugs. I'm a little bit nervous about large changes like this as we have no real tests. |
This PR adds the capability to define a custom UserDefaults to support App Groups and access group containers.
I am using KeyboardShortcuts in an environment with App Groups. The shared UserDefaults shall also include the defined shortcuts.