Change KeyValueStore api

This commit is contained in:
ismailgulek
2020-07-28 18:21:39 +03:00
parent 2d671580f7
commit baa1e3737e
4 changed files with 90 additions and 31 deletions

View File

@@ -19,7 +19,16 @@ import Foundation
typealias KeyValueStoreKey = String
protocol KeyValueStore {
func set(_ value: Any?, forKey key: KeyValueStoreKey) throws
func object(forKey key: KeyValueStoreKey) throws -> Any?
// setters
func setData(_ value: Data?, forKey key: KeyValueStoreKey) throws
func setString(_ value: String?, forKey key: KeyValueStoreKey) throws
func setBool(_ value: Bool?, forKey key: KeyValueStoreKey) throws
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data?
func string(forKey key: KeyValueStoreKey) throws -> String?
func bool(forKey key: KeyValueStoreKey) throws -> Bool?
// remove
func removeObject(forKey key: KeyValueStoreKey) throws
}

View File

@@ -35,8 +35,7 @@ extension Keychain {
}
@objcMembers
/// Only supports `String`, `Data` and `Bool` values for now.
class KeychainStore: KeyValueStore {
class KeychainStore {
private var keychain: Keychain
@@ -46,32 +45,52 @@ class KeychainStore: KeyValueStore {
self.keychain = keychain
}
func set(_ value: Any?, forKey key: KeyValueStoreKey) throws {
if value == nil {
}
extension KeychainStore: KeyValueStore {
// setters
func setData(_ value: Data?, forKey key: KeyValueStoreKey) throws {
guard let value = value else {
try removeObject(forKey: key)
return
}
if let value = value as? Data {
try keychain.set(value, key: key)
} else if let value = value as? String {
try keychain.set(value, key: key)
} else if let value = value as? Bool {
try keychain.set(value, key: key)
}
}
func object(forKey key: KeyValueStoreKey) throws -> Any? {
if let value = try keychain.getBool(key) {
return value
} else if let value = try keychain.getString(key) {
return value
} else if let value = try keychain.getData(key) {
return value
}
return try keychain.get(key)
try keychain.set(value, key: key)
}
func setString(_ value: String?, forKey key: KeyValueStoreKey) throws {
guard let value = value else {
try removeObject(forKey: key)
return
}
try keychain.set(value, key: key)
}
func setBool(_ value: Bool?, forKey key: KeyValueStoreKey) throws {
guard let value = value else {
try removeObject(forKey: key)
return
}
try keychain.set(value, key: key)
}
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data? {
return try keychain.getData(key)
}
func string(forKey key: KeyValueStoreKey) throws -> String? {
return try keychain.getString(key)
}
func bool(forKey key: KeyValueStoreKey) throws -> Bool? {
return try keychain.getBool(key)
}
// remove
func removeObject(forKey key: KeyValueStoreKey) throws {
try keychain.remove(key)
}

View File

@@ -17,11 +17,11 @@
import Foundation
@objcMembers
class MemoryStore: KeyValueStore {
class MemoryStore {
private var map: Dictionary<KeyValueStoreKey, Any> = [:]
func set(_ value: Any?, forKey key: KeyValueStoreKey) {
private func set(_ value: Any?, forKey key: KeyValueStoreKey) {
if let value = value {
map[key] = value
} else {
@@ -29,10 +29,41 @@ class MemoryStore: KeyValueStore {
}
}
func object(forKey key: KeyValueStoreKey) -> Any? {
private func object(forKey key: KeyValueStoreKey) -> Any? {
return map[key]
}
}
extension MemoryStore: KeyValueStore {
// setters
func setData(_ value: Data?, forKey key: KeyValueStoreKey) throws {
set(value, forKey: key)
}
func setString(_ value: String?, forKey key: KeyValueStoreKey) throws {
set(value, forKey: key)
}
func setBool(_ value: Bool?, forKey key: KeyValueStoreKey) throws {
set(value, forKey: key)
}
// getters
func data(forKey key: KeyValueStoreKey) throws -> Data? {
return object(forKey: key) as? Data
}
func string(forKey key: KeyValueStoreKey) throws -> String? {
return object(forKey: key) as? String
}
func bool(forKey key: KeyValueStoreKey) throws -> Bool? {
return object(forKey: key) as? Bool
}
// remove
func removeObject(forKey key: KeyValueStoreKey) {
map.removeValue(forKey: key)
}

View File

@@ -73,14 +73,14 @@ final class PinCodePreferences: NSObject {
var pin: String? {
get {
do {
return try store.object(forKey: StoreKeys.pin) as? String
return try store.string(forKey: StoreKeys.pin)
} catch let error {
NSLog("[PinCodePreferences] Error when reading user pin from store: \(error)")
return nil
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.pin)
try store.setString(newValue, forKey: StoreKeys.pin)
} catch let error {
NSLog("[PinCodePreferences] Error when storing user pin to the store: \(error)")
}
@@ -90,14 +90,14 @@ final class PinCodePreferences: NSObject {
var biometricsEnabled: Bool? {
get {
do {
return try store.object(forKey: StoreKeys.biometricsEnabled) as? Bool
return try store.bool(forKey: StoreKeys.biometricsEnabled)
} catch let error {
NSLog("[PinCodePreferences] Error when reading biometrics enabled from store: \(error)")
return nil
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.biometricsEnabled)
try store.setBool(newValue, forKey: StoreKeys.biometricsEnabled)
} catch let error {
NSLog("[PinCodePreferences] Error when storing biometrics enabled to the store: \(error)")
}