Some refactoring after Steves comments

This commit is contained in:
ismailgulek
2020-07-22 17:07:54 +03:00
parent 8e7c846ada
commit 9be4d599c5
4 changed files with 41 additions and 20 deletions

View File

@@ -18,8 +18,8 @@ import Foundation
typealias KeyValueStoreKey = String
@objc protocol KeyValueStore {
func setObject(forKey key: KeyValueStoreKey, value: Any?)
func getObject(forKey key: KeyValueStoreKey) -> Any?
func removeObject(forKey key: KeyValueStoreKey)
protocol KeyValueStore {
func set(_ value: Any?, forKey key: KeyValueStoreKey) throws
func object(forKey key: KeyValueStoreKey) throws -> Any?
func removeObject(forKey key: KeyValueStoreKey) throws
}

View File

@@ -21,27 +21,33 @@ import KeychainAccess
/// Only supports `String` and `Data` values for now.
class KeychainStore: KeyValueStore {
private let keychain = Keychain()
private var keychain: Keychain
func setObject(forKey key: KeyValueStoreKey, value: Any?) {
/// Initializer
/// - Parameter keychain: Keychain instance to be used to read/write
init(withKeychain keychain: Keychain) {
self.keychain = keychain
}
func set(_ value: Any?, forKey key: KeyValueStoreKey) throws {
if value == nil {
removeObject(forKey: key)
try removeObject(forKey: key)
return
}
if let value = value as? String {
try? keychain.set(value, key: key)
try keychain.set(value, key: key)
} else if let value = value as? Data {
try? keychain.set(value, key: key)
try keychain.set(value, key: key)
}
}
func getObject(forKey key: KeyValueStoreKey) -> Any? {
return try? keychain.get(key)
func object(forKey key: KeyValueStoreKey) throws -> Any? {
return try keychain.get(key)
}
func removeObject(forKey key: KeyValueStoreKey) {
try? keychain.remove(key)
func removeObject(forKey key: KeyValueStoreKey) throws {
try keychain.remove(key)
}
}

View File

@@ -21,7 +21,7 @@ class MemoryStore: KeyValueStore {
private var map: Dictionary<KeyValueStoreKey, Any> = [:]
func setObject(forKey key: KeyValueStoreKey, value: Any?) {
func set(_ value: Any?, forKey key: KeyValueStoreKey) {
if let value = value {
map[key] = value
} else {
@@ -29,7 +29,7 @@ class MemoryStore: KeyValueStore {
}
}
func getObject(forKey key: KeyValueStoreKey) -> Any? {
func object(forKey key: KeyValueStoreKey) -> Any? {
return map[key]
}