@aigens/aigens-sdk-core 0.0.7 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,15 +11,101 @@ import Capacitor
11
11
 
12
12
  @objc open class WebContainerViewController: CAPBridgeViewController {
13
13
 
14
+ // {themeColor: "#xxxxxx"}
14
15
  public var options: Dictionary<String, Any>?
15
-
16
+
17
+ var externalProtocols: [String] = [
18
+ "octopus://", "alipay://", "alipays://", "alipayhk://", "https://itunes.apple.com", "tel:", "mailto:", "itms-apps://itunes.apple.com", "https://apps.apple.com", "payme://"
19
+ ]
20
+ let containerView = WebContainer.webContainer()
21
+ var webContainerView: WebContainer {
22
+ return containerView
23
+ }
16
24
  override open func viewDidLoad() {
17
25
 
18
26
  print("WebContainerViewController viewDidLoad")
19
27
 
20
28
  self.becomeFirstResponder()
21
29
  loadWebViewCustom()
22
- //loadWebView()
30
+ initView()
31
+
32
+ }
33
+
34
+ private func initView(){
35
+
36
+ print("VC initView")
37
+
38
+ //let bundle = Bundle(for: WebContainerViewController.self)
39
+ //let containerView = WebContainerView()
40
+ //let containerView = bundle.loadNibNamed("WebContainerView", owner: self, options: nil)?.first as! UIView
41
+
42
+ self.view.addSubview(webContainerView)
43
+ webContainerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
44
+ setupOptions(webContainerView)
45
+ containerView.vc = self
46
+
47
+ //
48
+ //
49
+
50
+
51
+ // let containerView = WebContainerView()
52
+ // self.view = containerView
53
+ //
54
+ // //containerView.frame = self.view!.bounds
55
+ // containerView.backgroundColor = UIColor.red
56
+ // containerView.translatesAutoresizingMaskIntoConstraints = false
57
+ //
58
+ // let webview = self.webView
59
+ // webview?.frame = containerView.webArea!.bounds
60
+ // webview?.frame.size = containerView.webArea.frame.size
61
+ // containerView.webArea.addSubview(self.webView!)
62
+ //
63
+
64
+
65
+ }
66
+
67
+ open override func viewWillLayoutSubviews() {
68
+ super.viewWillLayoutSubviews()
69
+ webContainerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
70
+ }
71
+
72
+ private func setupOptions(_ view: WebContainer) {
73
+ if let theme = self.options?["themeColor"] as? String, let color = UIColor.getHex(hex: theme) {
74
+ self.view.backgroundColor = color
75
+ self.webView?.backgroundColor = color
76
+ self.webView?.scrollView.backgroundColor = color
77
+ view.setTheme(theme)
78
+ }
79
+ if let externalProtocols = options?["externalProtocols"] as? [String] {
80
+ self.externalProtocols.append(contentsOf: externalProtocols)
81
+ }
82
+
83
+ }
84
+
85
+ public final func loadWebViewCustom() {
86
+
87
+ //let bridge = self.bridge
88
+ //let plugins = self.bridge["plugins"]
89
+
90
+
91
+ let urlString = self.options?["url"] as? String;
92
+
93
+ if(urlString == nil){
94
+ return;
95
+ }
96
+
97
+ let url = URL(string: urlString!)
98
+
99
+ let member = self.options?["member"] as? Dictionary<String, Any>
100
+
101
+ CorePlugin.member = member
102
+ let deeplink = self.options?["deeplink"] as? Dictionary<String, Any>
103
+ CorePlugin.deeplink = deeplink
104
+
105
+ //bridge.webViewDelegationHandler.willLoadWebview(webView)
106
+ _ = webView?.load(URLRequest(url: url!))
107
+ webView?.navigationDelegate = self
108
+
23
109
  }
24
110
 
25
111
  //this method overwrite parent to return a desc with custom config.json
@@ -48,29 +134,153 @@ import Capacitor
48
134
 
49
135
  return descriptor
50
136
  }
137
+
138
+ deinit {
139
+ print("WebContainerViewController deinit")
140
+ }
51
141
 
52
- public final func loadWebViewCustom() {
53
-
54
- //let bridge = self.bridge
55
- //let plugins = self.bridge["plugins"]
56
-
142
+
143
+ }
57
144
 
58
- let urlString = self.options?["url"] as? String;
145
+ extension WebContainerViewController: WKNavigationDelegate {
146
+ // The force unwrap is part of the protocol declaration, so we should keep it.
147
+ // swiftlint:disable:next implicitly_unwrapped_optional
148
+ public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
149
+ // Reset the bridge on each navigation
150
+ // self.bridge?.reset()
151
+ }
152
+
153
+ public func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
154
+ webContainerView.showError(false)
155
+ webContainerView.showLoading(true)
156
+ }
157
+
158
+ public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
159
+ // post a notification for any listeners
160
+ NotificationCenter.default.post(name: .capacitorDecidePolicyForNavigationAction, object: navigationAction)
59
161
 
60
- if(urlString == nil){
61
- return;
162
+ // sanity check, these shouldn't ever be nil in practice
163
+ guard let bridge = bridge, let navURL = navigationAction.request.url else {
164
+ decisionHandler(.allow)
165
+ return
62
166
  }
63
-
64
- let url = URL(string: urlString!)
65
-
66
- let member = self.options?["member"] as? Dictionary<String, Any>
67
167
 
68
- CorePlugin.member = member
168
+ var isCanOpen = false
169
+ if externalProtocols.count > 0 {
170
+ externalProtocols.forEach { (url) in
171
+ if (navURL.absoluteString.starts(with: url)) {
172
+ isCanOpen = UIApplication.shared.canOpenURL(navURL) ;
173
+ return;
174
+ }
175
+ }
176
+ }
69
177
 
178
+ if isCanOpen {
179
+ if #available(iOS 10.0, *) {
180
+ UIApplication.shared.open(navURL, options: [:], completionHandler: nil);
181
+ } else {
182
+ UIApplication.shared.openURL(navURL)
183
+ }
184
+ decisionHandler(.cancel)
185
+ return;
186
+ }
70
187
 
71
- //bridge.webViewDelegationHandler.willLoadWebview(webView)
72
- _ = webView?.load(URLRequest(url: url!))
73
188
 
189
+ // first, give plugins the chance to handle the decision
190
+ // for pluginObject in bridge.plugins {
191
+ // let plugin = pluginObject.value
192
+ // let selector = NSSelectorFromString("shouldOverrideLoad:")
193
+ // if plugin.responds(to: selector) {
194
+ // let shouldOverrideLoad = plugin.shouldOverrideLoad(navigationAction)
195
+ // if shouldOverrideLoad != nil {
196
+ // if shouldOverrideLoad == true {
197
+ // decisionHandler(.cancel)
198
+ // return
199
+ // } else if shouldOverrideLoad == false {
200
+ // decisionHandler(.allow)
201
+ // return
202
+ // }
203
+ // }
204
+ // }
205
+ // }
206
+
207
+ // next, check if this is covered by the allowedNavigation configuration
208
+ if let host = navURL.host, bridge.config.shouldAllowNavigation(to: host) {
209
+ decisionHandler(.allow)
210
+ return
211
+ }
212
+
213
+ // otherwise, is this a new window or a main frame navigation but to an outside source
214
+ let toplevelNavigation = (navigationAction.targetFrame == nil || navigationAction.targetFrame?.isMainFrame == true)
215
+ if navURL.absoluteString.contains(bridge.config.serverURL.absoluteString) == false, toplevelNavigation {
216
+ // disallow and let the system handle it
217
+ if UIApplication.shared.applicationState == .active {
218
+ UIApplication.shared.open(navURL, options: [:], completionHandler: nil)
219
+ }
220
+ decisionHandler(.cancel)
221
+ return
222
+ }
223
+
224
+ // fallthrough to allowing it
225
+ decisionHandler(.allow)
226
+ }
227
+
228
+
229
+ // The force unwrap is part of the protocol declaration, so we should keep it.
230
+ // swiftlint:disable:next implicitly_unwrapped_optional
231
+ public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
232
+ // if case .initialLoad(let isOpaque) = webViewLoadingState {
233
+ // webView.isOpaque = isOpaque
234
+ // webViewLoadingState = .subsequentLoad
235
+ // }
236
+ webContainerView.showLoading(false)
237
+ webContainerView.showError(false)
238
+ CAPLog.print("⚡️ WebView loaded")
239
+ }
240
+
241
+ // The force unwrap is part of the protocol declaration, so we should keep it.
242
+ // swiftlint:disable:next implicitly_unwrapped_optional
243
+ public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
244
+ // if case .initialLoad(let isOpaque) = webViewLoadingState {
245
+ // webView.isOpaque = isOpaque
246
+ // webViewLoadingState = .subsequentLoad
247
+ // }
248
+ webContainerView.showLoading(false)
249
+
250
+ webContainerView.showError(true, error.localizedDescription)
251
+ CAPLog.print("⚡️ WebView failed to load")
252
+ CAPLog.print("⚡️ Error: " + error.localizedDescription)
253
+ }
254
+
255
+ // The force unwrap is part of the protocol declaration, so we should keep it.
256
+ // swiftlint:disable:next implicitly_unwrapped_optional
257
+ public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
258
+ webContainerView.showLoading(false)
259
+ // cancel
260
+ let e = error as NSError
261
+ if (e.code == NSURLErrorCancelled || e.code == -999) {
262
+ webContainerView.showError(false)
263
+ }
264
+ CAPLog.print("⚡️ WebView failed provisional navigation")
265
+ CAPLog.print("⚡️ Error: " + error.localizedDescription)
266
+ webContainerView.showError(true, error.localizedDescription)
267
+ }
268
+
269
+ public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
270
+ webView.reload()
271
+ }
272
+ }
74
273
 
274
+ extension UIColor {
275
+ static func getHex(hex: String, _ alpha: CGFloat = 1.0) -> UIColor? {
276
+ guard !hex.isEmpty && hex.hasPrefix("#") else { return nil }
277
+ var rgbValue: UInt32 = 0
278
+ let scanner = Scanner(string: hex)
279
+ scanner.scanLocation = 1
280
+ guard scanner.scanHexInt32(&rgbValue) else { return nil }
281
+ return UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
282
+ green: CGFloat((rgbValue & 0xFF00) >> 8) / 255.0,
283
+ blue: CGFloat((rgbValue & 0xFF)) / 255.0,
284
+ alpha: alpha);
75
285
  }
76
286
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aigens/aigens-sdk-core",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
4
4
  "description": "Aigens Order.Place Core Plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -74,5 +74,8 @@
74
74
  "android": {
75
75
  "src": "android"
76
76
  }
77
+ },
78
+ "dependencies": {
79
+ "aigens-mono": "file:../.."
77
80
  }
78
81
  }