-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPortalView.swift
77 lines (67 loc) · 2.51 KB
/
PortalView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// PortalView.swift
// ReactNativePortals
//
// Created by Steven Sherry on 10/5/22.
// Copyright © 2022 Ionic. All rights reserved.
//
import UIKit
import Capacitor
import IonicPortals
import React
@objc(IONPortalViewManager)
class PortalViewManager: RCTViewManager {
override class func requiresMainQueueSetup() -> Bool { true }
override func view() -> UIView! { PortalView() }
}
class PortalView: UIView {
private var webView: PortalUIView?
@objc var portal: [String: Any]? {
get {
guard let _portal = _portal else { return nil }
return try? _portal.encode(to: JSValueEncoder(optionalEncodingStrategy: .undefined))
}
set {
guard let portalDict = newValue else { return }
var portal: Portal
do {
let jsObject = JSTypes.coerceDictionaryToJSObject(portalDict) ?? [:]
portal = try Portal.decode(from: jsObject, with: JSValueDecoder())
} catch {
print(error.localizedDescription)
return
}
if portal.usesWebVitals {
let vitalsPlugin = WebVitalsPlugin { portalName, duration in
IonicPortals.PortalsPubSub
.shared
.publish(
["portalName": portalName, "duration": duration],
to: "webVitals:received"
)
}
portal._portal.plugins.append(.instance(vitalsPlugin))
}
_portal = portal
}
}
private var _portal: Portal? {
didSet {
guard let portal = _portal else { return }
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.webView?.removeFromSuperview()
let webView = PortalUIView(portal: portal._portal)
webView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: self.topAnchor),
webView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
webView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
])
self.webView = webView
}
}
}
}