Skip to content

Commit a6de46c

Browse files
committed
Pass _platform as a query param on initial page load
1 parent b04447a commit a6de46c

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

Sources/LiveViewNative/Coordinators/LiveSessionCoordinator.swift

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
5959
private var eventSubject = PassthroughSubject<(LiveViewCoordinator<R>, (String, Payload)), Never>()
6060
private var eventHandlers = Set<AnyCancellable>()
6161

62-
var isMounted: Bool = false
63-
6462
public convenience init(_ host: some LiveViewHost, config: LiveSessionConfiguration = .init(), customRegistryType: R.Type = R.self) {
6563
self.init(host.url, config: config, customRegistryType: customRegistryType)
6664
}
@@ -221,7 +219,9 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
221219
let data: Data
222220
let resp: URLResponse
223221
do {
224-
(data, resp) = try await configuration.urlSession.data(from: url)
222+
(data, resp) = try await configuration.urlSession.data(from: url.appending(queryItems: [
223+
.init(name: "_platform", value: platform)
224+
]))
225225
} catch {
226226
throw LiveConnectionError.initialFetchError(error)
227227
}
@@ -388,3 +388,62 @@ public class LiveSessionCoordinator<R: RootRegistry>: ObservableObject {
388388
}
389389
}
390390
}
391+
392+
extension LiveSessionCoordinator {
393+
var platform: String { "swiftui" }
394+
var platformMeta: Payload {
395+
[
396+
"os_name": getOSName(),
397+
"os_version": getOSVersion(),
398+
"user_interface_idiom": getUserInterfaceIdiom()
399+
]
400+
}
401+
402+
private func getOSName() -> String {
403+
#if os(macOS)
404+
return "macOS"
405+
#elseif os(tvOS)
406+
return "tvOS"
407+
#elseif os(watchOS)
408+
return "watchOS"
409+
#else
410+
return "iOS"
411+
#endif
412+
}
413+
414+
private func getOSVersion() -> String {
415+
#if os(watchOS)
416+
return WKInterfaceDevice.current().systemVersion
417+
#elseif os(macOS)
418+
let operatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion
419+
let majorVersion = operatingSystemVersion.majorVersion
420+
let minorVersion = operatingSystemVersion.minorVersion
421+
let patchVersion = operatingSystemVersion.patchVersion
422+
423+
return "\(majorVersion).\(minorVersion).\(patchVersion)"
424+
#else
425+
return UIDevice.current.systemVersion
426+
#endif
427+
}
428+
429+
private func getUserInterfaceIdiom() -> String {
430+
#if os(watchOS)
431+
return "watch"
432+
#elseif os(macOS)
433+
return "mac"
434+
#else
435+
switch UIDevice.current.userInterfaceIdiom {
436+
case .phone:
437+
return "phone"
438+
case .pad:
439+
return "pad"
440+
case .mac:
441+
return "mac"
442+
case .tv:
443+
return "tv"
444+
default:
445+
return "unspecified"
446+
}
447+
#endif
448+
}
449+
}

Sources/LiveViewNative/Coordinators/LiveViewCoordinator.swift

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
217217
var connectParams = session.configuration.connectParams?(self.url) ?? [:]
218218
connectParams["_mounts"] = 0
219219
connectParams["_csrf_token"] = domValues.phxCSRFToken
220-
connectParams["_platform"] = "swiftui"
221-
connectParams["_platform_meta"] = try getPlatformMetadata()
220+
connectParams["_platform"] = session.platform
221+
connectParams["_platform_meta"] = session.platformMeta
222222
connectParams["_global_native_bindings"] = Dictionary(uniqueKeysWithValues: R.globalBindings.map({ ($0, R.bindingValue(forKey: $0, in: nil)) }))
223223

224224
let params: Payload = [
@@ -275,62 +275,6 @@ public class LiveViewCoordinator<R: RootRegistry>: ObservableObject {
275275
self.document = nil
276276
}
277277

278-
private func getPlatformMetadata() throws -> Payload {
279-
return [
280-
"os_name": getOSName(),
281-
"os_version": getOSVersion(),
282-
"user_interface_idiom": getUserInterfaceIdiom()
283-
]
284-
}
285-
286-
private func getOSName() -> String {
287-
#if os(macOS)
288-
return "macOS"
289-
#elseif os(tvOS)
290-
return "tvOS"
291-
#elseif os(watchOS)
292-
return "watchOS"
293-
#else
294-
return "iOS"
295-
#endif
296-
}
297-
298-
private func getOSVersion() -> String {
299-
#if os(watchOS)
300-
return WKInterfaceDevice.current().systemVersion
301-
#elseif os(macOS)
302-
let operatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion
303-
let majorVersion = operatingSystemVersion.majorVersion
304-
let minorVersion = operatingSystemVersion.minorVersion
305-
let patchVersion = operatingSystemVersion.patchVersion
306-
307-
return "\(majorVersion).\(minorVersion).\(patchVersion)"
308-
#else
309-
return UIDevice.current.systemVersion
310-
#endif
311-
}
312-
313-
private func getUserInterfaceIdiom() -> String {
314-
#if os(watchOS)
315-
return "watch"
316-
#elseif os(macOS)
317-
return "mac"
318-
#else
319-
switch UIDevice.current.userInterfaceIdiom {
320-
case .phone:
321-
return "phone"
322-
case .pad:
323-
return "pad"
324-
case .mac:
325-
return "mac"
326-
case .tv:
327-
return "tv"
328-
default:
329-
return "unspecified"
330-
}
331-
#endif
332-
}
333-
334278
enum JoinResult {
335279
case rendered(Payload)
336280
case redirect(LiveRedirect)

0 commit comments

Comments
 (0)