|
509 | 509 | }
|
510 | 510 |
|
511 | 511 | extension DependencyValues {
|
| 512 | + /// Default file storage used by ``SharedReaderKey/appStorage(_:)``. |
| 513 | + /// |
| 514 | + /// Use this dependency to override the manner in which |
| 515 | + /// ``SharedReaderKey/appStorage(_:)`` interacts with UserDefaults. For |
| 516 | + /// example, while your app is running for UI tests you probably do not want your features writing |
| 517 | + /// changes to your actual UserDefaults suite, which would cause that data to bleed over from test to test. |
| 518 | + /// |
| 519 | + /// So, for that situation you can use the ``inMemory`` value so that each |
| 520 | + /// run of the app starts with a fresh suite that will never interfere with other tests: |
| 521 | + /// |
| 522 | + /// ```swift |
| 523 | + /// import Dependencies |
| 524 | + /// import Sharing |
| 525 | + /// import SwiftUI |
| 526 | + /// |
| 527 | + /// @main |
| 528 | + /// struct MyApp: App { |
| 529 | + /// init() { |
| 530 | + /// if ProcessInfo.processInfo.environment["UITesting"] == "true" |
| 531 | + /// prepareDependencies { |
| 532 | + /// $0.defaultAppStorage = .inMemory |
| 533 | + /// } |
| 534 | + /// } |
| 535 | + /// } |
| 536 | + /// // ... |
| 537 | + /// } |
| 538 | + /// ``` |
512 | 539 | public var defaultAppStorage: UserDefaults {
|
513 | 540 | get { self[DefaultAppStorageKey.self].value }
|
514 | 541 | set { self[DefaultAppStorageKey.self].value = newValue }
|
|
521 | 548 | }
|
522 | 549 |
|
523 | 550 | private enum DefaultAppStorageKey: DependencyKey {
|
524 |
| - static var testValue: UncheckedSendable<UserDefaults> { |
525 |
| - let suiteName: String |
526 |
| - // NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using |
527 |
| - // file-based suites. Go back to using temporary directory always when we drop iOS 16 |
528 |
| - // support. |
529 |
| - if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) { |
530 |
| - suiteName = "co.pointfree.Sharing.\(UUID().uuidString)" |
531 |
| - } else { |
532 |
| - suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)" |
533 |
| - } |
534 |
| - return UncheckedSendable( |
535 |
| - UserDefaults(suiteName: suiteName)! |
536 |
| - ) |
| 551 | + static var liveValue: UncheckedSendable<UserDefaults> { |
| 552 | + UncheckedSendable(.standard) |
537 | 553 | }
|
538 | 554 | static var previewValue: UncheckedSendable<UserDefaults> {
|
539 |
| - testValue |
| 555 | + UncheckedSendable(.inMemory) |
540 | 556 | }
|
541 |
| - static var liveValue: UncheckedSendable<UserDefaults> { |
542 |
| - UncheckedSendable(UserDefaults.standard) |
| 557 | + static var testValue: UncheckedSendable<UserDefaults> { |
| 558 | + UncheckedSendable(.inMemory) |
543 | 559 | }
|
544 | 560 | }
|
545 | 561 |
|
| 562 | +extension UserDefaults { |
| 563 | + public static var inMemory: UserDefaults { |
| 564 | + let suiteName: String |
| 565 | + // NB: Due to a bug in iOS 16 and lower, UserDefaults does not observe changes when using |
| 566 | + // file-based suites. Go back to using temporary directory always when we drop iOS 16 |
| 567 | + // support. |
| 568 | + if #unavailable(iOS 17, macOS 14, tvOS 17, watchOS 10, visionOS 1) { |
| 569 | + suiteName = "co.pointfree.Sharing.\(UUID().uuidString)" |
| 570 | + } else { |
| 571 | + suiteName = "\(NSTemporaryDirectory())co.pointfree.Sharing.\(UUID().uuidString)" |
| 572 | + } |
| 573 | + return UserDefaults(suiteName: suiteName)! |
| 574 | + } |
| 575 | +} |
| 576 | + |
546 | 577 | private enum AppStorageKeyFormatWarningEnabledKey: DependencyKey {
|
547 | 578 | static let liveValue = true
|
548 | 579 | static let testValue = true
|
|
0 commit comments