-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppStorage.swift
220 lines (185 loc) · 7.83 KB
/
AppStorage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// AppStorage.swift
// AppStorage
//
// Created by Philipp on 26.08.21.
//
import SwiftUI
import Combine
import os.log
#if TARGET_IOS_MAJOR_13
/// A protocol for types compatible which can be read from and written to UserDefaults
public protocol UserDefaultsValueTransform {
static func readValue(from store: UserDefaults, key: String) -> Any?
static func writeValue(_ value: Any?, to store: UserDefaults, key: String)
}
// Default implementation protocol
extension UserDefaultsValueTransform {
public static func readValue(from store: UserDefaults, key: String) -> Any? {
store.value(forKey: key)
}
public static func writeValue(_ value: Any?, to store: UserDefaults, key: String) {
store.set(value as? Self, forKey: key)
}
}
// The following types are known to be compatible with UserDefaults
extension Bool: UserDefaultsValueTransform {}
extension Int: UserDefaultsValueTransform {}
extension Double: UserDefaultsValueTransform {}
extension String: UserDefaultsValueTransform {}
extension URL: UserDefaultsValueTransform {
public static func readValue(from store: UserDefaults, key: String) -> Any? {
store.url(forKey: key)
}
public static func writeValue(_ value: Any?, to store: UserDefaults, key: String) {
store.set(value as? Self, forKey: key)
}
}
extension Data: UserDefaultsValueTransform {}
// For enums which are `RawRepresentable` use the `rawValue` for storing in `UserDefaults`
struct RawRepresentableTransform<T>: UserDefaultsValueTransform
where T: RawRepresentable, T.RawValue: UserDefaultsValueTransform {
public static func readValue(from store: UserDefaults, key: String) -> Any? {
if let rawValue = store.value(forKey: key) as? T.RawValue {
return T(rawValue: rawValue)
}
return nil
}
public static func writeValue(_ value: Any?, to store: UserDefaults, key: String) {
guard let value = value as? T? else { return }
store.setValue(value?.rawValue, forKey: key)
}
}
/// Helper class to encapsulate the management of a single value: read/write, caching, UserDefaults KVO
private class UserDefaultLocation<Value>: NSObject, ObservableObject {
private let key: String
private let transform: UserDefaultsValueTransform.Type
private let defaultValue: Value
private var cachedValue: Value?
var store: UserDefaults {
didSet {
if oldValue != store {
oldValue.removeObserver(self, forKeyPath: key)
store.addObserver(self, forKeyPath: key, options: [], context: nil)
}
}
}
func get() -> Value {
if let value = cachedValue {
return value
}
let value = transform.readValue(from: store, key: key) as? Value ?? defaultValue
cachedValue = value
return value
}
func set(_ newValue: Value) {
cachedValue = newValue
transform.writeValue(newValue, to: store, key: key)
}
init(key: String, transform: UserDefaultsValueTransform.Type, store: UserDefaults?, defaultValue: Value) {
self.key = key
self.transform = transform
self.store = store ?? .standard
self.defaultValue = defaultValue
super.init()
self.store.addObserver(self, forKeyPath: key, options: [], context: nil)
}
deinit {
store.removeObserver(self, forKeyPath: key)
}
// Called whenever the related UserDefaults value changed
// swiftlint:disable block_based_kvo
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey: Any]?,
context: UnsafeMutableRawPointer?) {
self.objectWillChange.send()
cachedValue = nil
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.AppStorage")
/// A property wrapper type that reflects a value from `UserDefaults` and
/// invalidates a view on a change in value in that user default.
@propertyWrapper
public struct AppStorage<Value>: DynamicProperty {
@Environment(\.defaultAppStorage) private var defaultStore
@ObservedObject private var location: UserDefaultLocation<Value>
public var wrappedValue: Value {
get {
location.get()
}
nonmutating set {
location.set(newValue)
}
}
public var projectedValue: Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { self.wrappedValue = $0 }
)
}
private init(key: String, transform: UserDefaultsValueTransform.Type,
store: UserDefaults?, defaultValue: Value) {
location = UserDefaultLocation(key: key, transform: transform, store: store, defaultValue: defaultValue)
}
public mutating func update() {
_location.update()
// Update storage with latest environment setting
location.store = defaultStore
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.AppStorage")
extension AppStorage {
/// Creates a property that can read and write to a plain user default type.
public init(wrappedValue: Value, _ key: String, store: UserDefaults? = nil)
where Value: UserDefaultsValueTransform {
self.init(key: key, transform: Value.self, store: store, defaultValue: wrappedValue)
}
/// Creates a property that can read and write a scalar user default, transforming
/// that from and to `RawRepresentable` data type.
public init(wrappedValue: Value, _ key: String, store: UserDefaults? = nil)
where Value: RawRepresentable, Value.RawValue: UserDefaultsValueTransform {
self.init(key: key, transform: RawRepresentableTransform<Value>.self, store: store, defaultValue: wrappedValue)
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.AppStorage")
extension AppStorage where Value: ExpressibleByNilLiteral {
/// Creates a property that can read and write an optional scalar user default.
public init<WrappedType>(_ key: String, store: UserDefaults? = nil)
where Value == WrappedType?, WrappedType: UserDefaultsValueTransform {
self.init(key: key, transform: WrappedType.self, store: store, defaultValue: nil)
}
/// Creates a property that can save and restore an optional value, transforming
/// it to an optional `RawRepresentable` data type.
public init<WrappedType>(_ key: String, store: UserDefaults? = nil)
where Value == WrappedType?, WrappedType: RawRepresentable,
WrappedType.RawValue: UserDefaultsValueTransform {
self.init(key: key, transform: RawRepresentableTransform<WrappedType>.self, store: store, defaultValue: nil)
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.AppStorage")
extension View {
/// The default store used by `AppStorage` contained within the view.
public func defaultAppStorage(_ store: UserDefaults) -> some View {
return self.environment(\.defaultAppStorage, store)
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.AppStorage")
struct DefaultAppStorage: EnvironmentKey {
static var defaultValue: UserDefaults = .standard
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.EnvironmentValues.AppStorage")
extension EnvironmentValues {
// swiftlint:disable private_over_fileprivate
fileprivate var defaultAppStorage: UserDefaults {
get { self[DefaultAppStorage.self] }
set { self[DefaultAppStorage.self] = newValue }
}
}
#endif