Code style tweaks

This commit is contained in:
Sindre Sorhus 2021-11-15 02:53:40 +07:00
parent 55f3302c3a
commit 5b30f01e46
8 changed files with 72 additions and 26 deletions

View File

@ -144,9 +144,13 @@ extension Color: Defaults.Serializable {
}
#if os(macOS)
/// `NSColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`.
/**
`NSColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`.
*/
extension NSColor: Defaults.Serializable {}
#else
/// `UIColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`.
/**
`UIColor` conforms to `NSSecureCoding`, so it goes to `NSSecureCodingBridge`.
*/
extension UIColor: Defaults.Serializable {}
#endif

View File

@ -21,10 +21,14 @@ public protocol DefaultsSerializable {
typealias Serializable = Bridge.Serializable
associatedtype Bridge: DefaultsBridge
/// Static bridge for the `Value` which cannot be stored natively.
/**
Static bridge for the `Value` which cannot be stored natively.
*/
static var bridge: Bridge { get }
/// A flag to determine whether `Value` can be stored natively or not.
/**
A flag to determine whether `Value` can be stored natively or not.
*/
static var isNativelySupportedType: Bool { get }
}
@ -89,16 +93,22 @@ public protocol DefaultsBridge {
}
public protocol DefaultsCollectionSerializable: Collection, Defaults.Serializable {
/// `Collection` does not have a initializer, but we need a initializer to convert an array into the `Value`.
/**
`Collection` does not have a initializer, but we need a initializer to convert an array into the `Value`.
*/
init(_ elements: [Element])
}
public protocol DefaultsSetAlgebraSerializable: SetAlgebra, Defaults.Serializable {
/// Since `SetAlgebra` protocol does not conform to `Sequence`, we cannot convert a `SetAlgebra` to an `Array` directly.
/**
Since `SetAlgebra` protocol does not conform to `Sequence`, we cannot convert a `SetAlgebra` to an `Array` directly.
*/
func toArray() -> [Element]
}
/// Convenience protocol for `Codable`.
/**
Convenience protocol for `Codable`.
*/
public protocol DefaultsCodableBridge: Defaults.Bridge where Serializable == String, Value: Codable {}
/**

View File

@ -7,7 +7,9 @@ public protocol DefaultsBaseKey: Defaults.AnyKey {
}
extension DefaultsBaseKey {
/// Reset the item back to its default value.
/**
Reset the item back to its default value.
*/
public func reset() {
suite.removeObject(forKey: name)
}
@ -39,8 +41,11 @@ public enum Defaults {
public final class Key<Value: Serializable>: AnyKey {
public let defaultValue: Value
/// Create a defaults key.
/// The `default` parameter can be left out if the `Value` type is an optional.
/**
Create a defaults key.
The `default` parameter can be left out if the `Value` type is an optional.
*/
public init(_ key: String, default defaultValue: Value, suite: UserDefaults = .standard) {
self.defaultValue = defaultValue

View File

@ -28,10 +28,14 @@ extension Defaults {
public typealias Observation = DefaultsObservation
public enum ObservationOption {
/// Whether a notification should be sent to the observer immediately, before the observer registration method even returns.
/**
Whether a notification should be sent to the observer immediately, before the observer registration method even returns.
*/
case initial
/// Whether separate notifications should be sent to the observer before and after each change, instead of a single notification after the change.
/**
Whether separate notifications should be sent to the observer before and after each change, instead of a single notification after the change.
*/
case prior
}

View File

@ -33,7 +33,9 @@ extension Defaults {
}
}
/// Reset the key back to its default value.
/**
Reset the key back to its default value.
*/
func reset() {
key.reset()
}
@ -47,7 +49,7 @@ public struct Default<Value: Defaults.Serializable>: DynamicProperty {
private let key: Defaults.Key<Value>
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamicaly changed.
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed.
@ObservedObject private var observable: Defaults.Observable<Value>
/**
@ -87,10 +89,14 @@ public struct Default<Value: Defaults.Serializable>: DynamicProperty {
public var projectedValue: Binding<Value> { $observable.value }
/// The default value of the key.
/**
The default value of the key.
*/
public var defaultValue: Value { key.defaultValue }
/// Combine publisher that publishes values when the `Defaults` item changes.
/**
Combine publisher that publishes values when the `Defaults` item changes.
*/
public var publisher: Publisher { Defaults.publisher(key) }
public mutating func update() {
@ -123,7 +129,9 @@ public struct Default<Value: Defaults.Serializable>: DynamicProperty {
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Default where Value: Equatable {
/// Indicates whether the value is the same as the default value.
/**
Indicates whether the value is the same as the default value.
*/
public var isDefaultValue: Bool { wrappedValue == defaultValue }
}
@ -165,7 +173,7 @@ extension Defaults {
private let label: () -> Label
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamicaly changed.
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed.
@ObservedObject private var observable: Defaults.Observable<Bool>
public init(key: Key, @ViewBuilder label: @escaping () -> Label) {
@ -192,7 +200,9 @@ extension Defaults.Toggle where Label == Text {
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
extension Defaults.Toggle {
/// Do something when the value changes to a different value.
/**
Do something when the value changes to a different value.
*/
public func onChange(_ action: @escaping (Bool) -> Void) -> Self {
onChange = action
return self

View File

@ -119,10 +119,15 @@ final class LifetimeAssociation {
}
/// A protocol for making generic type constraints of optionals.
/// - Note: It's intentionally not including `associatedtype Wrapped` as that limits a lot of the use-cases.
/**
A protocol for making generic type constraints of optionals.
- Note: It's intentionally not including `associatedtype Wrapped` as that limits a lot of the use-cases.
*/
public protocol _DefaultsOptionalType: ExpressibleByNilLiteral {
/// This is useful as you can't compare `_OptionalType` to `nil`.
/**
This is useful as you cannot compare `_OptionalType` to `nil`.
*/
var isNil: Bool { get }
}
@ -146,7 +151,9 @@ extension DispatchQueue {
extension Sequence {
/// Returns an array containing the non-nil elements.
/**
Returns an array containing the non-nil elements.
*/
func compact<T>() -> [T] where Element == T? {
// TODO: Make this `compactMap(\.self)` when https://bugs.swift.org/browse/SR-12897 is fixed.
compactMap { $0 }

View File

@ -25,7 +25,9 @@ private struct TimeZone: Hashable {
}
extension TimeZone: Defaults.NativeType {
/// Associated `CodableForm` to `CodableTimeZone`.
/**
Associated `CodableForm` to `CodableTimeZone`.
*/
typealias CodableForm = CodableTimeZone
static let bridge = TimeZoneBridge()
@ -37,7 +39,9 @@ private struct CodableTimeZone {
}
extension CodableTimeZone: Defaults.CodableType {
/// Convert from `Codable` to `Native`.
/**
Convert from `Codable` to `Native`.
*/
func toNative() -> TimeZone {
TimeZone(id: id, name: name)
}

View File

@ -380,7 +380,9 @@ private struct CodableTimeZone {
}
extension CodableTimeZone: Defaults.CodableType {
/// Convert from `Codable` to native type.
/**
Convert from `Codable` to native type.
*/
func toNative() -> TimeZone {
TimeZone(id: id, name: name)
}