|
| 1 | +import Foundation |
| 2 | + |
| 3 | +#if canImport(SwiftUI) |
| 4 | + |
| 5 | +import SwiftUI |
| 6 | + |
| 7 | +public extension AppStorage { |
| 8 | + init<K: UserDefaultPreferenceKey>( |
| 9 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 10 | + ) where K.Value == Value, Value == Bool { |
| 11 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 12 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 13 | + } |
| 14 | + |
| 15 | + init<K: UserDefaultPreferenceKey>( |
| 16 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 17 | + ) where K.Value == Value, Value == String { |
| 18 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 19 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 20 | + } |
| 21 | + |
| 22 | + init<K: UserDefaultPreferenceKey>( |
| 23 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 24 | + ) where K.Value == Value, Value == Double { |
| 25 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 26 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 27 | + } |
| 28 | + |
| 29 | + init<K: UserDefaultPreferenceKey>( |
| 30 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 31 | + ) where K.Value == Value, Value == Int { |
| 32 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 33 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 34 | + } |
| 35 | + |
| 36 | + init<K: UserDefaultPreferenceKey>( |
| 37 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 38 | + ) where K.Value == Value, Value == URL { |
| 39 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 40 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 41 | + } |
| 42 | + |
| 43 | + init<K: UserDefaultPreferenceKey>( |
| 44 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 45 | + ) where K.Value == Value, Value == Data { |
| 46 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 47 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 48 | + } |
| 49 | + |
| 50 | + init<K: UserDefaultPreferenceKey>( |
| 51 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 52 | + ) where K.Value == Value, Value: RawRepresentable, Value.RawValue == Int { |
| 53 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 54 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 55 | + } |
| 56 | + |
| 57 | + init<K: UserDefaultPreferenceKey>( |
| 58 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 59 | + ) where K.Value == Value, Value: RawRepresentable, Value.RawValue == String { |
| 60 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 61 | + self.init(wrappedValue: key.defaultValue, key.key, store: .shared) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +public extension AppStorage where Value: ExpressibleByNilLiteral { |
| 66 | + init<K: UserDefaultPreferenceKey>( |
| 67 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 68 | + ) where K.Value == Value, Value == Bool? { |
| 69 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 70 | + self.init(key.key, store: .shared) |
| 71 | + } |
| 72 | + |
| 73 | + init<K: UserDefaultPreferenceKey>( |
| 74 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 75 | + ) where K.Value == Value, Value == String? { |
| 76 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 77 | + self.init(key.key, store: .shared) |
| 78 | + } |
| 79 | + |
| 80 | + init<K: UserDefaultPreferenceKey>( |
| 81 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 82 | + ) where K.Value == Value, Value == Double? { |
| 83 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 84 | + self.init(key.key, store: .shared) |
| 85 | + } |
| 86 | + |
| 87 | + init<K: UserDefaultPreferenceKey>( |
| 88 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 89 | + ) where K.Value == Value, Value == Int? { |
| 90 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 91 | + self.init(key.key, store: .shared) |
| 92 | + } |
| 93 | + |
| 94 | + init<K: UserDefaultPreferenceKey>( |
| 95 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 96 | + ) where K.Value == Value, Value == URL? { |
| 97 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 98 | + self.init(key.key, store: .shared) |
| 99 | + } |
| 100 | + |
| 101 | + init<K: UserDefaultPreferenceKey>( |
| 102 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 103 | + ) where K.Value == Value, Value == Data? { |
| 104 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 105 | + self.init(key.key, store: .shared) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +public extension AppStorage { |
| 110 | + init<K: UserDefaultPreferenceKey, R>( |
| 111 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 112 | + ) where K.Value == Value, Value == R?, R : RawRepresentable, R.RawValue == String { |
| 113 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 114 | + self.init(key.key, store: .shared) |
| 115 | + } |
| 116 | + |
| 117 | + init<K: UserDefaultPreferenceKey, R>( |
| 118 | + _ keyPath: KeyPath<UserDefaultPreferenceKeys, K> |
| 119 | + ) where K.Value == Value, Value == R?, R : RawRepresentable, R.RawValue == Int { |
| 120 | + let key = UserDefaultPreferenceKeys()[keyPath: keyPath] |
| 121 | + self.init(key.key, store: .shared) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#endif |
0 commit comments