Skip to content

Commit 96c8da6

Browse files
authored
Renamed StateProperty to ObservableProperty and make it public (#241)
* Renamed StateProperty to ObservableProperty and made the protocol public * Implemented requested changes moved ObservableProperty to separate file, updated documentation comment * Attempt to fix CI run failing due to as? ObservableProperty instead of as? any ObservableProperty
1 parent 25bbff4 commit 96c8da6

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
/// View properties that conform to ObservableProperty are automatically observed by SwiftCrossUI.
4+
///
5+
/// This protocol is intended to be implemented by property wrappers. You shouldn't
6+
/// have to implement it for your own model types.
7+
public protocol ObservableProperty: DynamicProperty {
8+
var didChange: Publisher { get }
9+
func tryRestoreFromSnapshot(_ snapshot: Data)
10+
func snapshot() throws -> Data?
11+
}

Sources/SwiftCrossUI/State/State.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Foundation
55
// - It supports ObservableObject
66
// - It supports Optional<ObservableObject>
77
@propertyWrapper
8-
public struct State<Value>: DynamicProperty, StateProperty {
8+
public struct State<Value>: ObservableProperty {
99
class Storage {
1010
// This inner box is what stays constant between view updates. The
1111
// outer box (Storage) is used so that we can assign this box to
@@ -55,7 +55,7 @@ public struct State<Value>: DynamicProperty, StateProperty {
5555

5656
var storage: Storage
5757

58-
var didChange: Publisher {
58+
public var didChange: Publisher {
5959
storage.box.didChange
6060
}
6161

@@ -109,7 +109,7 @@ public struct State<Value>: DynamicProperty, StateProperty {
109109
}
110110
}
111111

112-
func tryRestoreFromSnapshot(_ snapshot: Data) {
112+
public func tryRestoreFromSnapshot(_ snapshot: Data) {
113113
guard
114114
let decodable = Value.self as? Codable.Type,
115115
let state = try? JSONDecoder().decode(decodable, from: snapshot)
@@ -120,17 +120,11 @@ public struct State<Value>: DynamicProperty, StateProperty {
120120
storage.box.value = state as! Value
121121
}
122122

123-
func snapshot() throws -> Data? {
123+
public func snapshot() throws -> Data? {
124124
if let value = storage.box as? Codable {
125125
return try JSONEncoder().encode(value)
126126
} else {
127127
return nil
128128
}
129129
}
130130
}
131-
132-
protocol StateProperty {
133-
var didChange: Publisher { get }
134-
func tryRestoreFromSnapshot(_ snapshot: Data)
135-
func snapshot() throws -> Data?
136-
}

Sources/SwiftCrossUI/ViewGraph/ViewGraphNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class ViewGraphNode<NodeView: View, Backend: AppBackend>: Sendable {
121121
)
122122
}
123123

124-
guard let value = property.value as? StateProperty else {
124+
guard let value = property.value as? any ObservableProperty else {
125125
continue
126126
}
127127

Sources/SwiftCrossUI/ViewGraph/ViewGraphSnapshotter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct ViewGraphSnapshotter: ErasedViewGraphNodeTransformer {
5555
let mirror = Mirror(reflecting: view)
5656
for property in mirror.children {
5757
guard
58-
let stateProperty = property as? StateProperty,
58+
let stateProperty = property as? any ObservableProperty,
5959
let propertyName = property.label,
6060
let encodedState = state[propertyName]
6161
else {
@@ -80,7 +80,7 @@ public struct ViewGraphSnapshotter: ErasedViewGraphNodeTransformer {
8080
for property in mirror.children {
8181
guard
8282
let propertyName = property.label,
83-
let property = property as? StateProperty,
83+
let property = property as? any ObservableProperty,
8484
let encodedState = try? property.snapshot()
8585
else {
8686
continue

Sources/SwiftCrossUI/_App.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class _App<AppRoot: App> {
6666
)
6767
}
6868

69-
guard let value = property.value as? StateProperty else {
69+
guard let value = property.value as? any ObservableProperty else {
7070
continue
7171
}
7272

0 commit comments

Comments
 (0)