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