Skip to content

Commit df627ba

Browse files
committed
Merge branch 'publicStatePropertyAndRenameToObservableProperty' into usemylatestchanges
2 parents 5017c28 + 9f3b473 commit df627ba

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

Sources/SwiftCrossUI/State/State.swift

Lines changed: 8 additions & 5 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>: DynamicProperty, 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,7 +120,7 @@ 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 {
@@ -129,7 +129,10 @@ public struct State<Value>: DynamicProperty, StateProperty {
129129
}
130130
}
131131

132-
protocol StateProperty {
132+
/// Declaring conformance to ObservableProperty is required for
133+
/// SwiftCrossUI to automatically register an observer on the
134+
/// conforming object's Publisher.
135+
public protocol ObservableProperty {
133136
var didChange: Publisher { get }
134137
func tryRestoreFromSnapshot(_ snapshot: Data)
135138
func snapshot() throws -> Data?

Sources/SwiftCrossUI/ViewGraph/ViewGraphNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class ViewGraphNode<NodeView: View, Backend: AppBackend>: Sendable {
116116
)
117117
}
118118

119-
guard let value = property.value as? StateProperty else {
119+
guard let value = property.value as? ObservableProperty else {
120120
continue
121121
}
122122

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? 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? ObservableProperty,
8484
let encodedState = try? property.snapshot()
8585
else {
8686
continue

Sources/SwiftCrossUI/Views/Modifiers/Lifecycle/OnDisappearModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension View {
1010
}
1111
}
1212

13-
struct OnDisappearModifier<Content: View>: View {
13+
struct OnDisappearModifier<Content: View>: TypeSafeView {
1414
var body: TupleView1<Content>
1515
var action: @Sendable @MainActor () -> Void
1616

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? ObservableProperty else {
7070
continue
7171
}
7272

0 commit comments

Comments
 (0)