Skip to content

Commit 4c009d5

Browse files
committed
Minor tweaks
1 parent 174b791 commit 4c009d5

File tree

6 files changed

+31
-18
lines changed

6 files changed

+31
-18
lines changed

Sources/Defaults/Defaults+Bridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ extension Defaults {
428428
}
429429

430430
extension Defaults {
431-
public struct AnyBridge: Defaults.Bridge, Sendable {
431+
public struct AnyBridge: Bridge, Sendable {
432432
public typealias Value = Defaults.AnySerializable
433433
public typealias Serializable = Any
434434

Sources/Defaults/Defaults.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ extension Defaults.Key {
209209

210210
extension Defaults.Key where Value: Equatable {
211211
/**
212-
Check whether the stored value is the default value.
212+
Indicates whether the value is the same as the default value.
213213
*/
214-
public var isDefaultValue: Bool { self._isDefaultValue }
214+
public var isDefaultValue: Bool { suite[self] == defaultValue }
215215
}
216216

217217
extension Defaults {

Sources/Defaults/Observation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,16 @@ extension Defaults {
287287
}
288288

289289
func start(options: ObservationOptions) {
290-
observations.forEach { $0.start(options: options) }
290+
for observation in observations {
291+
observation.start(options: options)
292+
}
291293
}
292294

293295
func invalidate() {
294-
observations.forEach { $0.invalidate() }
296+
for observation in observations {
297+
observation.invalidate()
298+
}
299+
295300
lifetimeAssociation?.cancel()
296301
}
297302

@@ -305,7 +310,7 @@ extension Defaults {
305310
}
306311

307312
func remove(key: Defaults._AnyKey) {
308-
guard let observation = observations.remove(DefaultsObservation(key: key, self.callback)) else {
313+
guard let observation = observations.remove(DefaultsObservation(key: key, callback)) else {
309314
return
310315
}
311316

Sources/Defaults/SwiftUI.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension Defaults {
3232
task?.cancel()
3333
}
3434

35-
func observe() {
35+
private func observe() {
3636
// We only use this on the latest OSes (as of adding this) since the backdeploy library has a lot of bugs.
3737
if #available(macOS 13, iOS 16, tvOS 16, watchOS 9, visionOS 1.0, *) {
3838
task?.cancel()
@@ -44,7 +44,7 @@ extension Defaults {
4444
return
4545
}
4646

47-
self.objectWillChange.send()
47+
objectWillChange.send()
4848
}
4949
}
5050
} else {
@@ -221,12 +221,26 @@ extension Defaults {
221221
}
222222

223223
extension Defaults.Toggle<Text> {
224-
public init(_ title: some StringProtocol, key: Defaults.Key<Bool>) {
224+
public init(
225+
_ title: some StringProtocol,
226+
key: Defaults.Key<Bool>
227+
) {
225228
self.label = { Text(title) }
226229
self.observable = .init(key)
227230
}
228231
}
229232

233+
extension Defaults.Toggle<Label<Text, Image>> {
234+
public init(
235+
_ title: some StringProtocol,
236+
systemImage: String,
237+
key: Defaults.Key<Bool>
238+
) {
239+
self.label = { Label(title, systemImage: systemImage) }
240+
self.observable = .init(key)
241+
}
242+
}
243+
230244
extension Defaults.Toggle {
231245
/**
232246
Do something when the value changes to a different value.

Tests/DefaultsTests/DefaultsTests.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ final class DefaultsTests: XCTestCase {
771771

772772
async let waiter = Defaults.updates(key, initial: false).first { $0 }
773773

774-
try? await Task.sleep(seconds: 0.1)
774+
try? await Task.sleep(for: .seconds(0.1))
775775

776776
Defaults[key] = true
777777

@@ -798,7 +798,7 @@ final class DefaultsTests: XCTestCase {
798798
}
799799
}()
800800

801-
try? await Task.sleep(seconds: 0.1)
801+
try? await Task.sleep(for: .seconds(0.1))
802802

803803
Defaults[key1] = true
804804
Defaults[key2] = true
@@ -820,10 +820,4 @@ actor Counter {
820820
}
821821
}
822822

823-
// TODO: Remove when testing on macOS 13.
824-
extension Task<Never, Never> {
825-
static func sleep(seconds: TimeInterval) async throws {
826-
try await sleep(nanoseconds: UInt64(seconds * Double(NSEC_PER_SEC)))
827-
}
828-
}
829823
// swiftlint:enable discouraged_optional_boolean

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Store key-value pairs persistently across launches of your app.
66

77
It uses `UserDefaults` underneath but exposes a type-safe facade with lots of nice conveniences.
88

9-
It's used in production by [all my apps](https://sindresorhus.com/apps) (1 million+ users).
9+
It's used in production by [all my apps](https://sindresorhus.com/apps) (4 million+ users).
1010

1111
## Highlights
1212

0 commit comments

Comments
 (0)