-
-
Notifications
You must be signed in to change notification settings - Fork 364
Fixes PropertyWrapper #267
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: master
Are you sure you want to change the base?
Changes from all commits
7eb2141
c406e6a
d05a851
bc7c59f
d498f39
0f37d9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ xcuserdata/ | |
| /www | ||
|
|
||
| /Carthage | ||
| /.swiftpm | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,30 +36,32 @@ public struct SwiftyUserDefaultOptions: OptionSet { | |
| } | ||
|
|
||
| @propertyWrapper | ||
| public final class SwiftyUserDefault<T: DefaultsSerializable> where T.T == T { | ||
| public final class SwiftyUserDefault<T: DefaultsSerializable, KeyStore: DefaultsKeyStore> where T.T == T { | ||
|
|
||
| public let key: DefaultsKey<T> | ||
| public let options: SwiftyUserDefaultOptions | ||
| private var adapter: DefaultsAdapter<KeyStore> | ||
|
|
||
| public var wrappedValue: T { | ||
| get { | ||
| if options.contains(.cached) { | ||
| return _value ?? Defaults[key: key] | ||
| return _value ?? adapter[key: key] | ||
| } else { | ||
| return Defaults[key: key] | ||
| return adapter[key: key] | ||
| } | ||
| } | ||
| set { | ||
| _value = newValue | ||
| Defaults[key: key] = newValue | ||
| adapter[key: key] = newValue | ||
| } | ||
| } | ||
|
|
||
| private var _value: T.T? | ||
| private var observation: DefaultsDisposable? | ||
|
|
||
| public init<KeyStore>(keyPath: KeyPath<KeyStore, DefaultsKey<T>>, adapter: DefaultsAdapter<KeyStore>, options: SwiftyUserDefaultOptions = []) { | ||
| public init(keyPath: KeyPath<KeyStore, DefaultsKey<T>>, adapter: DefaultsAdapter<KeyStore>, options: SwiftyUserDefaultOptions = []) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so ideally I'd love to have the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I was thinking we would be able to utilize a global framework property as a default initializer but that doesn't appear to be the case. I might suggest that we set default initializer with the same adapter init used for
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @spydercapriani yeah that would work - thanks! |
||
| self.key = adapter.keyStore[keyPath: keyPath] | ||
| self.adapter = adapter | ||
| self.options = options | ||
|
|
||
| if options.contains(.observed) { | ||
|
|
@@ -69,17 +71,6 @@ public final class SwiftyUserDefault<T: DefaultsSerializable> where T.T == T { | |
| } | ||
| } | ||
|
|
||
| public init(keyPath: KeyPath<DefaultsKeys, DefaultsKey<T>>, options: SwiftyUserDefaultOptions = []) { | ||
| self.key = Defaults.keyStore[keyPath: keyPath] | ||
| self.options = options | ||
|
|
||
| if options.contains(.observed) { | ||
| observation = Defaults.observe(key) { [weak self] update in | ||
| self?._value = update.newValue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| observation?.dispose() | ||
| } | ||
|
|
||
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.
can we remove the default behavior change from this PR and create another one for it? the reason is that this is a change that breaks compatibility with the current implementation, and so we would need a major version for that vs we don't have for the property wrapper fixes
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.
Sorry, this was suppose to be an internal change specific to our use case and not something for this PR. Didn't realize this PR was opened against our Master branch, will fix.