mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-17 23:18:27 +02:00
Some refactoring after Steves comments
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user