feat: Fix TIFoundationUtils public error

This commit is contained in:
Boyko Mihail 2021-04-28 17:58:55 +03:00
parent 38df9a8db7
commit 5146a75c22
16 changed files with 2 additions and 591 deletions

View File

@ -20,7 +20,7 @@
// THE SOFTWARE.
//
enum StorageError: Error {
public enum StorageError: Error {
case valueNotFound
case unableToExtractData(underlyingError: Error)
case unableToDecode(underlyingError: Error)

View File

@ -1,49 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import TISwiftUtils
public typealias CodableKeyValueBackingStore<S: CodableKeyValueStorage, T: Codable> = BackingStore<S, T>
public extension BackingStore where Store: CodableKeyValueStorage, StoreContent: Codable {
init<Value: Codable>(key: StorageKey<Value>,
codableKeyValueStorage: Store,
decoder: CodableKeyValueDecoder = JSONKeyValueDecoder(),
encoder: CodableKeyValueEncoder = JSONKeyValueEncoder())
where StoreContent == Value? {
self.init(store: codableKeyValueStorage,
getClosure: { try? $0.codableObject(forKey: key, decoder: decoder) },
setClosure: { try? $0.setOrRemove(codableObject: $1, forKey: key, encoder: encoder) })
}
init(wrappedValue: StoreContent,
key: StorageKey<StoreContent>,
codableKeyValueStorage: Store,
decoder: CodableKeyValueDecoder = JSONKeyValueDecoder(),
encoder: CodableKeyValueEncoder = JSONKeyValueEncoder()) {
self.init(store: codableKeyValueStorage,
getClosure: { $0.codableObject(forKey: key, defaultValue: wrappedValue, decoder: decoder) },
setClosure: { try? $0.setOrRemove(codableObject: $1, forKey: key, encoder: encoder) })
}
}

View File

@ -1,91 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
public protocol CodableKeyValueStorage {
/// Returns the object with specified type associated with the first occurrence of the specified key.
/// - Parameters:
/// - key: A key in the storage.
/// - decoder: CodableKeyValueDecoder to decode stored data.
/// - Returns: The object with specified type associated with the specified key,
/// or throw exception if the key was not found.
/// - Throws: CodableStorageError
func codableObject<Value: Decodable>(forKey key: StorageKey<Value>,
decoder: CodableKeyValueDecoder) throws -> Value
/// Set or remove the value of the specified key in the storage.
/// - Parameters:
/// - object: The object with specified type to store.
/// - key: The key with which to associate with the value.
/// - encoder: CodableKeyValueEncoder to encode to encode passed object.
/// - Throws: EncodingError if error is occured during passed object encoding.
func set<Value: Encodable>(encodableObject: Value,
forKey key: StorageKey<Value>,
encoder: CodableKeyValueEncoder) throws
/// Removes value for specific key
/// - Parameter key: The key with which to associate with the value.
/// - Throws: EncodingError if error is occured during reading/writing.
func removeCodableValue<Value: Codable>(forKey key: StorageKey<Value>) throws
}
public extension CodableKeyValueStorage {
/// Returns the object with specified type associated with the first occurrence of the specified key.
/// - Parameters:
/// - key: A key in the storage.
/// - defaultValue: A default value that will be used if there is no such value for specified key,
/// - decoder: CodableKeyValueDecoder to decode stored data.
/// or if error occurred during decoding
/// - Returns: The object with specified type associated with the specified key, or passed default value
/// if there is no such value for specified key or if error occurred during mapping.
func codableObject<Value: Decodable>(forKey key: StorageKey<Value>,
defaultValue: Value,
decoder: CodableKeyValueDecoder = JSONKeyValueDecoder()) -> Value {
(try? codableObject(forKey: key, decoder: decoder)) ?? defaultValue
}
func setOrRemove<Value: Codable>(codableObject: Value?,
forKey key: StorageKey<Value>,
encoder: CodableKeyValueEncoder = JSONKeyValueEncoder()) throws {
if let codableObject = codableObject {
try set(encodableObject: codableObject, forKey: key, encoder: encoder)
} else {
try? removeCodableValue(forKey: key)
}
}
subscript<Value: Codable>(key: StorageKey<Value>,
decoder: CodableKeyValueDecoder = JSONKeyValueDecoder(),
encoder: CodableKeyValueEncoder = JSONKeyValueEncoder()) -> Value? {
get {
try? codableObject(forKey: key, decoder: decoder)
}
set {
try? setOrRemove(codableObject: newValue, forKey: key, encoder: encoder)
}
}
}

View File

@ -1,27 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
public protocol CodableKeyValueDecoder {
func decodeDecodable<Value: Decodable>(from data: Data, for key: StorageKey<Value>) throws -> Value
}

View File

@ -1,39 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
open class JSONKeyValueDecoder: CodableKeyValueDecoder {
private let jsonDecoder: JSONDecoder
public init(jsonDecoder: JSONDecoder = JSONDecoder()) {
self.jsonDecoder = jsonDecoder
}
open func decodeDecodable<Value: Decodable>(from data: Data, for key: StorageKey<Value>) throws -> Value {
do {
return try jsonDecoder.decode(Value.self, from: data)
} catch {
throw StorageError.unableToDecode(underlyingError: error)
}
}
}

View File

@ -1,47 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
open class UnarchiverKeyValueDecoder: CodableKeyValueDecoder {
public init() {}
open func decodeDecodable<Value: Decodable>(from data: Data, for key: StorageKey<Value>) throws -> Value {
let unarchiver: NSKeyedUnarchiver
do {
unarchiver = try NSKeyedUnarchiver(forReadingFrom: data)
} catch {
throw StorageError.unableToDecode(underlyingError: error)
}
defer {
unarchiver.finishDecoding()
}
guard let decodableObject = unarchiver.decodeDecodable(Value.self, forKey: key.rawValue) else {
throw StorageError.valueNotFound
}
return decodableObject
}
}

View File

@ -1,41 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
open class ArchiverKeyValueEncoder: CodableKeyValueEncoder {
public init() {}
open func encodeEncodable<Value: Encodable>(value: Value, for key: StorageKey<Value>) throws -> Data {
let archiver = NSKeyedArchiver(requiringSecureCoding: true)
do {
try archiver.encodeEncodable(value, forKey: key.rawValue)
} catch {
throw StorageError.unableToEncode(underlyingError: error)
}
archiver.finishEncoding()
return archiver.encodedData
}
}

View File

@ -1,27 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
public protocol CodableKeyValueEncoder {
func encodeEncodable<Value: Encodable>(value: Value, for key: StorageKey<Value>) throws -> Data
}

View File

@ -1,39 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
open class JSONKeyValueEncoder: CodableKeyValueEncoder {
private let jsonEncoder: JSONEncoder
public init(jsonEncoder: JSONEncoder = JSONEncoder()) {
self.jsonEncoder = jsonEncoder
}
open func encodeEncodable<Value: Encodable>(value: Value, for key: StorageKey<Value>) throws -> Data {
do {
return try jsonEncoder.encode(value)
} catch {
throw StorageError.unableToEncode(underlyingError: error)
}
}
}

View File

@ -1,29 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
enum StorageError: Error {
case valueNotFound
case unableToExtractData(underlyingError: Error)
case unableToDecode(underlyingError: Error)
case unableToEncode(underlyingError: Error)
case unableToWriteData(underlyingError: Error)
}

View File

@ -1,29 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
public struct StorageKey<ValueType>: RawRepresentable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}

View File

@ -1,52 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
extension UserDefaults: CodableKeyValueStorage {
public func codableObject<Value: Decodable>(forKey key: StorageKey<Value>,
decoder: CodableKeyValueDecoder) throws -> Value {
guard let storedData = data(forKey: key.rawValue) else {
throw StorageError.valueNotFound
}
return try decoder.decodeDecodable(from: storedData, for: key)
}
public func set<Value: Encodable>(encodableObject: Value,
forKey key: StorageKey<Value>,
encoder: CodableKeyValueEncoder) throws {
let encodedData = try encoder.encodeEncodable(value: encodableObject, for: key)
set(encodedData, forKey: key.rawValue)
}
public func removeCodableValue<Value: Codable>(forKey key: StorageKey<Value>) throws {
guard data(forKey: key.rawValue) != nil else {
throw StorageError.valueNotFound
}
removeObject(forKey: key.rawValue)
}
}

View File

@ -1,25 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
public typealias UserDefaultsCodableBackingStore<T: Codable> = CodableKeyValueBackingStore<UserDefaults, T>

View File

@ -1,26 +0,0 @@
//
// Copyright (c) 2020 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import KeychainAccess
import TIFoundationUtils
typealias KeychainCodableBackingStore<T: Codable> = CodableKeyValueBackingStore<Keychain, T>

View File

@ -1,69 +0,0 @@
//
// Copyright (c) 2021 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import TISwiftUtils
import Foundation
public typealias UserDefaultsBackingStore<T> = BackingStore<UserDefaults, T>
public extension BackingStore where Store: UserDefaults {
typealias GetValueByRawKeyClosure = (Store, String) -> StoreContent
typealias SetValueByRawKeyClosure = (Store, StoreContent, String) -> Void
init<Value>(key: StorageKey<Value>,
userDefaultsStorage: Store,
getClosure: @escaping GetValueByRawKeyClosure,
setClosure: @escaping SetValueByRawKeyClosure)
where StoreContent == Value? {
self.init(store: userDefaultsStorage,
getClosure: {
guard $0.object(forKey: key.rawValue) != nil else {
return nil
}
return getClosure($0, key.rawValue)
},
setClosure: {
setClosure($0, $1, key.rawValue)
})
}
init(wrappedValue: StoreContent,
key: StorageKey<StoreContent>,
userDefaultsStorage: Store,
getClosure: @escaping GetValueByRawKeyClosure,
setClosure: @escaping SetValueByRawKeyClosure) {
self.init(store: userDefaultsStorage,
getClosure: {
guard $0.object(forKey: key.rawValue) != nil else {
return wrappedValue
}
return getClosure($0, key.rawValue)
},
setClosure: {
setClosure($0, $1, key.rawValue)
})
}
}

View File

@ -13,5 +13,6 @@ Pod::Spec.new do |s|
s.source_files = s.name + '/Sources/**/*'
s.dependency 'TISwiftUtils', s.version.to_s
s.dependency 'TIFoundationUtils', s.version.to_s
s.framework = 'Foundation'
end