Minor cleanup (#74)
This commit is contained in:
parent
63d93f97ad
commit
6ff23ff1f8
|
@ -53,7 +53,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct RawRepresentableBridge<Value: RawRepresentable>: Defaults.Bridge {
|
public struct RawRepresentableBridge<Value: RawRepresentable>: Bridge {
|
||||||
public typealias Value = Value
|
public typealias Value = Value
|
||||||
public typealias Serializable = Value.RawValue
|
public typealias Serializable = Value.RawValue
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct NSSecureCodingBridge<Value: NSSecureCoding>: Defaults.Bridge {
|
public struct NSSecureCodingBridge<Value: NSSecureCoding>: Bridge {
|
||||||
public typealias Value = Value
|
public typealias Value = Value
|
||||||
public typealias Serializable = Data
|
public typealias Serializable = Data
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct OptionalBridge<Wrapped: Defaults.Serializable>: Defaults.Bridge {
|
public struct OptionalBridge<Wrapped: Serializable>: Bridge {
|
||||||
public typealias Value = Wrapped.Value
|
public typealias Value = Wrapped.Value
|
||||||
public typealias Serializable = Wrapped.Serializable
|
public typealias Serializable = Wrapped.Serializable
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct ArrayBridge<Element: Defaults.Serializable>: Defaults.Bridge {
|
public struct ArrayBridge<Element: Serializable>: Bridge {
|
||||||
public typealias Value = [Element]
|
public typealias Value = [Element]
|
||||||
public typealias Serializable = [Element.Serializable]
|
public typealias Serializable = [Element.Serializable]
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct DictionaryBridge<Key: LosslessStringConvertible & Hashable, Element: Defaults.Serializable>: Defaults.Bridge {
|
public struct DictionaryBridge<Key: LosslessStringConvertible & Hashable, Element: Serializable>: Bridge {
|
||||||
public typealias Value = [Key: Element.Value]
|
public typealias Value = [Key: Element.Value]
|
||||||
public typealias Serializable = [String: Element.Serializable]
|
public typealias Serializable = [String: Element.Serializable]
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ extension Defaults {
|
||||||
We need both `SetBridge` and `SetAlgebraBridge` because `Set` conforms to `Sequence` but `SetAlgebra` does not. `Set` conforms to `Sequence`, so we can convert it into an array with `Array.init<S>(S)` and store it in the `UserDefaults`. But `SetAlgebra` does not, so it is hard to convert it into an array. Thats why we need the `Defaults.SetAlgebraSerializable` protocol to convert it into an array.
|
We need both `SetBridge` and `SetAlgebraBridge` because `Set` conforms to `Sequence` but `SetAlgebra` does not. `Set` conforms to `Sequence`, so we can convert it into an array with `Array.init<S>(S)` and store it in the `UserDefaults`. But `SetAlgebra` does not, so it is hard to convert it into an array. Thats why we need the `Defaults.SetAlgebraSerializable` protocol to convert it into an array.
|
||||||
*/
|
*/
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct SetBridge<Element: Defaults.Serializable & Hashable>: Defaults.Bridge {
|
public struct SetBridge<Element: Serializable & Hashable>: Bridge {
|
||||||
public typealias Value = Set<Element>
|
public typealias Value = Set<Element>
|
||||||
public typealias Serializable = Any
|
public typealias Serializable = Any
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct SetAlgebraBridge<Value: Defaults.SetAlgebraSerializable>: Defaults.Bridge where Value.Element: Defaults.Serializable {
|
public struct SetAlgebraBridge<Value: SetAlgebraSerializable>: Bridge where Value.Element: Serializable {
|
||||||
public typealias Value = Value
|
public typealias Value = Value
|
||||||
public typealias Element = Value.Element
|
public typealias Element = Value.Element
|
||||||
public typealias Serializable = Any
|
public typealias Serializable = Any
|
||||||
|
@ -260,7 +260,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct CollectionBridge<Value: Defaults.CollectionSerializable>: Defaults.Bridge where Value.Element: Defaults.Serializable {
|
public struct CollectionBridge<Value: CollectionSerializable>: Bridge where Value.Element: Serializable {
|
||||||
public typealias Value = Value
|
public typealias Value = Value
|
||||||
public typealias Element = Value.Element
|
public typealias Element = Value.Element
|
||||||
public typealias Serializable = Any
|
public typealias Serializable = Any
|
||||||
|
|
|
@ -99,4 +99,4 @@ public protocol DefaultsSetAlgebraSerializable: SetAlgebra, Defaults.Serializabl
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience protocol for `Codable`.
|
/// Convenience protocol for `Codable`.
|
||||||
public protocol DefaultsCodableBridge: DefaultsBridge where Serializable == String, Value: Codable {}
|
public protocol DefaultsCodableBridge: Defaults.Bridge where Serializable == String, Value: Codable {}
|
||||||
|
|
|
@ -16,15 +16,15 @@ extension Defaults {
|
||||||
Defaults.migrate(.array, to: .v5)
|
Defaults.migrate(.array, to: .v5)
|
||||||
```
|
```
|
||||||
*/
|
*/
|
||||||
public static func migrate<Value: Defaults.Serializable & Codable>(_ keys: Key<Value>..., to version: Version) {
|
public static func migrate<Value: Serializable & Codable>(_ keys: Key<Value>..., to version: Version) {
|
||||||
migrate(keys, to: version)
|
migrate(keys, to: version)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func migrate<Value: Defaults.NativeType>(_ keys: Key<Value>..., to version: Version) {
|
public static func migrate<Value: NativeType>(_ keys: Key<Value>..., to version: Version) {
|
||||||
migrate(keys, to: version)
|
migrate(keys, to: version)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func migrate<Value: Defaults.Serializable & Codable>(_ keys: [Key<Value>], to version: Version) {
|
public static func migrate<Value: Serializable & Codable>(_ keys: [Key<Value>], to version: Version) {
|
||||||
switch version {
|
switch version {
|
||||||
case .v5:
|
case .v5:
|
||||||
for key in keys {
|
for key in keys {
|
||||||
|
@ -34,7 +34,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func migrate<Value: Defaults.NativeType>(_ keys: [Key<Value>], to version: Version) {
|
public static func migrate<Value: NativeType>(_ keys: [Key<Value>], to version: Version) {
|
||||||
switch version {
|
switch version {
|
||||||
case .v5:
|
case .v5:
|
||||||
for key in keys {
|
for key in keys {
|
||||||
|
|
|
@ -180,7 +180,7 @@ extension Defaults.SetAlgebraSerializable where Self: Defaults.NativeType, Eleme
|
||||||
public typealias CodableForm = [Element.CodableForm]
|
public typealias CodableForm = [Element.CodableForm]
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults.CodableType where Self: RawRepresentable, NativeForm: RawRepresentable, Self.RawValue == NativeForm.RawValue {
|
extension Defaults.CodableType where Self: RawRepresentable, NativeForm: RawRepresentable, RawValue == NativeForm.RawValue {
|
||||||
public func toNative() -> NativeForm {
|
public func toNative() -> NativeForm {
|
||||||
NativeForm(rawValue: self.rawValue)!
|
NativeForm(rawValue: self.rawValue)!
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import Combine
|
||||||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
|
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
final class Observable<Value: Serializable>: ObservableObject {
|
final class Observable<Value: Serializable>: ObservableObject {
|
||||||
private var observation: DefaultsObservation?
|
private var cancellable: AnyCancellable?
|
||||||
private let key: Defaults.Key<Value>
|
private let key: Defaults.Key<Value>
|
||||||
|
|
||||||
let objectWillChange = ObservableObjectPublisher()
|
let objectWillChange = ObservableObjectPublisher()
|
||||||
|
@ -21,15 +21,16 @@ extension Defaults {
|
||||||
init(_ key: Key<Value>) {
|
init(_ key: Key<Value>) {
|
||||||
self.key = key
|
self.key = key
|
||||||
|
|
||||||
self.observation = Defaults.observe(key, options: [.prior]) { [weak self] change in
|
self.cancellable = Defaults.publisher(key, options: [.prior])
|
||||||
guard change.isPrior else {
|
.sink { [weak self] change in
|
||||||
return
|
guard change.isPrior else {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
DispatchQueue.mainSafeAsync {
|
DispatchQueue.mainSafeAsync {
|
||||||
self?.objectWillChange.send()
|
self?.objectWillChange.send()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reset the key back to its default value.
|
/// Reset the key back to its default value.
|
||||||
|
|
|
@ -174,9 +174,12 @@ extension Defaults.Serializable {
|
||||||
*/
|
*/
|
||||||
static func toValue(_ anyObject: Any) -> Self? {
|
static func toValue(_ anyObject: Any) -> Self? {
|
||||||
// Return directly if `anyObject` can cast to Value, since it means `Value` is a natively supported type.
|
// Return directly if `anyObject` can cast to Value, since it means `Value` is a natively supported type.
|
||||||
if Self.isNativelySupportedType, let anyObject = anyObject as? Self {
|
if
|
||||||
|
isNativelySupportedType,
|
||||||
|
let anyObject = anyObject as? Self
|
||||||
|
{
|
||||||
return anyObject
|
return anyObject
|
||||||
} else if let value = Self.bridge.deserialize(anyObject as? Serializable) {
|
} else if let value = bridge.deserialize(anyObject as? Serializable) {
|
||||||
return value as? Self
|
return value as? Self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,9 +197,9 @@ extension Defaults.Serializable {
|
||||||
*/
|
*/
|
||||||
static func toSerializable(_ value: Self) -> Any? {
|
static func toSerializable(_ value: Self) -> Any? {
|
||||||
// Return directly if `Self` is a natively supported type, since it does not need serialization.
|
// Return directly if `Self` is a natively supported type, since it does not need serialization.
|
||||||
if Self.isNativelySupportedType {
|
if isNativelySupportedType {
|
||||||
return value
|
return value
|
||||||
} else if let serialized = Self.bridge.serialize(value as? Self.Value) {
|
} else if let serialized = bridge.serialize(value as? Value) {
|
||||||
return serialized
|
return serialized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue