Created
November 6, 2023 08:17
-
-
Save MihailPreis/a8cae4da73de20f67dee7b7ca39445a2 to your computer and use it in GitHub Desktop.
UserDefault+AppStorage extensions
This file contains hidden or 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 | |
public struct AppStorageKeys { | |
public var userOnboarding: AppStorageKey<Bool> { .init("user_onboarding", defaultValue: false) } | |
public var updateFrequencyTimeInterval: AppStorageKey<Double> { .init("update_frequency_time_interval", defaultValue: 1.0) } | |
} | |
public struct AppStorageKey<Value> { | |
let name: String | |
let defaultValue: Value | |
init(_ name: String, defaultValue: Value) { | |
self.name = name | |
self.defaultValue = defaultValue | |
} | |
} | |
public extension UserDefaults { | |
func `get`<Value>(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>) -> Value { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
let result = self.object(forKey: strongKey.name) as? Value | |
return result ?? strongKey.defaultValue | |
} | |
func `set`<Value>(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, value: Value) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.set(value, forKey: strongKey.name) | |
} | |
} | |
import SwiftUI | |
public extension AppStorage where Value == Bool { | |
init(wrappedValue: Bool, strongKey: AppStorageKey<Value>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKey.name, store: store) | |
} | |
init(wrappedValue: Value, strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: wrappedValue, strongKey: strongKey, store: store) | |
} | |
init(wrappedValue: Value, _ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKeyPath: strongKeyPath, store: store) | |
} | |
init(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: strongKey.defaultValue, strongKey: strongKey, store: store) | |
} | |
} | |
public extension AppStorage where Value == Int { | |
init(wrappedValue: Int, strongKey: AppStorageKey<Value>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKey.name, store: store) | |
} | |
init(wrappedValue: Value, strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: wrappedValue, strongKey: strongKey, store: store) | |
} | |
init(wrappedValue: Value, _ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKeyPath: strongKeyPath, store: store) | |
} | |
init(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: strongKey.defaultValue, strongKey: strongKey, store: store) | |
} | |
} | |
public extension AppStorage where Value == Double { | |
init(wrappedValue: Double, strongKey: AppStorageKey<Value>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKey.name, store: store) | |
} | |
init(wrappedValue: Value, strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: wrappedValue, strongKey: strongKey, store: store) | |
} | |
init(wrappedValue: Value, _ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKeyPath: strongKeyPath, store: store) | |
} | |
init(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: strongKey.defaultValue, strongKey: strongKey, store: store) | |
} | |
} | |
public extension AppStorage where Value == URL { | |
init(wrappedValue: URL, strongKey: AppStorageKey<Value>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKey.name, store: store) | |
} | |
init(wrappedValue: Value, strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: wrappedValue, strongKey: strongKey, store: store) | |
} | |
init(wrappedValue: Value, _ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKeyPath: strongKeyPath, store: store) | |
} | |
init(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: strongKey.defaultValue, strongKey: strongKey, store: store) | |
} | |
} | |
public extension AppStorage where Value == String { | |
init(wrappedValue: String, strongKey: AppStorageKey<Value>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKey.name, store: store) | |
} | |
init(wrappedValue: Value, strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: wrappedValue, strongKey: strongKey, store: store) | |
} | |
init(wrappedValue: Value, _ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKeyPath: strongKeyPath, store: store) | |
} | |
init(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: strongKey.defaultValue, strongKey: strongKey, store: store) | |
} | |
} | |
public extension AppStorage where Value == Data { | |
init(wrappedValue: Data, strongKey: AppStorageKey<Value>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKey.name, store: store) | |
} | |
init(wrappedValue: Value, strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: wrappedValue, strongKey: strongKey, store: store) | |
} | |
init(wrappedValue: Value, _ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
self.init(wrappedValue: wrappedValue, strongKeyPath: strongKeyPath, store: store) | |
} | |
init(_ strongKeyPath: KeyPath<AppStorageKeys, AppStorageKey<Value>>, store: UserDefaults? = nil) { | |
let strongKey = AppStorageKeys()[keyPath: strongKeyPath] | |
self.init(wrappedValue: strongKey.defaultValue, strongKey: strongKey, store: store) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment