diff --git a/Riot/Managers/KeyValueStorage/KeyValueStore.swift b/Riot/Managers/KeyValueStorage/KeyValueStore.swift index 7e046da6f..fdabe3de1 100644 --- a/Riot/Managers/KeyValueStorage/KeyValueStore.swift +++ b/Riot/Managers/KeyValueStorage/KeyValueStore.swift @@ -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 } diff --git a/Riot/Managers/KeyValueStorage/KeychainStore.swift b/Riot/Managers/KeyValueStorage/KeychainStore.swift index 97b8cffed..234828a8b 100644 --- a/Riot/Managers/KeyValueStorage/KeychainStore.swift +++ b/Riot/Managers/KeyValueStorage/KeychainStore.swift @@ -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) } diff --git a/Riot/Managers/KeyValueStorage/MemoryStore.swift b/Riot/Managers/KeyValueStorage/MemoryStore.swift index 7c8af8d85..564fd0be7 100644 --- a/Riot/Managers/KeyValueStorage/MemoryStore.swift +++ b/Riot/Managers/KeyValueStorage/MemoryStore.swift @@ -17,11 +17,11 @@ import Foundation @objcMembers -class MemoryStore: KeyValueStore { +class MemoryStore { private var map: Dictionary = [:] - 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) } diff --git a/Riot/Modules/SetPinCode/PinCodePreferences.swift b/Riot/Modules/SetPinCode/PinCodePreferences.swift index 78f86ff62..f04ea9fb7 100644 --- a/Riot/Modules/SetPinCode/PinCodePreferences.swift +++ b/Riot/Modules/SetPinCode/PinCodePreferences.swift @@ -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)") }