Skip to content

Commit

Permalink
Renamed UserDefaultsBacked to UserDefaultsStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jan 13, 2023
1 parent 431d8ce commit bb599ed
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 44 deletions.
61 changes: 53 additions & 8 deletions Sources/OTCore/Extensions/Foundation/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ extension UserDefaults {
///
/// If a default value is provided, the `Value` will be treated as a non-Optional with a default.
///
/// @UserDefaultsBacked(key: "myPref")
/// @UserDefaultsStorage(key: "myPref")
/// var myPref: Bool = true
///
/// If no default is provided, the `Value` will be treated as an Optional.
///
/// @UserDefaultsBacked(key: "myPref")
/// @UserDefaultsStorage(key: "myPref")
/// var myPref: Bool?
///
/// A different type than the underlying storage type can be used.
Expand All @@ -77,7 +77,7 @@ extension UserDefaults {
/// // Stored as a `String`, but the var is an `Int`.
/// // get closure: transform `String` into `Int`
/// // set closure: transform `Int` into `String`
/// @UserDefaultsBacked(
/// @UserDefaultsStorage(
/// key: "myPref",
/// get: { Int($0) },
/// set: { "\($0)" }
Expand All @@ -89,7 +89,7 @@ extension UserDefaults {
/// // Stored as a `String`, but the var is an `Int`.
/// // get closure: transform `String?` into `Int`
/// // set closure: transform `Int` into `String`
/// @UserDefaultsBacked(
/// @UserDefaultsStorage(
/// key: "myPref",
/// get: { Int($0 ?? "") ?? 0 },
/// set: { "\($0)" }
Expand All @@ -101,7 +101,7 @@ extension UserDefaults {
/// A special value validation closure is available when the value type matches the stored value
/// type.
///
/// @UserDefaultsBacked(
/// @UserDefaultsStorage(
/// key: "myPref",
/// validation: { $0.trimmingCharacters(in: .whitespaces) },
/// )
Expand All @@ -110,11 +110,11 @@ extension UserDefaults {
/// A special value clamping closure is available when the value type matches the stored value
/// type. Any types (not just integers) that can form a range can be clamped.
///
/// @UserDefaultsBacked(key: "myPref", clamped: 5 ... 10)
/// @UserDefaultsStorage(key: "myPref", clamped: 5 ... 10)
/// var pref = 1 // will be clamped to 5
///
@propertyWrapper
public struct UserDefaultsBacked<Value, StorageValue> {
public struct UserDefaultsStorage<Value, StorageValue> {
private let key: String
private let defaultValue: Any
public var storage: UserDefaults
Expand Down Expand Up @@ -303,7 +303,7 @@ public struct UserDefaultsBacked<Value, StorageValue> {
}
}

extension UserDefaultsBacked where Value: ExpressibleByNilLiteral {
extension UserDefaultsStorage where Value: ExpressibleByNilLiteral {
// MARK: Init - Same Type

public init(
Expand Down Expand Up @@ -350,4 +350,49 @@ extension UserDefaultsBacked where Value: ExpressibleByNilLiteral {
}
}

// MARK: - API Changes in 1.4.6
@available(*, unavailable, renamed: "UserDefaultsStorage")
@propertyWrapper
public struct UserDefaultsBacked<Value> {
public var storage: UserDefaults
public var wrappedValue: Value

//public init(wrappedValue: Value) { fatalError() }

//public init(key: String) { fatalError() }

public init(
wrappedValue defaultValue: Value,
key: String,
storage: UserDefaults = .standard
) { fatalError() }

public init<R: RangeExpression>(
wrappedValue defaultValue: Value,
key: String,
clamped range: R,
storage: UserDefaults = .standard
) where R.Bound == Value { fatalError() }

public init(
wrappedValue defaultValue: Value,
key: String,
validation closure: @escaping (Value) -> Value,
storage: UserDefaults = .standard
) { fatalError() }

/* extension UserDefaultsBacked where Value: ExpressibleByNilLiteral { */

public init<V>(
key: String,
storage: UserDefaults = .standard
) where Value == V? { fatalError() }

public init<V>(
key: String,
validation closure: @escaping (Value) -> Value,
storage: UserDefaults = .standard
) where Value == V? { fatalError() }
}

#endif
72 changes: 36 additions & 36 deletions Tests/OTCoreTests/Extensions/Foundation/UserDefaults Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertFalse(ud.exists(key: "does_not_exist"))
}

func testUserDefaultsBacked_Defaulted_NoPreviousValue() {
func testUserDefaultsStorage_Defaulted_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "defaultedPref"

@UserDefaultsBacked(key: prefKey, storage: ud)
@UserDefaultsStorage(key: prefKey, storage: ud)
var pref = 2
}

Expand All @@ -101,11 +101,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 4)
}

func testUserDefaultsBacked_Defaulted_HasPreviousValue() {
func testUserDefaultsStorage_Defaulted_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "defaultedPref"

@UserDefaultsBacked(key: prefKey, storage: ud)
@UserDefaultsStorage(key: prefKey, storage: ud)
var pref = 2
}

Expand All @@ -123,11 +123,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 4)
}

func testUserDefaultsBacked_NonDefaulted_NoPreviousValue() {
func testUserDefaultsStorage_NonDefaulted_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "nonDefaultedPref"

@UserDefaultsBacked(key: prefKey, storage: ud)
@UserDefaultsStorage(key: prefKey, storage: ud)
var pref: String?
}

Expand All @@ -148,11 +148,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, nil)
}

func testUserDefaultsBacked_NonDefaulted_HasPreviousValue() {
func testUserDefaultsStorage_NonDefaulted_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "nonDefaultedPref"

@UserDefaultsBacked(key: prefKey, storage: ud)
@UserDefaultsStorage(key: prefKey, storage: ud)
var pref: String?
}

Expand All @@ -176,11 +176,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, nil)
}

func testUserDefaultsBacked_Defaulted_Clamped_NoPreviousValue() {
func testUserDefaultsStorage_Defaulted_Clamped_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "clampedPref"

@UserDefaultsBacked(key: prefKey, clamped: 5 ... 10, storage: ud)
@UserDefaultsStorage(key: prefKey, clamped: 5 ... 10, storage: ud)
var pref = 1
}

Expand Down Expand Up @@ -216,11 +216,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 10)
}

func testUserDefaultsBacked_Defaulted_Clamped_HasPreviousValue() {
func testUserDefaultsStorage_Defaulted_Clamped_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "clampedPref"

@UserDefaultsBacked(key: prefKey, clamped: 5 ... 10, storage: ud)
@UserDefaultsStorage(key: prefKey, clamped: 5 ... 10, storage: ud)
var pref = 1
}

Expand Down Expand Up @@ -259,11 +259,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 10)
}

func testUserDefaultsBacked_Defaulted_Validated_NoPreviousValue() {
func testUserDefaultsStorage_Defaulted_Validated_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "validatedPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
validation: { $0.clamped(to: 5 ... 10) },
storage: ud
Expand Down Expand Up @@ -303,11 +303,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 10)
}

func testUserDefaultsBacked_Defaulted_Validated_HasPreviousValue() {
func testUserDefaultsStorage_Defaulted_Validated_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "validatedPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
validation: { $0.clamped(to: 5 ... 10) },
storage: ud
Expand Down Expand Up @@ -350,11 +350,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 10)
}

func testUserDefaultsBacked_Defaulted_GetSet_NoPreviousValue() {
func testUserDefaultsStorage_Defaulted_GetSet_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "transformedPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { Int($0) },
set: { $0 < 5 ? "\($0)" : nil },
Expand All @@ -381,11 +381,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 1)
}

func testUserDefaultsBacked_Defaulted_GetSet_HasPreviousValue() {
func testUserDefaultsStorage_Defaulted_GetSet_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "transformedPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { Int($0) },
set: { $0 < 8 ? "\($0)" : nil },
Expand All @@ -409,11 +409,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 4)
}

func testUserDefaultsBacked_NonDefaulted_GetSet_NoPreviousValue() {
func testUserDefaultsStorage_NonDefaulted_GetSet_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "nonDefaultedTransformedPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { Int($0) },
set: { $0 != nil ? "\($0!)" : nil },
Expand All @@ -439,11 +439,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, nil)
}

func testUserDefaultsBacked_NonDefaulted_GetSet_HasPreviousValue() {
func testUserDefaultsStorage_NonDefaulted_GetSet_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "nonDefaultedTransformedPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { Int($0) },
set: { $0 != nil ? "\($0!)" : nil },
Expand All @@ -467,12 +467,12 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 4)
}

func testUserDefaultsBacked_ComputedOnly_Generic() {
func testUserDefaultsStorage_ComputedOnly_Generic() {
struct DummyPrefs {
static let prefKey = "computedOnlyPref"

// not used, just here to see if this alternate syntax compiles
@UserDefaultsBacked<Int, String>(
@UserDefaultsStorage<Int, String>(
key: prefKey,
get: { $0 != nil ? Int($0!) ?? 0 : 0 },
set: { "\($0)" },
Expand All @@ -481,7 +481,7 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
private var pref1: Int

// not used, just here to see if this alternate syntax compiles
@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { (storedValue: String?) in
storedValue != nil ? Int(storedValue!) ?? 0 : 0
Expand All @@ -495,11 +495,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
_ = DummyPrefs() // forces property wrappers to init
}

func testUserDefaultsBacked_ComputedOnly_NoPreviousValue() {
func testUserDefaultsStorage_ComputedOnly_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "computedOnlyPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { $0 != nil ? Int($0!) ?? -1 : -1 },
set: { "\($0)" },
Expand All @@ -520,11 +520,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 4)
}

func testUserDefaultsBacked_ComputedOnly_Optional_NoPreviousValue() {
func testUserDefaultsStorage_ComputedOnly_Optional_NoPreviousValue() {
struct DummyPrefs {
static let prefKey = "computedOnlyOptionalPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { $0 != nil ? Int($0!) ?? -1 : -1 },
set: { "\($0 ?? -1)" },
Expand All @@ -550,11 +550,11 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, -1)
}

func testUserDefaultsBacked_ComputedOnly_HasPreviousValue() {
func testUserDefaultsStorage_ComputedOnly_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "computedOnlyPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { $0 != nil ? Int($0!) ?? -1 : -1 },
set: { "\($0)" },
Expand All @@ -578,19 +578,19 @@ class Extensions_Foundation_UserDefaults_Tests: XCTestCase {
XCTAssertEqual(dummyPrefs.pref, 4)
}

func testUserDefaultsBacked_ComputedOnly_Optional_HasPreviousValue() {
func testUserDefaultsStorage_ComputedOnly_Optional_HasPreviousValue() {
struct DummyPrefs {
static let prefKey = "computedOnlyOptionalPref"

@UserDefaultsBacked(
@UserDefaultsStorage(
key: prefKey,
get: { $0 != nil ? Int($0!) ?? -1 : -1 },
set: { "\($0 ?? -1)" },
storage: ud
)
var pref: Int?

@UserDefaultsBacked(
@UserDefaultsStorage(
key: "myPref",
get: { Int($0 ?? "") ?? 0 },
set: { "\($0)" }
Expand Down

0 comments on commit bb599ed

Please sign in to comment.