-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOSIABWebViewRouterAdapter.swift
More file actions
157 lines (138 loc) · 6.39 KB
/
OSIABWebViewRouterAdapter.swift
File metadata and controls
157 lines (138 loc) · 6.39 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import SwiftUI
/// Adapter that makes the required calls so that an `WKWebView` implementation can perform the Web View routing.
/// This is done via a customisable interface.
public class OSIABWebViewRouterAdapter: NSObject, OSIABRouter {
public typealias ReturnType = UIViewController
/// Object that contains the value to format the visual presentation.
private let options: OSIABWebViewOptions
/// Custom headers to be used by the WebView.
private let customHeaders: [String: String]?
/// Object that manages the browser's cache
private let cacheManager: OSIABCacheManager
/// Object that manages all the callbacks available for the WebView.
private let callbackHandler: OSIABWebViewCallbackHandler
/// Constructor method.
/// - Parameters:
/// - options: Object that contains the value to format the visual presentation.
/// - customHeaders: Custom headers to be used by the WebView. `nil` is provided in case of no value.
/// - cacheManager: Object that manages the browser's cache
/// - callbackHandler: Object that manages all the callbacks available for the WebView.
public init(
options: OSIABWebViewOptions,
customHeaders: [String: String]? = nil,
cacheManager: OSIABCacheManager,
callbackHandler: OSIABWebViewCallbackHandler
) {
self.options = options
self.customHeaders = customHeaders
self.cacheManager = cacheManager
self.callbackHandler = callbackHandler
}
public func handleOpen(_ url: URL, _ completionHandler: @escaping (ReturnType) -> Void) {
if self.options.clearCache {
self.cacheManager.clearCache()
} else if self.options.clearSessionCache {
self.cacheManager.clearSessionCache()
}
let viewModel = OSIABWebViewModel(
url: url,
customHeaders: customHeaders,
webViewConfiguration: options.toConfigurationModel().toWebViewConfiguration(),
scrollViewBounces: options.allowOverScroll,
customUserAgent: options.customUserAgent,
backForwardNavigationGestures: options.allowsBackForwardNavigationGestures,
uiModel: options.toUIModel(),
callbackHandler: callbackHandler
)
let dismissCallback: () -> Void = { self.callbackHandler.onBrowserClosed(true) }
let hostingController: UIViewController
if #available(iOS 14.0, *) {
hostingController = OSIABWebViewController(rootView: .init(viewModel), dismiss: dismissCallback)
} else {
hostingController = OSIABWebView13Controller(rootView: .init(viewModel), dismiss: dismissCallback)
}
hostingController.modalPresentationStyle = options.modalPresentationStyle
hostingController.modalTransitionStyle = options.modalTransitionStyle
hostingController.presentationController?.delegate = self
completionHandler(hostingController)
}
}
// MARK: - Accelerator methods.
private extension OSIABWebViewOptions {
/// Converts the current value to `OSIABWebViewConfigurationModel` equivalent.
/// - Returns: The `OSIABWebViewConfigurationModel` equivalent value.
func toConfigurationModel() -> OSIABWebViewConfigurationModel {
.init(
mediaTypesRequiringUserActionForPlayback,
enableViewportScale,
allowInLineMediaPlayback,
surpressIncrementalRendering
)
}
/// Converts the current value to `OSIABWebViewUIModel` equivalent.
/// - Returns: The `OSIABWebViewUIModel` equivalent value.
func toUIModel() -> OSIABWebViewUIModel {
.init(
showURL: showURL,
showToolbar: showToolbar,
toolbarPosition: toolbarPosition,
showNavigationButtons: showNavigationButtons,
leftToRight: leftToRight,
closeButtonText: closeButtonText
)
}
}
// MARK: - UIAdaptivePresentationControllerDelegate implementation
extension OSIABWebViewRouterAdapter: UIAdaptivePresentationControllerDelegate {
public func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
self.callbackHandler.onBrowserClosed(true)
}
}
/// A subclass for `UIHostingController` where it's possible to delegate the `dismiss` call to its callers.
@available(iOS 14.0, *)
private class OSIABWebViewController: UIHostingController<OSIABWebViewWrapperView> {
/// Callback to trigger when the view controller is closed.
let dismiss: (() -> Void)?
/// Constructor method.
/// - Parameters:
/// - rootView: The root view of the SwiftUI view hierarchy that you want to manage using the hosting view controller.
/// - dismiss: The callback to trigger when the view controller is dismissed.
init(rootView: OSIABWebViewWrapperView, dismiss: (() -> Void)?) {
self.dismiss = dismiss
super.init(rootView: rootView)
}
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
self.dismiss = nil
super.init(coder: aDecoder)
}
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: {
self.dismiss?()
completion?()
})
}
}
/// A subclass for `UIHostingController` where it's possible to delegate the `dismiss` call to its callers.
@available(iOS, deprecated: 14.0, message: "Use OSIABWebViewController for iOS 14.0+")
private class OSIABWebView13Controller: UIHostingController<OSIABWebView13WrapperView> {
/// Callback to trigger when the view controller is closed.
let dismiss: (() -> Void)?
/// Constructor method.
/// - Parameters:
/// - rootView: The root view of the SwiftUI view hierarchy that you want to manage using the hosting view controller.
/// - dismiss: The callback to trigger when the view controller is dismissed.
init(rootView: OSIABWebView13WrapperView, dismiss: (() -> Void)?) {
self.dismiss = dismiss
super.init(rootView: rootView)
}
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
self.dismiss = nil
super.init(coder: aDecoder)
}
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: {
self.dismiss?()
completion?()
})
}
}