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

Save codable models #64

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions Example/Tests/Specs/Defaults/DefaultsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ enum SomeIntEnum: Int {
case type3
}

/// Some codable
struct SomeCodable: Codable {
var identifier: String
var name: String?
}

// Define the keys used for this test.
extension DefaultsKeys {
static let enumStringTypeValue = DefaultsKey<SomeStringEnum?>("enumStringTypeValue")
Expand All @@ -35,12 +41,40 @@ extension DefaultsKeys {
static let booleanValue = DefaultsKey<Bool?>("booleanValue")
static let dateValue = DefaultsKey<Date?>("dateValue")
static let stringsValue = DefaultsKey<[String]?>("stringsValue")
static let codableValue = DefaultsKey<SomeCodable?>("codableValue")
}

class DefaultsSpec: QuickSpec {
// swiftlint:disable function_body_length
override func spec() {
describe("defaults") {
context("Codable value") {
it("should be able to write a codable value to the defaults") {
let someCodable = SomeCodable(identifier: "1", name: "someName")
Defaults[.codableValue] = someCodable
let data = UserDefaults.standard.object(forKey: "codableValue") as? Data
let model = try? JSONDecoder().decode(SomeCodable.self, from: data ?? Data())
expect(model?.name) == "someName"
}

it("should be able to clear the codable defaults") {
let someCodable = SomeCodable(identifier: "1", name: "someName")
Defaults[.codableValue] = someCodable
Defaults[.codableValue] = nil
let data = UserDefaults.standard.object(forKey: "codableValue") as? Data
let model = try? JSONDecoder().decode(SomeCodable.self, from: data ?? Data())
expect(model).to(beNil())
}

it("should be able to read the codable defaults") {
let someCodable = SomeCodable(identifier: "1", name: "someName")
let value = try? JSONEncoder().encode(someCodable)
UserDefaults.standard.set(value, forKey: "codableValue")
let name = Defaults[.codableValue]?.name
expect(name) == "someName"
}
}

context("enumType value + String") {
it("should be able to write to the defaults") {
Defaults[.enumStringTypeValue] = .type1
Expand Down
22 changes: 22 additions & 0 deletions Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ open class DefaultsKey<ValueType>: DefaultsKeys {
}

public extension UserDefaults {
/// Get the defaults Codable model value for the given `DefaultsKey`. The preferred way to do this is to pass
/// the static key variable defined in the `DefaultsKeys` extension.
///
/// ```
/// static let someCodableModel = DefaultsKey<CodableModel?>("the model defaults key")
/// ```
public subscript<T: Codable>(key: DefaultsKey<T?>) -> T? {
get {
guard
let savedData = object(forKey: key.key) as? Data,
let codableModel = try? JSONDecoder().decode(T.self, from: savedData) else { return nil }
return codableModel
}
set {
if let encoded = try? JSONEncoder().encode(newValue) {
set(encoded, forKey: key.key)
} else {
removeObject(forKey: key.key)
}
}
}

/// Get the defaults Enum (type String) value for the given `DefaultsKey`. The preferred way to do this is to pass
/// the static key variable defined in the `DefaultsKeys` extension.
///
Expand Down