Last active
February 1, 2023 13:01
-
-
Save jandamm/68ffe86a972c764677389a6630d83c96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// MARK: - Library | |
public extension UserDefaults { | |
struct Key<Value> { | |
let key: String | |
public init(key: String) { | |
self.key = key | |
} | |
public init(key: String, value _: Value.Type) { | |
self.init(key: key) | |
} | |
} | |
struct DefaultValueKey<Value> { | |
let key: String | |
let defaultValue: () -> Value | |
public init(key: String, defaultValue: @escaping @autoclosure () -> Value) { | |
self.key = key | |
self.defaultValue = defaultValue | |
} | |
} | |
} | |
#if canImport(SwiftUI) | |
import SwiftUI | |
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | |
public extension AppStorage where Value == String { | |
init(wrappedValue: Value, _ key: UserDefaults.Key<Value>) { | |
self.init(wrappedValue: wrappedValue, key.key) | |
} | |
init(_ key: UserDefaults.DefaultValueKey<Value>) { | |
self.init(wrappedValue: key.defaultValue(), key.key) | |
} | |
} | |
#endif | |
public extension UserDefaults { | |
func removeObject<Value>(for key: Key<Value>) { | |
removeObject(forKey: key.key) | |
} | |
func removeObject<Value>(for key: DefaultValueKey<Value>) { | |
removeObject(forKey: key.key) | |
} | |
func setValue<Value>(_ value: Value, for key: Key<Value>) { | |
setValue(value, forKey: key.key) | |
} | |
func setValue<Value>(_ value: Value, for key: DefaultValueKey<Value>) { | |
setValue(value, forKey: key.key) | |
} | |
func string(for key: Key<String>) -> String? { | |
string(forKey: key.key) | |
} | |
func string(for key: DefaultValueKey<String>) -> String { | |
register(defaults: [key.key: key.defaultValue()]) | |
return string(forKey: key.key) ?? key.defaultValue() | |
} | |
} | |
// MARK: - Example of App Code | |
extension UserDefaults.Key where Value == String { | |
static let username = Self(key: "username") | |
} | |
extension UserDefaults.DefaultValueKey where Value == String { | |
static let autoPlay = Self(key: "autoPlay", defaultValue: "Hiho") | |
} | |
UserDefaults.standard.string(for: .autoPlay) | |
struct V: View { | |
@AppStorage(.autoPlay) var autoPlay | |
var body: some View { Text("") } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment