Skip to content
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

Add initialize from string key method to SwiftyUserDefault PropertyWrapper #284

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/DefaultsObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class DefaultsObserver<T: DefaultsSerializable>: NSObject, Defaults
private static func deserialize<T: DefaultsSerializable>(_ value: Any?, for key: DefaultsKey<T>) -> T.T? where T.T == T {
guard let value = value else { return nil }

let deserialized = T._defaults.deserialize(value)
let deserialized = T._defaults.deserialize(value)

let ret: T.T?
if key.isOptional, let _deserialized = deserialized, let __deserialized = _deserialized as? OptionalTypeCheck, !__deserialized.isNil {
Expand Down
49 changes: 48 additions & 1 deletion Sources/PropertyWrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ public struct SwiftyUserDefaultOptions: OptionSet {
}
}


@propertyWrapper
public struct SwiftyDefaults<T: DefaultsSerializable> where T.T == T {
public let key: DefaultsKey<T>
private var _value: T.T
public var wrappedValue: T.T {
get { _value }
set {
_value = newValue
Defaults[key: key] = newValue
}
}
public init(keyPath: KeyPath<DefaultsKeys, DefaultsKey<T>>) {
self.key = Defaults.keyStore[keyPath: keyPath]
self._value = Defaults[key: self.key]
}
public init(key: String, defaultValue value: T) {
self.key = DefaultsKey(key, defaultValue: value)
self._value = Defaults[key: self.key]
}
}
extension SwiftyDefaults where T: OptionalType, T.Wrapped: DefaultsSerializable {
public init(key: String) {
self.init(key: key, defaultValue: T.__swifty_empty)
}
}

@propertyWrapper
public final class SwiftyUserDefault<T: DefaultsSerializable> where T.T == T {

Expand All @@ -44,7 +71,10 @@ public final class SwiftyUserDefault<T: DefaultsSerializable> where T.T == T {
public var wrappedValue: T {
get {
if options.contains(.cached) {
return _value ?? Defaults[key: key]
if let v = _value { return v }
let v = Defaults[key: key]
_value = v
return v
} else {
return Defaults[key: key]
}
Expand Down Expand Up @@ -80,8 +110,25 @@ public final class SwiftyUserDefault<T: DefaultsSerializable> where T.T == T {
}
}

public init(key: String, defaultValue value: T, options: SwiftyUserDefaultOptions = []) {
self.key = DefaultsKey(key, defaultValue: value)
self.options = options

if options.contains(.observed) {
observation = Defaults.observe(self.key) { [weak self] update in
self?._value = update.newValue
}
}
}
deinit {
observation?.dispose()
}
}
extension SwiftyUserDefault where T: OptionalType, T.Wrapped: DefaultsSerializable {
public convenience init(key: String, options: SwiftyUserDefaultOptions = []) {
self.init(key: key, defaultValue: T.__swifty_empty, options: options)
}
}


#endif