Skip to content

Commit d7d73cb

Browse files
committed
OcaWritablePropertyView: handle property errors instead of staying stuck on ProgressView
Previously, OcaWritablePropertyView only handled .success results from the property async stream, silently ignoring .failure. This caused the view to remain stuck on ProgressView indefinitely when a property returned an error. Align error handling with OcaPropertyView by using Result<Value, Error> for currentValue and displaying an error message on failure.
1 parent b04ff18 commit d7d73cb

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

Sources/SwiftOCAUI/Views/PropertyView.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public struct OcaWritablePropertyView<Value: Sendable, Resolved: View>: View {
7575
let content: (Binding<Value>) -> Resolved
7676

7777
@State
78-
private var currentValue: Value?
78+
private var currentValue: Result<Value, Error>?
7979

8080
public init(
8181
_ object: OcaRoot,
@@ -89,9 +89,14 @@ public struct OcaWritablePropertyView<Value: Sendable, Resolved: View>: View {
8989

9090
private var binding: Binding<Value> {
9191
Binding<Value>(
92-
get: { currentValue! },
92+
get: {
93+
if case let .success(value) = currentValue {
94+
return value
95+
}
96+
preconditionFailure()
97+
},
9398
set: { newValue in
94-
currentValue = newValue
99+
currentValue = .success(newValue)
95100
Task {
96101
try? await property._setValue(object, newValue)
97102
}
@@ -101,8 +106,13 @@ public struct OcaWritablePropertyView<Value: Sendable, Resolved: View>: View {
101106

102107
public var body: some View {
103108
Group {
104-
if currentValue != nil {
105-
content(binding)
109+
if let currentValue {
110+
switch currentValue {
111+
case .success:
112+
content(binding)
113+
case let .failure(error):
114+
Text("Error: \(error.localizedDescription)").foregroundStyle(.tertiary)
115+
}
106116
} else {
107117
ProgressView()
108118
}
@@ -111,8 +121,15 @@ public struct OcaWritablePropertyView<Value: Sendable, Resolved: View>: View {
111121
await property.subscribe(object)
112122
do {
113123
for try await result in property.async {
114-
if case let .success(value) = result, let typed = value as? Value {
115-
currentValue = typed
124+
switch result {
125+
case let .success(value):
126+
if let typed = value as? Value {
127+
currentValue = .success(typed)
128+
} else {
129+
currentValue = .failure(Ocp1Error.status(.badFormat))
130+
}
131+
case let .failure(error):
132+
currentValue = .failure(error)
116133
}
117134
}
118135
} catch {}

0 commit comments

Comments
 (0)