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

Remove duplicates in app storage key. #3186

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,23 @@ extension AppStorageKey: PersistenceKey {
initialValue: Value?,
didSet: @Sendable @escaping (_ newValue: Value?) -> Void
) -> Shared<Value>.Subscription {
let previousValue = LockIsolated(initialValue)
let userDefaultsDidChange = NotificationCenter.default.addObserver(
forName: UserDefaults.didChangeNotification,
object: self.store,
queue: nil
) { _ in
let newValue = load(initialValue: initialValue)
defer { previousValue.withValue { $0 = newValue } }
guard
!(_isEqual(newValue as Any, previousValue.value as Any) ?? false)
|| (_isEqual(newValue as Any, initialValue as Any) ?? true)
else {
return
}
guard !SharedAppStorageLocals.isSetting
else { return }
didSet(load(initialValue: initialValue))
didSet(newValue)
}
let willEnterForeground: (any NSObjectProtocol)?
if let willEnterForegroundNotificationName {
Expand Down Expand Up @@ -365,10 +374,15 @@ private protocol Lookup<Value> {

private struct CastableLookup<Value>: Lookup {
func loadValue(
from store: UserDefaults, at key: String, default defaultValue: Value?
from store: UserDefaults,
at key: String,
default defaultValue: Value?
) -> Value? {
guard let value = store.object(forKey: key) as? Value
else {
guard !SharedAppStorageLocals.isSetting
else { return defaultValue }

SharedAppStorageLocals.$isSetting.withValue(true) {
store.setValue(defaultValue, forKey: key)
}
Expand All @@ -393,6 +407,9 @@ private struct URLLookup: Lookup {
func loadValue(from store: UserDefaults, at key: String, default defaultValue: URL?) -> URL? {
guard let value = store.url(forKey: key)
else {
guard !SharedAppStorageLocals.isSetting
else { return defaultValue }

SharedAppStorageLocals.$isSetting.withValue(true) {
store.set(defaultValue, forKey: key)
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/ComposableArchitectureTests/AppStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ final class AppStorageTests: XCTestCase {
@Shared(.appStorage("count")) var count2: Int? = nil
XCTAssertEqual(count2, nil)
}

func testOptionalInitializers_URL() {
@Shared(.appStorage("url1")) var url1: URL?
XCTAssertEqual(url1, nil)
@Shared(.appStorage("url2")) var url2: URL? = nil
XCTAssertEqual(url2, nil)
}

func testRemoveDuplicates() {
@Dependency(\.defaultAppStorage) var store
@Shared(.appStorage("count")) var count = 0

let values = LockIsolated([Int]())
let cancellable = $count
.publisher
.sink { count in values.withValue { $0.append(count) } }
defer { _ = cancellable }

count += 1
XCTAssertEqual(values.value, [1])

store.setValue(2, forKey: "other-count")
XCTAssertEqual(values.value, [1])
}
}

extension UserDefaults {
Expand Down