-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSceneStorage.swift
191 lines (160 loc) · 6.85 KB
/
SceneStorage.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
//
// SceneStorage.swift
// SceneStorage
//
// Created by Philipp on 29.08.21.
//
import SwiftUI
#if TARGET_IOS_MAJOR_13
/// A protocol for types compatible which can be read from and written to a dictionnary of type [AnyHashable: Any]
public protocol PropertyListTransform {
static func readValue(from store: [AnyHashable: Any], key: String, read: inout Bool) -> Any?
static func writeValue(_ value: Any?, to store: inout [AnyHashable: Any], key: String)
}
// A default implementation of PropertyListTransformable
extension PropertyListTransform {
public static func readValue(from store: [AnyHashable: Any], key: String, read: inout Bool) -> Any? {
read = store.index(forKey: key) != nil
return store[key]
}
public static func writeValue(_ value: Any?, to store: inout [AnyHashable: Any], key: String) {
store[key] = value
}
}
// The following types are known to be compatible with property lists
extension Bool: PropertyListTransform {}
extension Int: PropertyListTransform {}
extension Double: PropertyListTransform {}
extension String: PropertyListTransform {}
extension URL: PropertyListTransform {}
extension Data: PropertyListTransform {}
// For enums which are `RawRepresentable` use the `rawValue` for storing
private struct RawRepresentablePropertyListTransform<T>: PropertyListTransform
where T: RawRepresentable, T.RawValue: PropertyListTransform {
public static func readValue(from store: [AnyHashable: Any], key: String, read: inout Bool) -> Any? {
if let rawValue = store[key] as? T.RawValue {
return T(rawValue: rawValue)
}
return nil
}
public static func writeValue(_ value: Any?, to store: inout [AnyHashable: Any], key: String) {
if value != nil, let value = value as? T? {
store[key] = value?.rawValue
} else {
store[key] = nil
}
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
/// Helper class to get/set a value from a `[AnyHashable: Any]` dictionnary using the PropertyListTransform
private class AnyLocation<Value>: NSObject, ObservableObject {
private let key: String
private let transform: PropertyListTransform.Type
private let defaultValue: Value
func get(from store: [AnyHashable: Any]) -> Value {
var didRead = false
let value = transform.readValue(from: store, key: key, read: &didRead) as? Value
return value ?? defaultValue
}
func set(_ value: Value, to store: inout [AnyHashable: Any]) {
self.objectWillChange.send()
transform.writeValue(value, to: &store, key: key)
}
init(key: String, transform: PropertyListTransform.Type, defaultValue: Value) {
self.key = key
self.transform = transform
self.defaultValue = defaultValue
super.init()
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
/// A property wrapper type that represents a "named scene value" (managed by the current `SceneManager`)
@propertyWrapper
public struct SceneStorage<Value>: DynamicProperty {
@Environment(\.sceneStorageValues) private var store
@ObservedObject private var location: AnyLocation<Value>
public var wrappedValue: Value {
get {
location.get(from: store.values)
}
nonmutating set {
var values = store.values
location.set(newValue, to: &values)
store.values = values
}
}
public var projectedValue: Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { self.wrappedValue = $0 }
)
}
private init(key: String, transform: PropertyListTransform.Type, defaultValue: Value) {
location = AnyLocation(key: key, transform: transform, defaultValue: defaultValue)
}
public mutating func update() {
_store.update()
_location.update()
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
extension SceneStorage {
/// Creates a property that can read and write to a plain user default type.
public init(wrappedValue: Value, _ key: String) where Value: PropertyListTransform {
self.init(key: key, transform: Value.self, 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)
where Value: RawRepresentable, Value.RawValue: PropertyListTransform {
self.init(key: key, transform: RawRepresentablePropertyListTransform<Value>.self, defaultValue: wrappedValue)
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
extension SceneStorage where Value: ExpressibleByNilLiteral {
/// Creates a property that can read and write an optional scalar user default.
public init<WrappedType>(_ key: String) where Value == WrappedType?, WrappedType: PropertyListTransform {
self.init(key: key, transform: WrappedType.self, 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)
where Value == WrappedType?, WrappedType: RawRepresentable, WrappedType.RawValue: PropertyListTransform {
self.init(key: key, transform: RawRepresentablePropertyListTransform<WrappedType>.self, defaultValue: nil)
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
internal class SceneStorageValues {
var values: [AnyHashable: Any]
init(_ initialStore: [AnyHashable: Any] = [:]) {
values = initialStore
}
func value(forKey key: AnyHashable) -> Any? {
values[key]
}
func set(_ value: Any, forKey key: AnyHashable) {
values[key] = value
}
func removeObject(forKey key: AnyHashable) {
values.removeValue(forKey: key)
}
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
internal struct SceneStorageValuesKey: EnvironmentKey {
static var defaultValue: SceneStorageValues = SceneStorageValues()
}
@available(iOS, introduced: 13, obsoleted: 14.0,
message: "Backport not necessary as of iOS 14", renamed: "SwiftUI.SceneStorage")
extension EnvironmentValues {
internal var sceneStorageValues: SceneStorageValues {
get { self[SceneStorageValuesKey.self] }
set { self[SceneStorageValuesKey.self] = newValue }
}
}
#endif