From 6ff23ff1f8a63bb0342aa38b69093645f2df7397 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 10 Jun 2021 13:06:19 +0700 Subject: [PATCH] Minor cleanup (#74) --- Sources/Defaults/Defaults+Bridge.swift | 16 ++++++++-------- Sources/Defaults/Defaults+Protocol.swift | 2 +- .../Defaults/Migration/Migration+Defaults.swift | 8 ++++---- .../Migration/v5/Migration+Extensions.swift | 2 +- Sources/Defaults/SwiftUI.swift | 17 +++++++++-------- Sources/Defaults/Utilities.swift | 11 +++++++---- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Sources/Defaults/Defaults+Bridge.swift b/Sources/Defaults/Defaults+Bridge.swift index f0815ef..b41287a 100644 --- a/Sources/Defaults/Defaults+Bridge.swift +++ b/Sources/Defaults/Defaults+Bridge.swift @@ -53,7 +53,7 @@ extension Defaults { } extension Defaults { - public struct RawRepresentableBridge: Defaults.Bridge { + public struct RawRepresentableBridge: Bridge { public typealias Value = Value public typealias Serializable = Value.RawValue @@ -72,7 +72,7 @@ extension Defaults { } extension Defaults { - public struct NSSecureCodingBridge: Defaults.Bridge { + public struct NSSecureCodingBridge: Bridge { public typealias Value = Value public typealias Serializable = Data @@ -109,7 +109,7 @@ extension Defaults { } extension Defaults { - public struct OptionalBridge: Defaults.Bridge { + public struct OptionalBridge: Bridge { public typealias Value = Wrapped.Value public typealias Serializable = Wrapped.Serializable @@ -124,7 +124,7 @@ extension Defaults { } extension Defaults { - public struct ArrayBridge: Defaults.Bridge { + public struct ArrayBridge: Bridge { public typealias Value = [Element] public typealias Serializable = [Element.Serializable] @@ -147,7 +147,7 @@ extension Defaults { } extension Defaults { - public struct DictionaryBridge: Defaults.Bridge { + public struct DictionaryBridge: Bridge { public typealias Value = [Key: Element.Value] 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)` 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 { - public struct SetBridge: Defaults.Bridge { + public struct SetBridge: Bridge { public typealias Value = Set public typealias Serializable = Any @@ -221,7 +221,7 @@ extension Defaults { } extension Defaults { - public struct SetAlgebraBridge: Defaults.Bridge where Value.Element: Defaults.Serializable { + public struct SetAlgebraBridge: Bridge where Value.Element: Serializable { public typealias Value = Value public typealias Element = Value.Element public typealias Serializable = Any @@ -260,7 +260,7 @@ extension Defaults { } extension Defaults { - public struct CollectionBridge: Defaults.Bridge where Value.Element: Defaults.Serializable { + public struct CollectionBridge: Bridge where Value.Element: Serializable { public typealias Value = Value public typealias Element = Value.Element public typealias Serializable = Any diff --git a/Sources/Defaults/Defaults+Protocol.swift b/Sources/Defaults/Defaults+Protocol.swift index 5a4b865..8d2f944 100644 --- a/Sources/Defaults/Defaults+Protocol.swift +++ b/Sources/Defaults/Defaults+Protocol.swift @@ -99,4 +99,4 @@ public protocol DefaultsSetAlgebraSerializable: SetAlgebra, Defaults.Serializabl } /// Convenience protocol for `Codable`. -public protocol DefaultsCodableBridge: DefaultsBridge where Serializable == String, Value: Codable {} +public protocol DefaultsCodableBridge: Defaults.Bridge where Serializable == String, Value: Codable {} diff --git a/Sources/Defaults/Migration/Migration+Defaults.swift b/Sources/Defaults/Migration/Migration+Defaults.swift index fe1e58b..0a720cb 100644 --- a/Sources/Defaults/Migration/Migration+Defaults.swift +++ b/Sources/Defaults/Migration/Migration+Defaults.swift @@ -16,15 +16,15 @@ extension Defaults { Defaults.migrate(.array, to: .v5) ``` */ - public static func migrate(_ keys: Key..., to version: Version) { + public static func migrate(_ keys: Key..., to version: Version) { migrate(keys, to: version) } - public static func migrate(_ keys: Key..., to version: Version) { + public static func migrate(_ keys: Key..., to version: Version) { migrate(keys, to: version) } - public static func migrate(_ keys: [Key], to version: Version) { + public static func migrate(_ keys: [Key], to version: Version) { switch version { case .v5: for key in keys { @@ -34,7 +34,7 @@ extension Defaults { } } - public static func migrate(_ keys: [Key], to version: Version) { + public static func migrate(_ keys: [Key], to version: Version) { switch version { case .v5: for key in keys { diff --git a/Sources/Defaults/Migration/v5/Migration+Extensions.swift b/Sources/Defaults/Migration/v5/Migration+Extensions.swift index 3aabb53..47988ab 100644 --- a/Sources/Defaults/Migration/v5/Migration+Extensions.swift +++ b/Sources/Defaults/Migration/v5/Migration+Extensions.swift @@ -180,7 +180,7 @@ extension Defaults.SetAlgebraSerializable where Self: Defaults.NativeType, Eleme 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 { NativeForm(rawValue: self.rawValue)! } diff --git a/Sources/Defaults/SwiftUI.swift b/Sources/Defaults/SwiftUI.swift index da98d93..8a7b219 100644 --- a/Sources/Defaults/SwiftUI.swift +++ b/Sources/Defaults/SwiftUI.swift @@ -5,7 +5,7 @@ import Combine @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) extension Defaults { final class Observable: ObservableObject { - private var observation: DefaultsObservation? + private var cancellable: AnyCancellable? private let key: Defaults.Key let objectWillChange = ObservableObjectPublisher() @@ -21,15 +21,16 @@ extension Defaults { init(_ key: Key) { self.key = key - self.observation = Defaults.observe(key, options: [.prior]) { [weak self] change in - guard change.isPrior else { - return - } + self.cancellable = Defaults.publisher(key, options: [.prior]) + .sink { [weak self] change in + guard change.isPrior else { + return + } - DispatchQueue.mainSafeAsync { - self?.objectWillChange.send() + DispatchQueue.mainSafeAsync { + self?.objectWillChange.send() + } } - } } /// Reset the key back to its default value. diff --git a/Sources/Defaults/Utilities.swift b/Sources/Defaults/Utilities.swift index fb48134..b19fab9 100644 --- a/Sources/Defaults/Utilities.swift +++ b/Sources/Defaults/Utilities.swift @@ -174,9 +174,12 @@ extension Defaults.Serializable { */ static func toValue(_ anyObject: Any) -> Self? { // 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 - } else if let value = Self.bridge.deserialize(anyObject as? Serializable) { + } else if let value = bridge.deserialize(anyObject as? Serializable) { return value as? Self } @@ -194,9 +197,9 @@ extension Defaults.Serializable { */ static func toSerializable(_ value: Self) -> Any? { // Return directly if `Self` is a natively supported type, since it does not need serialization. - if Self.isNativelySupportedType { + if isNativelySupportedType { return value - } else if let serialized = Self.bridge.serialize(value as? Self.Value) { + } else if let serialized = bridge.serialize(value as? Value) { return serialized }