Skip to content

Commit 83b304a

Browse files
committed
Apply reply payload from file validation
1 parent 3114d78 commit 83b304a

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
111111

112112
let replyPayload = try await channel.call(event: .user(user: event), payload: payload, timeout: PUSH_TIMEOUT)
113113

114-
switch replyPayload {
115-
case let .jsonPayload(json):
116-
switch json {
117-
case let .object(object):
118-
if case let .object(diff) = object["diff"] {
119-
try self.handleDiff(payload: .object(object: diff), baseURL: self.url)
120-
if case let .object(reply) = diff["r"] {
121-
return reply
122-
}
123-
} else if case let .object(redirectObject) = object["live_redirect"],
124-
let redirect = LiveRedirect(from: redirectObject, relativeTo: self.url)
125-
{
126-
try await session.redirect(redirect)
127-
} else if case let .object(redirectObject) = object["redirect"],
128-
case let .str(destinationString) = redirectObject["to"],
129-
let destination = URL(string: destinationString, relativeTo: self.url)
130-
{
131-
try await session.redirect(.init(kind: .push, to: destination, mode: .replaceTop))
132-
} else {
133-
return nil
134-
}
135-
default:
136-
fatalError("unsupported message type \(replyPayload)")
137-
}
138-
default:
139-
fatalError("unsupported message type \(replyPayload)")
140-
}
141-
return nil
114+
return try await handleEventReplyPayload(replyPayload)
142115
}
143116

144117
/// Creates a publisher that can be used to listen for server-sent LiveView events.
@@ -204,6 +177,37 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
204177
handleEvents(payload)
205178
try self.document?.mergeFragmentJson(String(data: try JSONEncoder().encode(payload), encoding: .utf8)!)
206179
}
180+
181+
func handleEventReplyPayload(_ replyPayload: LiveViewNativeCore.Payload) async throws -> [String:Any]? {
182+
switch replyPayload {
183+
case let .jsonPayload(json):
184+
switch json {
185+
case let .object(object):
186+
if case let .object(diff) = object["diff"] {
187+
try self.handleDiff(payload: .object(object: diff), baseURL: self.url)
188+
if case let .object(reply) = diff["r"] {
189+
return reply
190+
}
191+
} else if case let .object(redirectObject) = object["live_redirect"],
192+
let redirect = LiveRedirect(from: redirectObject, relativeTo: self.url)
193+
{
194+
try await session.redirect(redirect)
195+
} else if case let .object(redirectObject) = object["redirect"],
196+
case let .str(destinationString) = redirectObject["to"],
197+
let destination = URL(string: destinationString, relativeTo: self.url)
198+
{
199+
try await session.redirect(.init(kind: .push, to: destination, mode: .replaceTop))
200+
} else {
201+
return nil
202+
}
203+
default:
204+
fatalError("unsupported message type \(replyPayload)")
205+
}
206+
default:
207+
fatalError("unsupported message type \(replyPayload)")
208+
}
209+
return nil
210+
}
207211

208212
private func handleEvents(_ json: LiveViewNativeCore.Json) {
209213
guard case let .object(object) = json,

Sources/LiveViewNative/Stylesheets/Modifiers/Files/FileImporterModifier.swift

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,31 @@ struct _FileImporterModifier<R: RootRegistry>: ViewModifier {
7070
guard let liveChannel = context.coordinator.liveChannel
7171
else { return }
7272

73-
do {
74-
let files = try result.get().map({ url in
75-
LiveFile(
76-
try Data(contentsOf: url),
77-
UTType(filenameExtension: url.pathExtension)!.preferredMIMEType!,
78-
url.lastPathComponent,
79-
id
80-
)
81-
})
82-
Task {
83-
do {
84-
for file in files {
85-
print(try await liveChannel.validateUpload(file))
86-
}
87-
} catch {
88-
logger.log(level: .error, "\(error.localizedDescription)")
73+
Task {
74+
do {
75+
let files = try result.get().map({ url in
76+
LiveFile(
77+
try Data(contentsOf: url),
78+
UTType(filenameExtension: url.pathExtension)!.preferredMIMEType!,
79+
url.lastPathComponent,
80+
id
81+
)
82+
})
83+
for file in files {
84+
let replyPayload = try await liveChannel.validateUpload(file)
85+
try await context.coordinator.handleEventReplyPayload(replyPayload)
8986
}
87+
self.formModel?.fileUploads.append(
88+
contentsOf: files.map({ file in
89+
{
90+
try await liveChannel.uploadFile(file)
91+
print("upload complete")
92+
}
93+
})
94+
)
95+
} catch {
96+
logger.log(level: .error, "\(error.localizedDescription)")
9097
}
91-
self.formModel?.fileUploads.append(
92-
contentsOf: files.map({ file in
93-
{
94-
try await liveChannel.uploadFile(file)
95-
print("upload complete")
96-
}
97-
})
98-
)
99-
} catch {
100-
logger.log(level: .error, "\(error.localizedDescription)")
10198
}
10299
}
103100
#else

0 commit comments

Comments
 (0)