Skip to content

Commit d01e5ff

Browse files
committed
Apply reply payload from file validation
1 parent 2275a21 commit d01e5ff

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
@@ -107,34 +107,7 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
107107

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

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

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

204208
private func handleEvents(_ json: LiveViewNativeCore.Json) {
205209
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)