123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- //
- // SwiftyUserDefaults
- //
- // Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz
- //
- // 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 DefaultsBridge {
- associatedtype T
- /// This method provides a way of saving your data in UserDefaults. Usually needed
- /// when you want to create your custom Bridge, so you'll have to override it.
- func get(key: String, userDefaults: UserDefaults) -> T?
- /// This method provides a way of saving your data in UserDefaults. Usually needed
- /// when you want to create your custom Bridge, so you'll have to override it.
- func save(key: String, value: T?, userDefaults: UserDefaults)
- /// Override this function if your data is represented differently in UserDefaults
- /// and you map it in save/get methods.
- ///
- /// For instance, if you store it as Data in UserDefaults, but your type is not Data in your
- /// defaults key, then you need to provide `deserialize(_:)` method as well.
- ///
- /// Similar if you store your array of type as e.g. `[String]` but the type you use is actually `[SomeClassThatHasOnlyOneStringProperty]`.
- ///
- /// See `DefaultsRawRepresentableBridge` or `DefaultsCodableBridge` for examples.
- func deserialize(_ object: Any) -> T?
- }
- public struct DefaultsObjectBridge<T>: DefaultsBridge {
- public init() {}
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- return userDefaults.object(forKey: key) as? T
- }
- public func deserialize(_ object: Any) -> T? {
- return nil
- }
- }
- public struct DefaultsArrayBridge<T: Collection>: DefaultsBridge {
- public init() {}
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- return userDefaults.array(forKey: key) as? T
- }
- public func deserialize(_ object: Any) -> T? {
- return nil
- }
- }
- public struct DefaultsStringBridge: DefaultsBridge {
- public init() {}
- public func save(key: String, value: String?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> String? {
- return userDefaults.string(forKey: key)
- }
- public func deserialize(_ object: Any) -> String? {
- return nil
- }
- }
- public struct DefaultsIntBridge: DefaultsBridge {
- public init() {}
- public func save(key: String, value: Int?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> Int? {
- if let int = userDefaults.number(forKey: key)?.intValue {
- return int
- }
- // Fallback for launch arguments
- if let string = userDefaults.object(forKey: key) as? String,
- let int = Int(string) {
- return int
- }
- return nil
- }
- public func deserialize(_ object: Any) -> Int? {
- return nil
- }
- }
- public struct DefaultsDoubleBridge: DefaultsBridge {
- public init() {}
- public func save(key: String, value: Double?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> Double? {
- if let double = userDefaults.number(forKey: key)?.doubleValue {
- return double
- }
- // Fallback for launch arguments
- if let string = userDefaults.object(forKey: key) as? String,
- let double = Double(string) {
- return double
- }
- return nil
- }
- public func deserialize(_ object: Any) -> Double? {
- return nil
- }
- }
- public struct DefaultsBoolBridge: DefaultsBridge {
- public init() {}
- public func save(key: String, value: Bool?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> Bool? {
- // @warning we use number(forKey:) instead of bool(forKey:), because
- // bool(forKey:) will always return a value, even if it's not set
- //
- // Now, let's see if there is value in defaults that converts to Bool first:
- if let bool = userDefaults.number(forKey: key)?.boolValue {
- return bool
- }
- // If not, fallback for values saved in a plist (e.g. for testing)
- // For instance, few of the string("YES", "true", "NO", "false") convert to Bool from a property list
- return (userDefaults.object(forKey: key) as? String)?.bool
- }
- public func deserialize(_ object: Any) -> Bool? {
- return nil
- }
- }
- public struct DefaultsDataBridge: DefaultsBridge {
- public init() {}
- public func save(key: String, value: Data?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> Data? {
- return userDefaults.data(forKey: key)
- }
- public func deserialize(_ object: Any) -> Data? {
- return nil
- }
- }
- public struct DefaultsUrlBridge: DefaultsBridge {
- public init() {}
- public func save(key: String, value: URL?, userDefaults: UserDefaults) {
- userDefaults.set(value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> URL? {
- return userDefaults.url(forKey: key)
- }
- public func deserialize(_ object: Any) -> URL? {
- if let object = object as? URL {
- return object
- }
- if let object = object as? Data {
- return NSKeyedUnarchiver.unarchiveObject(with: object) as? URL
- }
- if let object = object as? NSString {
- let path = object.expandingTildeInPath
- return URL(fileURLWithPath: path)
- }
- return nil
- }
- }
- public struct DefaultsCodableBridge<T: Codable>: DefaultsBridge {
- public init() {}
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- guard let value = value else {
- userDefaults.removeObject(forKey: key)
- return
- }
- userDefaults.set(encodable: value, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- guard let data = userDefaults.data(forKey: key) else {
- return nil
- }
- return deserialize(data)
- }
- public func deserialize(_ object: Any) -> T? {
- guard let data = object as? Data else { return nil }
- return try? JSONDecoder().decode(T.self, from: data)
- }
- }
- public struct DefaultsKeyedArchiverBridge<T>: DefaultsBridge {
- public init() {}
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- guard let value = value else {
- userDefaults.removeObject(forKey: key)
- return
- }
- // Needed because Quick/Nimble have min target 10.10...
- if #available(OSX 10.11, *) {
- userDefaults.set(NSKeyedArchiver.archivedData(withRootObject: value), forKey: key)
- } else {
- fatalError("Shouldn't really happen. We do not support macOS 10.10, if it happened to you please report your use-case on GitHub issues.")
- }
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- guard let data = userDefaults.data(forKey: key) else {
- return nil
- }
- return deserialize(data)
- }
- public func deserialize(_ object: Any) -> T? {
- guard let data = object as? Data else { return nil }
- return NSKeyedUnarchiver.unarchiveObject(with: data) as? T
- }
- }
- public struct DefaultsRawRepresentableBridge<T: RawRepresentable>: DefaultsBridge {
- public init() {}
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- userDefaults.set(value?.rawValue, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- guard let object = userDefaults.object(forKey: key) else { return nil }
- return deserialize(object)
- }
- public func deserialize(_ object: Any) -> T? {
- guard let rawValue = object as? T.RawValue else { return nil }
- return T(rawValue: rawValue)
- }
- }
- public struct DefaultsRawRepresentableArrayBridge<T: Collection>: DefaultsBridge where T.Element: RawRepresentable {
- public init() {}
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- let raw = value?.map { $0.rawValue }
- userDefaults.set(raw, forKey: key)
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- guard let object = userDefaults.array(forKey: key) else { return nil }
- return deserialize(object)
- }
- public func deserialize(_ object: Any) -> T? {
- guard let rawValue = object as? [T.Element.RawValue] else { return nil }
- return rawValue.compactMap { T.Element(rawValue: $0) } as? T
- }
- }
- public struct DefaultsOptionalBridge<Bridge: DefaultsBridge>: DefaultsBridge {
- public typealias T = Bridge.T?
- private let bridge: Bridge
- init(bridge: Bridge) {
- self.bridge = bridge
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- return bridge.get(key: key, userDefaults: userDefaults)
- }
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- bridge.save(key: key, value: value as? Bridge.T, userDefaults: userDefaults)
- }
- public func deserialize(_ object: Any) -> T? {
- return bridge.deserialize(object)
- }
- }
- public struct DefaultsOptionalArrayBridge<Bridge: DefaultsBridge>: DefaultsBridge where Bridge.T: Collection {
- public typealias T = Bridge.T?
- private let bridge: Bridge
- init(bridge: Bridge) {
- self.bridge = bridge
- }
- public func get(key: String, userDefaults: UserDefaults) -> T? {
- return bridge.get(key: key, userDefaults: userDefaults)
- }
- public func save(key: String, value: T?, userDefaults: UserDefaults) {
- bridge.save(key: key, value: value as? Bridge.T, userDefaults: userDefaults)
- }
- public func deserialize(_ object: Any) -> T? {
- return bridge.deserialize(object)
- }
- }
|