Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ApolloZhu committed Sep 17, 2019
1 parent 202a557 commit cd391c2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
33 changes: 19 additions & 14 deletions CocoaPods/Cocoapods/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,31 @@ extension UserDefaults {

extension UIImage: YYCacheStorable { } // Auto synthesized implmentation for NSCoding

extension Bool: KeychainAccessStorable {
public func asKeychainStorable() -> KeychainAccessStorable! {
extension Double: KeychainAccessStorable {
public func asKeychainStorable() -> Result<AsIsKeychainAccessStorable, Error> {
return "\(self)".asKeychainStorable()
}
public static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Bool? {
return String.fromKeychain(keychain, forKey: key).map {
$0 == "true"
}
public static func fromKeychain(_ keychain: Keychain, forKey key: String) -> Double? {
return String.fromKeychain(keychain, forKey: key).flatMap(Double.init)
}
}

extension EFStorageKeychainAccessRef {
@_dynamicReplacement(for: onConversionFailure(for:dueTo:))
func doNothingOnConversionFailure(for content: Content, dueTo error: Error) {
print("\(content) -> \(error.localizedDescription)")
}
}

// MARK: Allow optional default value

extension Optional: UserDefaultsStorable where Wrapped: UserDefaultsStorable {
public static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Optional<Wrapped>? {
return Wrapped.fromUserDefaults(userDefaults, forKey: key)
public func asUserDefaultsStorable() -> AsIsUserDefaultsStorable! {
return self.flatMap { $0.asUserDefaultsStorable() }
}

public func asUserDefaultsStorable() -> UserDefaultsStorable! {
return self.flatMap { $0.asUserDefaultsStorable() }
public static func fromUserDefaults(_ userDefaults: UserDefaults, forKey key: String) -> Optional<Wrapped>? {
return Wrapped.fromUserDefaults(userDefaults, forKey: key)
}
}

Expand All @@ -65,8 +70,8 @@ class ViewController: UIViewController {
@EFStorageUserDefaults(forKey: "username", defaultsTo: nil)
var username: String?

@EFStorageKeychainAccess(forKey: "isNewUser", defaultsTo: true)
var isNewUser: Bool
@EFStorageKeychainAccess(forKey: "isNewUser", defaultsTo: 0)
var isNewUser: Double

@EFStorageUserDefaults(forKey: "legacyNames", defaultsTo: [])
var usedNames: [String]
Expand All @@ -87,11 +92,11 @@ class ViewController: UIViewController {
print(usedNames)

avatarView.image = avatar
if !isNewUser {
if isNewUser == .infinity {
label.text = "Welcome back,"
}
textField.text = username
Keychain.efStorage.isNewUser = false
Keychain.efStorage.isNewUser = Double.infinity

textField.addTarget(self, action: #selector(updateUsername),
for: .allEditingEvents)
Expand Down
2 changes: 2 additions & 0 deletions Example/Sources/Example/main.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import EFStorageUserDefaults
import Foundation

print(UserDefaults.standard)

UserDefaults.standard.removeObject(forKey: "catSound")

struct UD {
Expand Down
20 changes: 12 additions & 8 deletions Example/Tests/ExampleTests/ExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ final class ExampleTests: XCTestCase {
process.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)

let output = try XCTUnwrap(String(data: data, encoding: .utf8))

let endOfFirstLine = try XCTUnwrap(output.firstIndex(of: "\n"))
let userDefaults = output[..<endOfFirstLine]

XCTAssertEqual(output, """
ALLOC EFStorageUserDefaultsRef<String>
CREAT EFStorageUserDefaultsRef<String> catSound
\(userDefaults)
EFStorage ALLOC EFStorageUserDefaultsRef<String>
EFStorage CREAT EFStorageUserDefaultsRef<String> catSound IN \(userDefaults)
nyan
FETCH EFStorageUserDefaultsRef<String> catSound
EFStorage FETCH EFStorageUserDefaultsRef<String> catSound FROM \(userDefaults)
nyan
FETCH EFStorageUserDefaultsRef<String> catSound
EFStorage FETCH EFStorageUserDefaultsRef<String> catSound FROM \(userDefaults)
meow
meow
FETCH EFStorageUserDefaultsRef<String> catSound
FETCH EFStorageUserDefaultsRef<String> catSound
EFStorage FETCH EFStorageUserDefaultsRef<String> catSound FROM \(userDefaults)
EFStorage FETCH EFStorageUserDefaultsRef<String> catSound FROM \(userDefaults)
""")
Expand Down

0 comments on commit cd391c2

Please sign in to comment.